10.4 Golang 操作Excel文件
日常开发中会遇到处理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/360EntSecGroup-Skylar/excelize
https://xuri.me/excelize/zh-hans/
links
- 目录
- 上一节:
- 下一节: