123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package excel
- import (
- "bytes"
- "fmt"
- "kpt-tmr-group/pkg/logger/zaplog"
- "kpt-tmr-group/pkg/xerr"
- "github.com/xuri/excelize/v2"
- "go.uber.org/zap"
- )
- func Export(titles []interface{}, rows []interface{}) (*bytes.Buffer, error) {
- file := excelize.NewFile()
- defer file.Close()
- streamWriter, err := file.NewStreamWriter("Sheet1")
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if err = streamWriter.SetRow("A1", titles); err != nil {
- return nil, xerr.WithStack(err)
- }
- for i, item := range rows {
- cell, err := excelize.CoordinatesToCellName(1, i+2)
- if err != nil {
- zaplog.Error("excel.CoordinatesToCellName", zap.Any("Err", err))
- continue
- }
- row := make([]interface{}, 0)
- // TODO 待封装
- fmt.Println(item)
- /*row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
- item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
- item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)*/
- if err = streamWriter.SetRow(cell, row); err != nil {
- return nil, xerr.WithStack(err)
- }
- }
- if err = streamWriter.Flush(); err != nil {
- return nil, xerr.WithStack(err)
- }
- return file.WriteToBuffer()
- }
|