excel_export.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package excel
  2. import (
  3. "bytes"
  4. "fmt"
  5. "kpt-tmr-group/pkg/logger/zaplog"
  6. "kpt-tmr-group/pkg/xerr"
  7. "github.com/xuri/excelize/v2"
  8. "go.uber.org/zap"
  9. )
  10. func Export(titles []interface{}, rows []interface{}) (*bytes.Buffer, error) {
  11. file := excelize.NewFile()
  12. defer file.Close()
  13. streamWriter, err := file.NewStreamWriter("Sheet1")
  14. if err != nil {
  15. return nil, xerr.WithStack(err)
  16. }
  17. if err = streamWriter.SetRow("A1", titles); err != nil {
  18. return nil, xerr.WithStack(err)
  19. }
  20. for i, item := range rows {
  21. cell, err := excelize.CoordinatesToCellName(1, i+2)
  22. if err != nil {
  23. zaplog.Error("excel.CoordinatesToCellName", zap.Any("Err", err))
  24. continue
  25. }
  26. row := make([]interface{}, 0)
  27. // TODO 待封装
  28. fmt.Println(item)
  29. /*row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
  30. item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
  31. item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)*/
  32. if err = streamWriter.SetRow(cell, row); err != nil {
  33. return nil, xerr.WithStack(err)
  34. }
  35. }
  36. if err = streamWriter.Flush(); err != nil {
  37. return nil, xerr.WithStack(err)
  38. }
  39. return file.WriteToBuffer()
  40. }