From e7438b24f25cbbad2f44c33a4e56b30f0f614df2 Mon Sep 17 00:00:00 2001 From: VivimanZhang Date: Fri, 31 Mar 2023 22:24:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A1=A8=E6=A0=BC=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../go_to_excel/demo_he_bing/build.bat | 2 +- .../go_to_excel/demo_he_bing/main.go | 56 ++++++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/golang_learn/data_func/go_to_excel/demo_he_bing/build.bat b/golang_learn/data_func/go_to_excel/demo_he_bing/build.bat index f4e30a5..60c499e 100644 --- a/golang_learn/data_func/go_to_excel/demo_he_bing/build.bat +++ b/golang_learn/data_func/go_to_excel/demo_he_bing/build.bat @@ -2,4 +2,4 @@ set GO111MODULE=on set GOARCH=386 rsrc -manifest main.manifest -ico main.ico -o main.syso go generate -go build -ldflags="-s -w -H=windowsgui" -o м.exe +go build -ldflags="-s -w -H=windowsgui" -o main.exe diff --git a/golang_learn/data_func/go_to_excel/demo_he_bing/main.go b/golang_learn/data_func/go_to_excel/demo_he_bing/main.go index 896135c..9802a12 100644 --- a/golang_learn/data_func/go_to_excel/demo_he_bing/main.go +++ b/golang_learn/data_func/go_to_excel/demo_he_bing/main.go @@ -1,23 +1,34 @@ package main import ( - "flag" + "bufio" "fmt" "github.com/360EntSecGroup-Skylar/excelize" + "io" + "os" "strings" ) // 定义命令行参数对应的变量,这三个变量都是指针类型 -var prefix = flag.String("str", "保留sheet前缀", "表格特定前缀") -var excelName = flag.String("f", "excel文件名称", "excel文件名称,需要保证同属一个文件夹下") +var mapObj map[string]string func main() { - // 把用户传递的命令行参数解析为对应变量的值 - flag.Parse() - fmt.Printf("文件:%s, 前缀:%s\n", *excelName, *prefix) + mapObj = make(map[string]string) + //查询配置文件 + sqlObj, err := os.Open("./配置文件.ini") + //为空表示配置文件不存在生成配置文件 + if err != nil { + sqlObj, err = os.Create("./配置文件.ini") + _, _ = sqlObj.WriteString("文件名(带格式):\n") + _, _ = sqlObj.WriteString("保留sheet前缀:") + _ = sqlObj.Close() + return + } + //通过配置文件信息,读取数据库连接信息 + _ = ReadLine(sqlObj, processLine) - f, err := excelize.OpenFile(*excelName) + f, err := excelize.OpenFile(mapObj["fileName"]) if err != nil { fmt.Println(err) return @@ -25,7 +36,7 @@ func main() { sheet := f.GetSheetMap() for _, s := range sheet { - if !strings.HasPrefix(s, *prefix) { + if !strings.HasPrefix(s, mapObj["prefix"]) { // Delete the second sheet. f.DeleteSheet(s) fmt.Println("删除sheet名称:", s) @@ -38,3 +49,32 @@ func main() { return } } + +//处理产生的字符串 +func processLine(line []byte) { + proStr := string(line) + if strings.HasPrefix(proStr, "文件名(带格式):") { + mapObj["fileName"] = strings.Replace(proStr[25:], "\n", "", -1) + fmt.Println("文件名:", mapObj["fileName"]) + } + if strings.HasPrefix(proStr, "保留sheet前缀:") { + mapObj["prefix"] = strings.Replace(proStr[18:], "\n", "", -1) + fmt.Println("保留sheet前缀:", mapObj["prefix"]) + } +} + +//逐行读取文件 +func ReadLine(f *os.File, hookfn func([]byte)) error { + bfRd := bufio.NewReader(f) + for { + line, err := bfRd.ReadBytes('\n') + hookfn(line) //放在错误处理前面,即使发生错误,也会处理已经读取到的数据。 + if err != nil { //遇到任何错误立即返回,并忽略 EOF 错误信息 + if err == io.EOF { + return nil + } + return err + } + } + return nil +}