sirius/internal/service/convert.go

50 lines
1.1 KiB
Go
Raw Normal View History

2023-04-28 02:35:30 +03:00
package service
import (
"bytes"
"encoding/json"
"errors"
"strconv"
"github.com/xuri/excelize/v2"
"gitstore.ru/tolikproh/sirius/internal/model"
)
func SiriusConv(data []byte) (*bytes.Buffer, error) {
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
if !json.Valid(data) {
return nil, errors.New("formated data not json")
}
var sirius model.Sirius
err := json.Unmarshal(data, &sirius)
if err != nil {
return nil, errors.New("Error unmarshal file")
}
return SaveToExel(sirius.NewBolid().ZoneInfo())
}
func SaveToExel(data []model.ZoneInfo) (*bytes.Buffer, error) {
f := excelize.NewFile()
defer f.Close()
// Create a new sheet.
f.SetCellValue("Sheet1", "A2", "№ зоны")
f.SetCellValue("Sheet1", "B2", "Описание зоны")
f.SetCellValue("Sheet1", "C2", "Адреса в зоне")
i := 3
for _, d := range data {
f.SetCellValue("Sheet1", "A"+strconv.Itoa(i), d.ZoneNum)
f.SetCellValue("Sheet1", "B"+strconv.Itoa(i), d.ZoneName)
f.SetCellValue("Sheet1", "C"+strconv.Itoa(i), d.InputString())
i++
}
return f.WriteToBuffer()
}