|
@@ -0,0 +1,88 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ var source = "data/190703/初中数学2019063被替换文件.xlsx"
|
|
|
+ var newSource = "data/190703/初中数学2019063被替换文件_new.xlsx"
|
|
|
+ var template = "data/190703/初中数学清单-统一标准.xlsx"
|
|
|
+
|
|
|
+ // 打开文件
|
|
|
+ sourceF, err := xlsx.OpenFile(source)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ templateF, err := xlsx.OpenFile(template)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ recoder := generateTemplate(templateF)
|
|
|
+
|
|
|
+ err = replace(sourceF, newSource, recoder)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func generateTemplate(templateF *xlsx.File) map[string]string {
|
|
|
+ var recoder = map[string]string{}
|
|
|
+ sheet := templateF.Sheets[0]
|
|
|
+ for rowIndex, row := range sheet.Rows {
|
|
|
+ if rowIndex == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cells := row.Cells
|
|
|
+ if len(cells) < 2 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ cell_0 := strings.TrimSpace(cells[0].Value)
|
|
|
+ cell_1 := strings.TrimSpace(cells[1].Value)
|
|
|
+ fmt.Println("---------------", cell_0, cell_1)
|
|
|
+
|
|
|
+ if cell_0 == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ recoder[cell_0] = cell_1
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(recoder)
|
|
|
+ return recoder
|
|
|
+}
|
|
|
+
|
|
|
+func replace(sourceF *xlsx.File, name string, recoder map[string]string) error {
|
|
|
+ sheet := sourceF.Sheets[0]
|
|
|
+ for rowIndex, row := range sheet.Rows {
|
|
|
+ if rowIndex == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cells := row.Cells
|
|
|
+ if len(cells) < 10 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ cell_9 := strings.TrimSpace(cells[9].Value)
|
|
|
+ cell_9_values := strings.Split(cell_9, ";")
|
|
|
+ cell_9_new := ""
|
|
|
+ for _, value := range cell_9_values {
|
|
|
+ v, ok := recoder[value]
|
|
|
+ if cell_9_new != "" {
|
|
|
+ cell_9_new += ";"
|
|
|
+ }
|
|
|
+ if ok {
|
|
|
+ cell_9_new = cell_9_new + v
|
|
|
+ } else {
|
|
|
+ cell_9_new = cell_9_new + value
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //fmt.Println(cell_9_new)
|
|
|
+ cells[9].Value = cell_9_new
|
|
|
+ }
|
|
|
+ return sourceF.Save(name)
|
|
|
+}
|