最近工作忙于作圖,圖表,經常和Excel打交道,這不要讀寫excel文件。以前都是用Python,現在學習Go語言,剛好試試。
要操作excel,自然需要找讀寫Excel的Package,前人栽好樹,等我去乘涼。
去哪里找合適的Package呢?
Go語言的包在 https://pkg.go.dev/。打開就能搜索。這里錄入關鍵字xlsx(如果需要讀寫xls則錄入xls也可以)。

通過關鍵字找到需要的包
這里我們使用 github.com/tealeg/xlsx/v3

xlsx 的v3版本
先點擊Doc,看下文檔。

Document 文檔全面解釋了包
func FileToSlice ¶
func FileToSlice(path string, options ...FileOption) ([][][]string, error)
A convenient wrApper around File.ToSlice, FileToSlice will return the raw data contained in an Excel XLSX file as three dimensional slice. The first index represents the sheet number, the second the row number, and the third the cell number.
這個函數可以直接打開一個表格文件,讀取到一個3維切片中,第一維是sheet表,第二維是行,第三維是列。非常簡單方便。
其實我比較討厭這種包裝,因為打開文件之后,何時關閉不受我控制。

這個錯誤就是表格文件打開后沒有關閉,再次打開導致異常
For example:
var mySlice [][][]string
var value string
mySlice = xlsx.FileToSlice("myXLSX.xlsx")
value = mySlice[0][0][0] //這就是 A1 了,簡單吧
value = mySlice[0][0][1] //這就是 A1 了,簡單吧
Here, value would be set to the raw value of the cell A1 in the first sheet in the XLSX file.
如果代碼中需要遍歷每一個sheet表格,需要range 循環
sheetsn := len(mySlice) //sheet 個數
rows :=len(mySlice[0]) //行數
cols :=len(mySlice[0][0]) //列數
怎么讀excel文件表格內容?
舉個例子:

準備讀的文件
func xlxsreadtotable(pathname string){
var mySlice [][][]string
var err error
mySlice ,err= xlsx.FileToSliceUnmerged(pathname)
value := mySlice[0]
sheetsn := len(mySlice) //sheet 個數
rows :=len(mySlice[0]) //行數
cols :=len(mySlice[0][0]) //列數
fmt.Println( value,err)
fmt.Println( sheetsn,cols,rows)
// 表 行 列
A1 := mySlice[0][0][0] //這就是 A1
B1 := mySlice[0][0][1] //這就是 B1
A2 := mySlice[0][1][0] //A2
B2 := mySlice[0][1][1] //B2
C1 := mySlice[0][0][2] //C1
fmt.Println("A1:",A1,"A2:",A2,"B1:",B1,"B2:",B2,"C1:",C1)
}

運行結果:

結果正確
如果使用OpenFile則代碼會是這樣:
func readxlsx(pathname string) {
wb, err := xlsx.OpenFile(pathname)
if err != nil {panic(err)}
// wb 引用 workbook
fmt.Println("Sheets in this file:")
for i, sh := range wb.Sheets {
fmt.Println(i, sh.Name)
}
fmt.Println("----")
}
那怎么改寫一個excel的內容呢?
func editexlsx(pathname string) {
//改寫一下某cell
wb, _ := xlsx.OpenFile(pathname)
cell ,_:= wb.Sheet["Sheet1"].Cell(3,3)
cell.Value ="改寫D4" wb.Save(pathname)
}
運行結果:

D4格子被改寫成功
看文檔還支持各種,表格格式,字體大小,文字顏色。
func SetDefaultFont ¶ 設置字體和字體大小
func SetDefaultFont(size float64, name string)
func SkipEmptyCells ¶ 在Row.ForEachCell循環中跳過空cell
func SkipEmptyCells(flags *cellVisitorFlags)
SkipEmptyCells can be passed as an option to Row.ForEachCell in order to make it skip over empty cells in the sheet.
func SkipEmptyRows ¶ 同理也可跳過空行
func SkipEmptyRows(flags *rowVisitorFlags)
SkipEmptyRows can be passed to the Sheet.ForEachRow function to cause it to skip over empty Rows.
讀寫表格時,從表格得到時間需要用 TimeFromExcelTime()轉換。
寫表格時,時間需要轉換成excel時間 TimeToExcelTime()。
如果表格超大,記得調用一下函數 UseDiskVCellStore(f *File)
如果表格較小,還可以全部放內存來加快表格處理,函數 UseMemoryCellStore(f *File)。
整數行列,可以直接轉換到字符串,如下:
y := xlsx.RowIndexToString(2)
x :=xlsx.ColIndexToLetters(3)
fmt.Println("x3:",x," y2:",y)
運行如下:

列索引3 即D,行索引2即第 3行
實際工作中,應該還需要整行寫。就需要單獨設計一個rowWrite函數,內部實現還是拆成一個個cell去寫。
總結,
Go寫Excel感覺上比Python還簡單。坑少。