|
|
@@ -3,7 +3,7 @@ package excelutils
|
|
|
import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
- "git.sxidc.com/go-tools/utils/strutils"
|
|
|
+ "github.com/pkg/errors"
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
"log"
|
|
|
"reflect"
|
|
|
@@ -52,7 +52,12 @@ func ExportExcel(titleList []string, data []interface{}, sheetName string) ([]by
|
|
|
}
|
|
|
|
|
|
length := len(titleList)
|
|
|
- headStyle := strutils.GenerateUpperLetters(length)
|
|
|
+
|
|
|
+ headStyle, err := generateExcelColumnIndexes(length)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
var lastRow string
|
|
|
var widthRow string
|
|
|
for k, v := range headStyle {
|
|
|
@@ -94,3 +99,41 @@ func ExportExcel(titleList []string, data []interface{}, sheetName string) ([]by
|
|
|
|
|
|
return buffer.Bytes(), nil
|
|
|
}
|
|
|
+
|
|
|
+func generateExcelColumnIndexes(columnNum int) ([]string, error) {
|
|
|
+ if columnNum > 26*27 {
|
|
|
+ return nil, errors.New("columnNum is too large")
|
|
|
+ }
|
|
|
+
|
|
|
+ indexes := make([]string, 0)
|
|
|
+
|
|
|
+ for currentColumn := 1; currentColumn <= columnNum; currentColumn++ {
|
|
|
+ var index string
|
|
|
+
|
|
|
+ if currentColumn%26 == 0 {
|
|
|
+ index = "Z"
|
|
|
+ } else {
|
|
|
+ index = string(rune('A' + currentColumn%26 - 1))
|
|
|
+ }
|
|
|
+
|
|
|
+ if currentColumn <= 26 {
|
|
|
+ indexes = append(indexes, index)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ upperLetterIndex := currentColumn / 26
|
|
|
+ if upperLetterIndex%26 == 0 && currentColumn != 26*26 {
|
|
|
+ index = "Z" + index
|
|
|
+ } else {
|
|
|
+ if currentColumn%26 == 0 {
|
|
|
+ index = string(rune('A'+upperLetterIndex-2)) + "Z"
|
|
|
+ } else {
|
|
|
+ index = string(rune('A'+upperLetterIndex%26-1)) + index
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ indexes = append(indexes, index)
|
|
|
+ }
|
|
|
+
|
|
|
+ return indexes, nil
|
|
|
+}
|