123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package excelutils
- import (
- "bytes"
- "fmt"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/xuri/excelize/v2"
- "log"
- "reflect"
- "strconv"
- )
- func ExportExcel(titleList []string, data []interface{}, sheetName string) ([]byte, error) {
- f := excelize.NewFile()
- defer func() {
- err := f.Close()
- if err != nil {
- log.Println("ExportExcel Close File:", err.Error())
- }
- }()
- err := f.SetSheetName("Sheet1", sheetName)
- if err != nil {
- return nil, err
- }
- header := make([]string, 0)
- for _, v := range titleList {
- header = append(header, v)
- }
- rowStyleID, _ := f.NewStyle(&excelize.Style{
- Font: &excelize.Font{
- Family: "arial",
- Size: 13,
- Color: "#666666",
- },
- Alignment: &excelize.Alignment{
- Horizontal: "center",
- Vertical: "center",
- },
- })
- err = f.SetSheetRow(sheetName, "A1", &header)
- if err != nil {
- return nil, err
- }
- err = f.SetRowHeight(sheetName, 1, 30)
- if err != nil {
- return nil, err
- }
- length := len(titleList)
- headStyle := strutils.GenerateUpperLetters(length)
- var lastRow string
- var widthRow string
- for k, v := range headStyle {
- if k == length-1 {
- lastRow = fmt.Sprintf("%s1", v)
- widthRow = v
- }
- }
- if err := f.SetColWidth(sheetName, "A", widthRow, 20); err != nil {
- return nil, err
- }
- rowNum := 1
- _ = f.SetCellStyle(sheetName, fmt.Sprintf("A%d", 1), fmt.Sprintf("%s", lastRow), rowStyleID)
- for _, v := range data {
- t := reflect.TypeOf(v)
- value := reflect.ValueOf(v)
- row := make([]interface{}, 0)
- for l := 0; l < t.NumField(); l++ {
- val := value.Field(l).Interface()
- row = append(row, val)
- }
- rowNum++
- err := f.SetSheetRow(sheetName, "A"+strconv.Itoa(rowNum), &row)
- if err != nil {
- return nil, err
- }
- }
- buffer := &bytes.Buffer{}
- err = f.Write(buffer)
- if err != nil {
- return nil, err
- }
- return buffer.Bytes(), nil
- }
|