推薦一款Golang操作Excel的開(kāi)源工具Excelize
日常開(kāi)發(fā)中會(huì)遇到處理Excel文件的相關(guān)操作,這里推薦一款應(yīng)用比較廣泛的操作Excel的開(kāi)源工具Excelize。
Excelize是一個(gè)用Go語(yǔ)言編寫(xiě)的庫(kù),提供了一組允許您寫(xiě)入和讀取XLSX / XLSM / XLTM文件的功能。支持讀寫(xiě)由Microsoft Excel?2007和更高版本生成的電子表格文檔。通過(guò)高度兼容性支持復(fù)雜的組件,并提供了流式API,用于從工作表中生成或讀取包含大量數(shù)據(jù)的數(shù)據(jù)。該庫(kù)需要Go版本1.10或更高版本。可以使用go的內(nèi)置文檔工具查看完整的API文檔,也可以在go.dev和docs reference上在線查看。

創(chuàng)建Excel文件
示例
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
)
func main() {
//NewFile提供了一個(gè)通過(guò)默認(rèn)模板創(chuàng)建新文件的功能
f := excelize.NewFile()
// 創(chuàng)建新圖紙Sheet2
index := f.NewSheet("Sheet2")
// 設(shè)置單元格的值.
f.SetCellValue("Sheet2", "A2", "Hello world.") //在Sheet2 A2
//設(shè)置單元格樣式
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") // 設(shè)置單元格的值.
// 設(shè)置工作簿的活動(dòng)工作表
f.SetActiveSheet(index)
// 按給定路徑保存xlsx文件.
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() {
//打開(kāi)文件
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
fmt.Println(err)
return
}
// 插入圖片
if err := f.AddPicture("Sheet1", "A3", "004.jpeg", ""); err != nil {
fmt.Println(err)
}
// 將圖片按比例插入到工作表中
if err := f.AddPicture("Sheet1", "D3", "1.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
fmt.Println(err)
}
// 在支持打印的單元格中插入圖片偏移
if err := f.AddPicture("Sheet1", "H3", "2.jpeg", `{"x_scale": 0.1, "y_scale": 0.1,"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() {
//打開(kāi)文件
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
fmt.Println(err)
return
}
// 按給定的工作表名稱和軸從單元格中獲取值.(只能獲取到文本信息,無(wú)法獲取圖片信息)
cell := f.GetCellValue("Sheet1", "A3")
if cell == "" {
fmt.Println("獲取內(nèi)容為空")
}else {
fmt.Println("指定表格中內(nèi)容為:", cell)
}
// 獲取圖紙1中的所有行
rows := f.GetRows("Sheet1")
for _, row := range rows {
for _, colCell := range row {
fmt.Print(colCell, "=")
}
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以內(nèi)口算題.xlsx")
w.Header().Set("Content-Transfer-Encoding", "binary")
_ = f.Write(w)
}
func main() {
http.HandleFunc("/", down) // 設(shè)置訪問(wèn)路由
log.Fatal(http.ListenAndServe(":8080", nil))
}
相關(guān)資料
https://github.com/360EntSecGroup-Skylar/excelize
https://xuri.me/exce lize/zh-hans/
推薦閱讀 點(diǎn)擊標(biāo)題可跳轉(zhuǎn)1、Golang圖片處理:學(xué)習(xí)如何添加水印和文字
評(píng)論
圖片
表情
