diff --git a/golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go b/golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go index 2f7bcdb..1b82580 100644 --- a/golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go +++ b/golang_learn/data_func/go_to_file/go_to_txt/readTXT/读取文件.go @@ -3,7 +3,6 @@ package main import ( "os" "path/filepath" - "time" ) func main() { diff --git a/golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go b/golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go index d15f878..6981f7b 100644 --- a/golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go +++ b/golang_learn/data_func/go_to_file/go_to_txt/readTXT/逐行读取.go @@ -2,12 +2,14 @@ package main import ( "bufio" + "fmt" "io" "os" ) func processLine(line []byte) { os.Stdout.Write(line) + fmt.Println(string(line)) } func ReadLine(filePth string, hookfn func([]byte)) error { diff --git a/golang_learn/data_office/get_hy_sql/main.exe.manifest b/golang_learn/data_office/get_hy_sql/main.exe.manifest new file mode 100644 index 0000000..c52acbc --- /dev/null +++ b/golang_learn/data_office/get_hy_sql/main.exe.manifest @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/golang_learn/data_office/get_hy_sql/main.go b/golang_learn/data_office/get_hy_sql/main.go new file mode 100644 index 0000000..71295aa --- /dev/null +++ b/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" + } +} diff --git a/golang_learn/data_office/get_hy_sql/main.syso b/golang_learn/data_office/get_hy_sql/main.syso new file mode 100644 index 0000000..b620536 Binary files /dev/null and b/golang_learn/data_office/get_hy_sql/main.syso differ diff --git a/golang_learn/data_office/get_hy_sql/rc.ico b/golang_learn/data_office/get_hy_sql/rc.ico new file mode 100644 index 0000000..a06bf77 Binary files /dev/null and b/golang_learn/data_office/get_hy_sql/rc.ico differ