Skip to content

Commit

Permalink
docs📝: add excel action
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjianzhang committed Sep 16, 2022
1 parent 67e6e72 commit 296430f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
58 changes: 52 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.18
require (
github.com/BurntSushi/toml v0.3.1
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/bsm/redislock v0.5.0
github.com/fsnotify/fsnotify v1.4.7
github.com/ghodss/yaml v1.0.0
Expand All @@ -17,21 +16,68 @@ require (
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/imdario/mergo v0.3.9
github.com/json-iterator/go v1.1.11
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/nsqio/go-nsq v1.0.8
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/robinjoseph08/redisqueue/v2 v2.1.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
github.com/xuri/excelize/v2 v2.6.1
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.26.0-rc.1
gorm.io/driver/mysql v1.0.3
gorm.io/gorm v1.21.11
gorm.io/plugin/dbresolver v1.1.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/git-chglog/git-chglog v0.0.0-20190611050339-63a4e637021f // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/goveralls v0.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/tsuyoshiwada/go-gitcmd v0.0.0-20180205145712-5f1f5f9475df // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/urfave/cli v1.20.0 // indirect
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.0.0-20210106214847-113979e3529a // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
gopkg.in/AlecAivazis/survey.v1 v1.8.5 // indirect
gopkg.in/kyokomi/emoji.v1 v1.5.1 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
65 changes: 65 additions & 0 deletions tools/utils/excel.go
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
}

0 comments on commit 296430f

Please sign in to comment.