日常开发中会遇到处理Excel文件的相关操作,这里推荐一款应用比较广泛的操作Excel的开源工具Excelize。
Excelize是一个用Go语言编写的库,提供了一组允许您写入和读取XLSX / XLSM / XLTM文件的功能。支持读写由Microsoft Excel™2007和更高版本生成的电子表格文档。通过高度兼容性支持复杂的组件,并提供了流式API,用于从工作表中生成或读取包含大量数据的数据。该库需要Go版本1.10或更高版本。可以使用go的内置文档工具查看完整的API文档,也可以在go.dev和docs reference上在线查看。
创建Excel文件
示例
1package main 2 3import ( 4 "fmt" 5 "github.com/360EntSecGroup-Skylar/excelize" 6) 7 8func main() { 9 f := excelize.NewFile() 10 // Create a new sheet. 11 index := f.NewSheet("Sheet2") 12 // Set value of a cell. 13 f.SetCellValue("Sheet2", "A2", "Hello world.") 14 //设置单元格样式 15 style, err := f.NewStyle(`{ 16 "font": 17 { 18 "bold": true, 19 "family": "font-family", 20 "size": 20, 21 "color": "#777777" 22 } 23}`) 24 if err != nil { 25 fmt.Println(err) 26 } 27 f.SetCellStyle("Sheet1", "B1", "B1", style) 28 f.SetCellValue("Sheet1", "B1", "hello") 29 30 // Set active sheet of the workbook. 31 f.SetActiveSheet(index) 32 // Save xlsx file by the given path. 33 if err := f.SaveAs("Book1.xlsx"); err != nil { 34 fmt.Println(err) 35 } 36}
插入图片到单元格
示例:
1package main 2 3import ( 4 "fmt" 5 _ "image/gif" 6 _ "image/jpeg" 7 _ "image/png" 8 9 "github.com/360EntSecGroup-Skylar/excelize" 10) 11 12func main() { 13 f, err := excelize.OpenFile("Book1.xlsx") 14 if err != nil { 15 fmt.Println(err) 16 return 17 } 18 // Insert a picture. 19 if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil { 20 fmt.Println(err) 21 } 22 // Insert a picture to worksheet with scaling. 23 if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil { 24 fmt.Println(err) 25 } 26 // Insert a picture offset in the cell with printing support. 27 if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil { 28 fmt.Println(err) 29 } 30 // Save the xlsx file with the origin path. 31 if err = f.Save(); err != nil { 32 fmt.Println(err) 33 } 34}
读取Excel文件
示例
1package main 2 3import ( 4 "fmt" 5 6 "github.com/360EntSecGroup-Skylar/excelize" 7) 8 9func main() { 10 f, err := excelize.OpenFile("Book1.xlsx") 11 if err != nil { 12 fmt.Println(err) 13 return 14 } 15 // Get value from cell by given worksheet name and axis. 16 cell, err := f.GetCellValue("Sheet1", "B2") 17 if err != nil { 18 fmt.Println(err) 19 return 20 } 21 fmt.Println(cell) 22 // Get all the rows in the Sheet1. 23 rows, err := f.GetRows("Sheet1") 24 for _, row := range rows { 25 for _, colCell := range row { 26 fmt.Print(colCell, "\t") 27 } 28 fmt.Println() 29 } 30}
生成Excel文件并下载
示例
1package main 2 3import ( 4 "github.com/360EntSecGroup-Skylar/excelize" 5 "log" 6 "net/http" 7) 8 9func down(w http.ResponseWriter, r *http.Request) { 10 f := excelize.NewFile() 11 // Set value of a cell. 12 f.SetCellValue("Sheet1", "A2", "Hello world.") 13 // Save xlsx file by the given path. 14 //if err := f.SaveAs("Book1.xlsx"); err != nil { 15 // fmt.Println(err) 16 //} 17 18 w.Header().Set("Content-Type", "application/octet-stream") 19 w.Header().Set("Content-Disposition", "attachment; filename="+"100以内口算题.xlsx") 20 w.Header().Set("Content-Transfer-Encoding", "binary") 21 _ = f.Write(w) 22} 23 24func main() { 25 http.HandleFunc("/", down) // 设置访问路由 26 log.Fatal(http.ListenAndServe(":8080", nil))