1、首先下载第三方excel读取库go get github.com/xuri/excelize/v2
2、读取xml库,未使用默认xml库 go get github.com/beevik/etree
package main import ( "fmt" "github.com/beevik/etree" "github.com/xuri/excelize/v2" ) func LoadExcelAndSaveXML() { f, err := excelize.OpenFile("Book1.xlsx") if err != nil { fmt.Println(err) return } defer func() { // Close the spreadsheet. if err := f.Close(); err != nil { fmt.Println(err) } }() // Get value from cell by given worksheet name and cell reference. cell, err := f.GetCellValue("Sheet1", "B2") if err != nil { fmt.Println(err) return } fmt.Println(cell) // Get all the rows in the Sheet1. rows, err := f.GetRows("Sheet1") if err != nil { fmt.Println(err) return } for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\t") } fmt.Println() } tempArray1 := [100]string{} doc := etree.NewDocument() doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) root := doc.CreateElement("Root") for i := 0; i < len(rows); i++ { tempArray := [100]string{} subElement := root.CreateElement("Property") for j := 0; j < len(rows[i]); j++ { if i == 0 { tempArray1[j] = rows[0][j] } if rows[0][j] == "" { continue } subElement.CreateAttr(rows[0][j], rows[i][j]) tempArray[0] = rows[i][0] tempArray[j+1] = rows[i][j] fmt.Printf("key:%s = %s \n", rows[0][j], rows[i][j]) } fmt.Println(tempArray) } doc.Indent(2) if err := doc.WriteToFile("Book1.xml"); err != nil { fmt.Println(err) } fmt.Println(tempArray1) } func main() { fmt.Println("hello,world") LoadExcelAndSaveXML() }