viviman 3 years ago
parent
commit
bc9e4af365
  1. 1
      golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go
  2. 2
      golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go
  3. 21
      golang_learn/data_office/get_hy_sql/main.exe.manifest
  4. 118
      golang_learn/data_office/get_hy_sql/main.go
  5. BIN
      golang_learn/data_office/get_hy_sql/main.syso
  6. BIN
      golang_learn/data_office/get_hy_sql/rc.ico

1
golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go

@ -3,7 +3,6 @@ package main
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"time"
) )
func main() { func main() {

2
golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go

@ -2,12 +2,14 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"os" "os"
) )
func processLine(line []byte) { func processLine(line []byte) {
os.Stdout.Write(line) os.Stdout.Write(line)
fmt.Println(string(line))
} }
func ReadLine(filePth string, hookfn func([]byte)) error { func ReadLine(filePth string, hookfn func([]byte)) error {

21
golang_learn/data_office/get_hy_sql/main.exe.manifest

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="x86"
name="controls"
type="win32"
></assemblyIdentity>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
></assemblyIdentity>
</dependentAssembly>
</dependency>
</assembly>

118
golang_learn/data_office/get_hy_sql/main.go

@ -0,0 +1,118 @@
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"
}
}

BIN
golang_learn/data_office/get_hy_sql/main.syso

Binary file not shown.

BIN
golang_learn/data_office/get_hy_sql/rc.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Loading…
Cancel
Save