

Golang 操作Excel文件
source link: https://studygolang.com/articles/31272
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

Recommend
-
32
首先,file 类是在 os 包中的,封装了底层的文件描述符和相关信息,同时封装了 Read 和 Write 的实现。 FileInfo 接口 FileInfo 接口中定义了 File 信息相关的方法。 type FileInfo interface { Name(...
-
23
关键术语介绍 为了方便开源库的快速上手,我们先来了解 excel 中的几个关键术语,如下图所示,①为sheet,也就是表格中的页签;②为row,代表 excel 中的一行;③为cell,代表 excel 中的一个单元格。
-
13
Os模块的使用与源码研究 文件:计算机中的文件是存储在外部介质(通常是磁盘)上的数据集合,文件分为文本文件和二进制文件。例如咱们常见的文件后缀名 .exe , .txt ,'.word'...等等 文件...
-
7
Golang简单写文件操作的四种方法 bluezwt · 2014-12-18 18:00:01 · 113262 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览
-
11
最近业务上有数据大屏的需求,要求不仅能展示数据,同时能提供所选日期范围的数据下载。本文纯记录实现方案作为笔记,实现细节十分不完备。xlsx电子表格的标准规范详见:
-
7
golang 的文件锁操作 2019-11-10 785 words 2 min read | 阅读 809...
-
12
使用golang操作文件和目录 评分: 4.5 作者: Ryan Lu 类别: golang...
-
3
使用 Golang 创建和读取 Excel 文件 2022年07月07日 10:39 · 阅读 951
-
6
前端使用的vue-element-admin框架,后端使用ABP框架,Excel导出使用的Magicodes.IE.Excel.Abp库。Excel导入和导出操作几乎一样,不再介绍。文本主要介绍Excel导出操作和过程中遇到的坑,主要是Excel文件导出后无法打开的问题。 一.Ma...
-
7
pandas 操作 excel 备忘 2022-11-12 tech
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK