forked from go-admin-team/go-admin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67e6e72
commit 296430f
Showing
2 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/xuri/excelize/v2" | ||
) | ||
|
||
var Cols = []string{"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} | ||
|
||
// WriteXlsx 填充excel | ||
func WriteXlsx(sheet string, records interface{}) *excelize.File { | ||
xlsx := excelize.NewFile() // new file | ||
index := xlsx.NewSheet(sheet) // new sheet | ||
xlsx.SetActiveSheet(index) // set active (default) sheet | ||
t := reflect.TypeOf(records) | ||
|
||
if t.Kind() != reflect.Slice { | ||
return xlsx | ||
} | ||
|
||
s := reflect.ValueOf(records) | ||
for i := 0; i < s.Len(); i++ { | ||
elem := s.Index(i).Interface() | ||
elemType := reflect.TypeOf(elem) | ||
elemValue := reflect.ValueOf(elem) | ||
for j := 0; j < elemType.NumField(); j++ { | ||
field := elemType.Field(j) | ||
tag := field.Tag.Get("xlsx") | ||
name := tag | ||
column, _ := ConvertNumToChars(j) | ||
if tag == "" { | ||
continue | ||
} | ||
// 设置表头 | ||
if i == 0 { | ||
err := xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+1), name) | ||
if err != nil { | ||
return nil | ||
} | ||
} | ||
// 设置内容 | ||
err := xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+2), elemValue.Field(j).Interface()) | ||
if err != nil { | ||
return nil | ||
} | ||
} | ||
} | ||
return xlsx | ||
} | ||
|
||
func ConvertNumToChars(num int) (string, error) { | ||
var cols string | ||
v := num + 1 | ||
for v > 0 { | ||
k := v % 26 | ||
if k == 0 { | ||
k = 26 | ||
} | ||
v = (v - k) / 26 | ||
cols = Cols[k] + cols | ||
} | ||
return cols, nil | ||
} |