亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

最近工作忙于作圖,圖表,經常和Excel打交道,這不要讀寫excel文件。以前都是用Python,現在學習Go語言,剛好試試。

要操作excel,自然需要找讀寫Excel的Package,前人栽好樹,等我去乘涼。

去哪里找合適的Package呢?

Go語言的包在 https://pkg.go.dev/。打開就能搜索。這里錄入關鍵字xlsx(如果需要讀寫xls則錄入xls也可以)。

「GoLang編程」Go操作Excel表格

通過關鍵字找到需要的包

這里我們使用 github.com/tealeg/xlsx/v3

「GoLang編程」Go操作Excel表格

xlsx 的v3版本

先點擊Doc,看下文檔。

「GoLang編程」Go操作Excel表格

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表,第二維是行,第三維是列。非常簡單方便。

其實我比較討厭這種包裝,因為打開文件之后,何時關閉不受我控制。

「GoLang編程」Go操作Excel表格

這個錯誤就是表格文件打開后沒有關閉,再次打開導致異常

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文件表格內容?

舉個例子:

 

「GoLang編程」Go操作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)
}
「GoLang編程」Go操作Excel表格

 

運行結果:

「GoLang編程」Go操作Excel表格

結果正確

如果使用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)
}

運行結果:

「GoLang編程」Go操作Excel表格

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)

運行如下:

「GoLang編程」Go操作Excel表格

列索引3 即D,行索引2即第 3行

實際工作中,應該還需要整行寫。就需要單獨設計一個rowWrite函數,內部實現還是拆成一個個cell去寫。

總結,

Go寫Excel感覺上比Python還簡單。坑少。

分享到:
標簽:表格 語言 Excel
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定