Files
servicebase/pkg/common/CmdTools/ExcelTool.go
2025-11-18 17:48:20 +08:00

71 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"strconv"
"github.com/tealeg/xlsx"
)
func main02() {
data := ReadSheet("./CmdTools/星钻VIP体系.xlsx", 0)
fmt.Println(json.Marshal(data))
for index, item := range data {
if index > 0 {
level, _ := strconv.Atoi(item[0])
exp, _ := strconv.Atoi(item[1])
fmt.Printf("insert into t_vip_config (id,vip_level,min_exp,vip_name,vip_icon,create_time) values (%d,%d,%d,'v%d','https://xz-static.10909.com/XingZuanCommon/Img/Vip/%d@2x.png',now());\n", level, level, exp, level, level)
}
}
}
func ReadSheet(file string, sheet int) [][]string {
var result [][]string
xlFile, err := xlsx.OpenFile(file)
if err != nil {
fmt.Println(err.Error())
return result
}
for index, row := range xlFile.Sheets[sheet].Rows {
if index > 60 {
break
}
var data []string
for index, cell := range row.Cells {
if index > 1 {
break
}
text := cell.String()
println(text)
data = append(data, text)
}
result = append(result, data)
}
return result
}
func ReadAll(file string) {
// 打开文件
xlFile, err := xlsx.OpenFile(file)
if err != nil {
fmt.Println(err.Error())
return
}
// 遍历sheet页读取
for _, sheet := range xlFile.Sheets {
fmt.Println("sheet name: ", sheet.Name)
//遍历行读取
for _, row := range sheet.Rows {
// 遍历每行的列读取
for _, cell := range row.Cells {
text := cell.String()
fmt.Printf("%20s", text)
}
fmt.Print("\n")
}
}
fmt.Println("\n\nimport success")
}