Golang操作excel的方法
關(guān)鍵術(shù)語介紹
為了方便開源庫的快速上手,我們先來了解 excel 中的幾個關(guān)鍵術(shù)語,如下圖所示,①為sheet,也就是表格中的頁簽;②為row,代表 excel 中的一行;③為cell,代表 excel 中的一個單元格。
正常情況下,創(chuàng)建一個表格的基本流程是打開 wps 點擊新建,這時會默認創(chuàng)建一個 sheet,然后在該 sheet 中的第一行填寫表頭,接下來根據(jù)表頭逐行填充內(nèi)容,最后將文件另存為到硬盤的某個位置。這與 Golang 開源庫創(chuàng)建 excel 的流程基本相同,下面演示一個極簡表格的創(chuàng)建。
創(chuàng)建表格
創(chuàng)建表格前需要先引入 excel 庫,我們以比較熱門的 tealeg/xlsx 庫為例。
go get github.com/tealeg/xlsx
首先創(chuàng)建一個空文件,拿到文件句柄。
file := xlsx.NewFile()
創(chuàng)建一個名為人員信息收集
的 sheet。
sheet, err := file.AddSheet("人員信息收集") if err != nil { panic(err.Error()) }
然后為該 sheet 創(chuàng)建一行,這行作為我們的表頭。
row := sheet.AddRow()
在該行中創(chuàng)建一個單元格。
cell := row.AddCell()
現(xiàn)在給單元格填充內(nèi)容,因為是表頭,暫且叫姓名
。
cell.Value = "姓名"
如何創(chuàng)建第二個單元格呢?原理相同,此處 cell 變量已定義,再創(chuàng)建新單元格只需賦值即可。
cell = row.AddCell() cell.Value = "性別"
表頭已經(jīng)設(shè)置好了,可以開始創(chuàng)建第二行來填充內(nèi)容了,方式與上述無差別。
row = sheet.AddRow() cell = row.AddCell() cell.Value = "張三" cell = row.AddCell() cell.Value = "男"
表格設(shè)置完成后,將該文件保存,文件名可自定義。
err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) }
跑起來后,可以發(fā)現(xiàn)目錄中多了一個 demo.xlsx 文件,打開預(yù)覽內(nèi)容如下,達到了預(yù)期效果。
文件源碼
package main import "github.com/tealeg/xlsx" func main() { file := xlsx.NewFile() sheet, err := file.AddSheet("人員信息收集") if err != nil { panic(err.Error()) } row := sheet.AddRow() cell := row.AddCell() cell.Value = "姓名" cell = row.AddCell() cell.Value = "性別" row = sheet.AddRow() cell = row.AddCell() cell.Value = "張三" cell = row.AddCell() cell.Value = "男" err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) } }
讀取表格
表格的讀取比創(chuàng)建簡單很多,依然以上文創(chuàng)建的文件為例。
output, err := xlsx.FileToSlice("demo.xlsx") if err != nil { panic(err.Error()) }
只需將文件路徑傳入上述方法,即可自動讀取并返回一個三維切片,我們來讀取第一個 sheet 的第二行中的第一個單元格。
log.Println(output[0][1][1]) //Output: 男
由此一來就非常容易遍歷了。
for rowIndex, row := range output[0] { for cellIndex, cell := range row { log.Println(fmt.Sprintf("第%d行,第%d個單元格:%s", rowIndex+1, cellIndex+1, cell)) } }
2020/10/11 16:15:29 第1行,第1個單元格:姓名
2020/10/11 16:15:29 第1行,第2個單元格:性別
2020/10/11 16:15:29 第2行,第1個單元格:張三
2020/10/11 16:15:29 第2行,第2個單元格:男
文件源碼
package main import ( "fmt" "github.com/tealeg/xlsx" "log" ) func main() { output, err := xlsx.FileToSlice("demo.xlsx") if err != nil { panic(err.Error()) } log.Println(output[0][1][1]) for rowIndex, row := range output[0] { for cellIndex, cell := range row { log.Println(fmt.Sprintf("第%d行,第%d個單元格:%s", rowIndex+1, cellIndex+1, cell)) } } }
修改表格
只是讀取表格內(nèi)容可能在特定場景下無法滿足需求,有時候需要對表格內(nèi)容進行更改。
file, err := xlsx.OpenFile("demo.xlsx") if err != nil { panic(err.Error()) }
修改表格之前依然需要先讀取文件,只是這次并沒有直接將其轉(zhuǎn)化為三維切片。拿到文件句柄后,可以直接修改某一行的內(nèi)容。
file.Sheets[0].Rows[1].Cells[0].Value = "李四"
上述代碼將第二行的張三改為了李四,但這還沒有結(jié)束,接下來需要將文件重新保存。
err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) }
打開文件預(yù)覽,可以看到已經(jīng)成功將張三改為了李四。
文件源碼
package main import "github.com/tealeg/xlsx" func main() { file, err := xlsx.OpenFile("demo.xlsx") if err != nil { panic(err.Error()) } file.Sheets[0].Rows[1].Cells[0].Value = "李四" err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) } }
樣式設(shè)置
該開源庫不僅支持內(nèi)容的編輯,還支持表格的樣式設(shè)置,樣式統(tǒng)一由結(jié)構(gòu)體 Style 來負責。
type Style struct { Border Border Fill Fill Font Font ApplyBorder bool ApplyFill bool ApplyFont bool ApplyAlignment bool Alignment Alignment NamedStyleIndex *int }
拿上述生成的文件為例,假如我要將姓名所在單元格居中,首先要實例化樣式對象。
style := xlsx.NewStyle()
賦值居中屬性。
style.Alignment = xlsx.Alignment{ Horizontal: "center", Vertical: "center", }
給第一行第一個單元格設(shè)置樣式。
file.Sheets[0].Rows[0].Cells[0].SetStyle(style)
與修改表格處理邏輯相同,最后保存文件。
err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) }
打開預(yù)覽,可以看到文字已經(jīng)上下左右居中。
同理,可以修改文字顏色和背景,同樣通過 style 的屬性來設(shè)置。
style.Font.Color = xlsx.RGB_Dark_Red style.Fill.BgColor = xlsx.RGB_Dark_Green
其他還有很多屬性可以設(shè)置,比如合并單元格、字體、大小等等,大家可以自行測試。
文件源碼
package main import "github.com/tealeg/xlsx" func main() { file, err := xlsx.OpenFile("demo.xlsx") if err != nil { panic(err.Error()) } style := xlsx.NewStyle() style.Font.Color = xlsx.RGB_Dark_Red style.Fill.BgColor = xlsx.RGB_Dark_Green style.Alignment = xlsx.Alignment{ Horizontal: "center", Vertical: "center", } file.Sheets[0].Rows[0].Cells[0].SetStyle(style) err = file.Save("demo.xlsx") if err != nil { panic(err.Error()) } }
我是平也,這有一個專注Gopher技術(shù)成長的開源項目「go home」
到此這篇關(guān)于Golang操作excel的方法的文章就介紹到這了,更多相關(guān)Golang操作excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go slice 數(shù)組和切片使用區(qū)別示例解析
這篇文章主要為大家介紹了go slice 數(shù)組和切片使用區(qū)別示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Golang中g(shù)oroutine和channel使用介紹深入分析
一次只做一件事情并不是完成任務(wù)最快的方法,一些大的任務(wù)可以拆解成若干個小任務(wù),goroutine可以讓程序同時處理幾個不同的任務(wù),goroutine使用channel來協(xié)調(diào)它們的工作,channel允許goroutine互相發(fā)送數(shù)據(jù)并同步,這樣一個goroutine就不會領(lǐng)先于另一個goroutine2023-01-01CMD下執(zhí)行Go出現(xiàn)中文亂碼的解決方法
需要在Go寫的服務(wù)里面調(diào)用命令行或者批處理,并根據(jù)返回的結(jié)果做處理。但是windows下面用cmd返回中文會出現(xiàn)亂碼,本文就詳細的介紹一下解決方法,感興趣的可以了解一下2021-12-12