提交 0e35d947 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

set excel style

上级 c699bc91
......@@ -3,11 +3,12 @@ package action
import (
"database/sql"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
numbUtils "github.com/easysoft/zendata/src/utils/numb"
_ "github.com/mattn/go-sqlite3"
"strconv"
)
func Upgrade() {
......@@ -20,7 +21,7 @@ func Upgrade() {
return
}
sql := "SELECT id, 'name', state, zipCode, cityCode FROM city"
sql := "SELECT id, name, state, zipCode, cityCode FROM city"
rows, err := db.Query(sql)
if err != nil {
logUtils.Screen("fail to exec query " + sql + ": " + err.Error())
......@@ -28,7 +29,15 @@ func Upgrade() {
}
sqls := make([]string, 0)
sheet := "Sheet1"
excel := excelize.NewFile()
headerData := []interface{}{"seq", "name", "state", "zipCode", "cityCode"}
colNumb := AddExcelRow(excel, sheet, 1, headerData)
rowIndex := 1
for rows.Next() {
var seq string
var id int
var name string
var state string
......@@ -40,12 +49,26 @@ func Upgrade() {
logUtils.Screen("fail to get sqlite3 row: " + err.Error())
return
}
fmt.Println(numbUtils.NumToBHex(id), name, state, zipCode, cityCode)
seq = numbUtils.NumToBHex(id)
fmt.Println(seq, name, state, zipCode, cityCode)
// gen update sql
sql := fmt.Sprintf("UPDATE city SET seq = '%s' where id = %d;", numbUtils.NumToBHex(id), id)
sqls = append(sqls, sql)
// gen excel row
rowIndex = rowIndex + 1
rowData := []interface{}{seq, name, state, zipCode, cityCode}
AddExcelRow(excel, sheet, rowIndex, rowData)
}
headerStyle, err := excel.NewStyle(constant.ExcelHeader)
excel.SetCellStyle(sheet, "A1", colNumb + "1", headerStyle)
borderStyle, err := excel.NewStyle(constant.ExcelBorder)
excel.SetCellStyle(sheet, "A1", colNumb + strconv.Itoa(rowIndex), borderStyle)
// update seq column
for _, sql := range sqls {
_, err := db.Exec(sql)
......@@ -53,4 +76,22 @@ func Upgrade() {
logUtils.Screen("fail to update row: " + err.Error())
}
}
err = excel.SaveAs(constant.ExcelFile)
if err != nil {
logUtils.Screen("fail to save excel: " + err.Error())
}
}
func AddExcelRow(excel *excelize.File, sheet string, rowIndex int, cols []interface{}) string {
start := byte('A')
var numb string
for index, col := range cols {
numb = string(start + byte(index))
colNumb := numb + strconv.Itoa(rowIndex)
excel.SetCellValue(sheet, colNumb, col)
}
return numb
}
\ No newline at end of file
......@@ -48,6 +48,11 @@ var (
Definition model.Definition = model.Definition{}
LoadedFields map[string]model.Field = map[string]model.Field{}
DataDir string = "def/data/"
SqliteDriver string = "sqlite3"
SqliteSource string = "file:def/data/data.db"
SqliteSource string = "file:" + DataDir + "data.db"
ExcelFile string = DataDir + "data.xlsx"
ExcelBorder string = `{"border": [{"type":"left","color":"999999","style":1}, {"type":"top","color":"999999","style":1},
{"type":"bottom","color":"999999","style":1}, {"type":"right","color":"999999","style":1}]}`
ExcelHeader string = `{"fill":{"type":"pattern","pattern":1,"color":["E0EBF5"]}}`
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册