需要导出的sql 表
代码 需要用到 "github.com/360EntSecGroup-Skylar/excelize" 请自行下载
此代码只限制 A到Z列,只限于一个sheet,数据量太大 建议修改
func SQL2XLSX(autoField []string,fileName string){
f:= excelize.NewFile()
//写入自定义字段
for i:=0;i<len(autoField);i++{
k:=strings.ToUpper(string(rune(97+i)))+"1"
fmt.Println(k,autoField[i])
f.SetCellValue("Sheet1", k, autoField[i])
}
SQL:=grdsql.NewDb("site_theme")
query:="SELECT theme_id,theme_des,option_A,option_B,option_C,option_D,correct_option from jijiu"
defer SQL.Close()
if rows,err:=SQL.Query(query);err==nil{
cloumns, err:= rows.Columns()
values:= make([]sql.RawBytes, len(cloumns))
scanArgs:= make([]interface{}, len(values))
for i:= range values{
scanArgs[i] = &values[i]
}
index:= 0 //写入xls的行数
for rows.Next() {
err= rows.Scan(scanArgs...)
if err!= nil{
}
var valuestring
for i, col:= range values{
if col== nil{
value= " "
} else {
value= string(col)
}
k:= strings.ToUpper(string(rune(97+i))) + strconv.Itoa(index+2) //生成XLS列名
fmt.Println(k)
f.SetCellValue("Sheet1", k, value)
fmt.Println(cloumns[i], ": ", value)
}
index++
}
err= f.SaveAs(fileName)
if err!= nil{
fmt.Println(err)
}
}
}
func main(){
//自定义字段名称
tt:=[]string{"编号","描述","选项A","选项B","选项C","选项D","正确答案"}
SQL2XLSX(tt,"test.xlsx")
}
有疑问加站长微信联系(非本文作者)