代码拉取完成,页面将自动刷新
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)
index := -1
for j := 0; j < elemType.NumField(); j++ {
field := elemType.Field(j)
tag := field.Tag.Get("xlsx")
name := tag
if tag == "" || tag == "-" {
continue
}
index++
if index == -1 {
continue
}
column, _ := ConvertNumToChars(index)
// 设置表头
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。