10.4 Golang 操作Excel文件

日常开发中会遇到处理Excel文件的相关操作,这里推荐一款应用比较广泛的操作Excel的开源工具Excelize。

Excelize是一个用Go语言编写的库,提供了一组允许您写入和读取XLSX / XLSM / XLTM文件的功能。支持读写由Microsoft Excel™2007和更高版本生成的电子表格文档。通过高度兼容性支持复杂的组件,并提供了流式API,用于从工作表中生成或读取包含大量数据的数据。该库需要Go版本1.10或更高版本。可以使用go的内置文档工具查看完整的API文档,也可以在go.devdocs reference上在线查看

创建Excel文件

示例

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/360EntSecGroup-Skylar/excelize"
  5. )
  6. func main() {
  7. f := excelize.NewFile()
  8. // Create a new sheet.
  9. index := f.NewSheet("Sheet2")
  10. // Set value of a cell.
  11. f.SetCellValue("Sheet2", "A2", "Hello world.")
  12. //设置单元格样式
  13. style, err := f.NewStyle(`{
  14. "font":
  15. {
  16. "bold": true,
  17. "family": "font-family",
  18. "size": 20,
  19. "color": "#777777"
  20. }
  21. }`)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. f.SetCellStyle("Sheet1", "B1", "B1", style)
  26. f.SetCellValue("Sheet1", "B1", "hello")
  27. // Set active sheet of the workbook.
  28. f.SetActiveSheet(index)
  29. // Save xlsx file by the given path.
  30. if err := f.SaveAs("Book1.xlsx"); err != nil {
  31. fmt.Println(err)
  32. }
  33. }

插入图片到单元格

示例:

  1. package main
  2. import (
  3. "fmt"
  4. _ "image/gif"
  5. _ "image/jpeg"
  6. _ "image/png"
  7. "github.com/360EntSecGroup-Skylar/excelize"
  8. )
  9. func main() {
  10. f, err := excelize.OpenFile("Book1.xlsx")
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. // Insert a picture.
  16. if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
  17. fmt.Println(err)
  18. }
  19. // Insert a picture to worksheet with scaling.
  20. if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
  21. fmt.Println(err)
  22. }
  23. // Insert a picture offset in the cell with printing support.
  24. 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 {
  25. fmt.Println(err)
  26. }
  27. // Save the xlsx file with the origin path.
  28. if err = f.Save(); err != nil {
  29. fmt.Println(err)
  30. }
  31. }

读取Excel文件

示例

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/360EntSecGroup-Skylar/excelize"
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile("Book1.xlsx")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. // Get value from cell by given worksheet name and axis.
  13. cell, err := f.GetCellValue("Sheet1", "B2")
  14. if err != nil {
  15. fmt.Println(err)
  16. return
  17. }
  18. fmt.Println(cell)
  19. // Get all the rows in the Sheet1.
  20. rows, err := f.GetRows("Sheet1")
  21. for _, row := range rows {
  22. for _, colCell := range row {
  23. fmt.Print(colCell, "\t")
  24. }
  25. fmt.Println()
  26. }
  27. }

生成Excel文件并下载

示例

  1. package main
  2. import (
  3. "github.com/360EntSecGroup-Skylar/excelize"
  4. "log"
  5. "net/http"
  6. )
  7. func down(w http.ResponseWriter, r *http.Request) {
  8. f := excelize.NewFile()
  9. // Set value of a cell.
  10. f.SetCellValue("Sheet1", "A2", "Hello world.")
  11. // Save xlsx file by the given path.
  12. //if err := f.SaveAs("Book1.xlsx"); err != nil {
  13. // fmt.Println(err)
  14. //}
  15. w.Header().Set("Content-Type", "application/octet-stream")
  16. w.Header().Set("Content-Disposition", "attachment; filename="+"100以内口算题.xlsx")
  17. w.Header().Set("Content-Transfer-Encoding", "binary")
  18. _ = f.Write(w)
  19. }
  20. func main() {
  21. http.HandleFunc("/", down) // 设置访问路由
  22. log.Fatal(http.ListenAndServe(":8080", nil))
  23. }

相关资料

https://github.com/360EntSecGroup-Skylar/excelize

https://xuri.me/excelize/zh-hans/

links

  • 目录
  • 上一节:
  • 下一节: