forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.7 KiB
118 lines
2.7 KiB
package main
|
|
|
|
/*
|
|
思路:
|
|
首先:配置文件,若不存在生成配置文件,生成配置文件
|
|
其次:通过配置获取数据库链接 ,并链接和查询语句
|
|
最后:将查询语句生成excel
|
|
|
|
*/
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/360EntSecGroup-Skylar/excelize"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
//存在所有对象信息 - 集合
|
|
func main() {
|
|
db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
|
|
|
|
// QuerySQL(db, "select A0824 学位名称,count(1) 数量 from a08 where A0824 is not null and A0824 <> '' group by A0824")
|
|
|
|
QuerySQL(db, "select (select code_name from code_value where CODE_VALUE=A0827 and CODE_TYPE='GB16835') 学位名称,count(1) 数量 from a08 where A0827 is not null and A0827 <> '' group by A0827")
|
|
}
|
|
|
|
// 获取表数据
|
|
func QuerySQL(db *sql.DB, sqlStr string) {
|
|
xlsx := excelize.NewFile()
|
|
rows, _ := db.Query(sqlStr)
|
|
defer rows.Close()
|
|
cloumns, _ := rows.Columns()
|
|
values := make([]sql.RawBytes, len(cloumns))
|
|
scanArgs := make([]interface{}, len(values))
|
|
for i := range values {
|
|
scanArgs[i] = &values[i]
|
|
}
|
|
isFrist := true
|
|
rowAfter := 1
|
|
colBefor := ""
|
|
colAfter := ""
|
|
for rows.Next() {
|
|
colBefor = ""
|
|
_ = rows.Scan(scanArgs...)
|
|
var value string
|
|
for i, col := range values {
|
|
if col == nil {
|
|
value = "NULL"
|
|
} else {
|
|
value = string(col)
|
|
}
|
|
colBefor = strings.Replace(ASCIINext(colBefor), " ", "", -1)
|
|
colAfter = colBefor + strconv.Itoa(rowAfter)
|
|
if isFrist {
|
|
colAfter = colBefor + "1"
|
|
xlsx.SetCellValue("Sheet1", colAfter, cloumns[i])
|
|
colAfter = colBefor + "2"
|
|
xlsx.SetCellValue("Sheet1", colAfter, value)
|
|
rowAfter = 2
|
|
} else {
|
|
xlsx.SetCellValue("Sheet1", colAfter, value)
|
|
}
|
|
//fmt.Print(cloumns[i], ": ", value) //输出字段名 - 值
|
|
}
|
|
isFrist = false
|
|
rowAfter += 1
|
|
}
|
|
var path string
|
|
path = "./查询结果-" + time.Now().Format("2006102150405") + ".xlsx"
|
|
errExcel := xlsx.SaveAs(path)
|
|
if errExcel != nil {
|
|
fmt.Println(errExcel)
|
|
}
|
|
}
|
|
|
|
// 获取行向单元格的下格子
|
|
func ASCIINext(strFrist string) string {
|
|
if strFrist == "" {
|
|
return "A"
|
|
}
|
|
switch len(strFrist) {
|
|
case 1:
|
|
if strFrist == "Z" {
|
|
return "AA"
|
|
} else {
|
|
return string(strFrist[0] + 1)
|
|
}
|
|
case 2:
|
|
if strFrist == "ZZ" {
|
|
return "AAA"
|
|
} else {
|
|
if strFrist[1:] == "Z" {
|
|
return string(strFrist[0]+1) + "A"
|
|
} else {
|
|
return strFrist[:1] + string(strFrist[1]+1)
|
|
}
|
|
}
|
|
case 3:
|
|
if strFrist == "ZZZ" {
|
|
return "AAAA"
|
|
} else {
|
|
if strFrist[2:] == "Z" {
|
|
if strFrist[1:2] == "Z" {
|
|
return string(strFrist[0]+1) + "AA"
|
|
} else {
|
|
return strFrist[:1] + string(strFrist[1]+1) + "A"
|
|
}
|
|
} else {
|
|
return strFrist[:2] + string(strFrist[2]+1)
|
|
}
|
|
}
|
|
default:
|
|
return "A"
|
|
}
|
|
}
|
|
|