omq/export_excel.go

44 lines
734 B
Go
Raw Permalink Normal View History

2023-11-17 20:34:20 +05:00
package main
import (
"github.com/xuri/excelize/v2"
)
// Экспорт в Excel
type XlsxExporter struct{}
func (x *XlsxExporter) FileExt() string {
return ".xlsx"
}
func (x *XlsxExporter) Convert(filePath string, rows chan []any) error {
excelFile := excelize.NewFile()
defer excelFile.Close()
excelFile.Path = filePath + x.FileExt()
stream, err := excelFile.NewStreamWriter("Sheet1")
if err != nil {
return err
}
rowNum := 0
for row := range rows {
rowNum += 1
cell, err := excelize.CoordinatesToCellName(1, rowNum)
if err != nil {
return err
}
if err := stream.SetRow(cell, row); err != nil {
return err
}
}
err = stream.Flush()
if err != nil {
return err
}
return excelFile.Save()
}