|
@ -1,23 +1,34 @@ |
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"flag" |
|
|
"bufio" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"github.com/360EntSecGroup-Skylar/excelize" |
|
|
"github.com/360EntSecGroup-Skylar/excelize" |
|
|
|
|
|
"io" |
|
|
|
|
|
"os" |
|
|
"strings" |
|
|
"strings" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// 定义命令行参数对应的变量,这三个变量都是指针类型
|
|
|
// 定义命令行参数对应的变量,这三个变量都是指针类型
|
|
|
var prefix = flag.String("str", "保留sheet前缀", "表格特定前缀") |
|
|
var mapObj map[string]string |
|
|
var excelName = flag.String("f", "excel文件名称", "excel文件名称,需要保证同属一个文件夹下") |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
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 { |
|
|
if err != nil { |
|
|
fmt.Println(err) |
|
|
fmt.Println(err) |
|
|
return |
|
|
return |
|
@ -25,7 +36,7 @@ func main() { |
|
|
|
|
|
|
|
|
sheet := f.GetSheetMap() |
|
|
sheet := f.GetSheetMap() |
|
|
for _, s := range sheet { |
|
|
for _, s := range sheet { |
|
|
if !strings.HasPrefix(s, *prefix) { |
|
|
if !strings.HasPrefix(s, mapObj["prefix"]) { |
|
|
// Delete the second sheet.
|
|
|
// Delete the second sheet.
|
|
|
f.DeleteSheet(s) |
|
|
f.DeleteSheet(s) |
|
|
fmt.Println("删除sheet名称:", s) |
|
|
fmt.Println("删除sheet名称:", s) |
|
@ -38,3 +49,32 @@ func main() { |
|
|
return |
|
|
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 |
|
|
|
|
|
} |
|
|