在Go语言和Excelize中循环遍历列单元格。

huangapple go评论120阅读模式
英文:

Loop through column cells in golang and excelize

问题

我很惊讶如何在Golang中循环遍历Excel表格的列单元格,这是我的Excel文件:

在Go语言和Excelize中循环遍历列单元格。

我尝试了下面这段代码,但是出于其他原因:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile("pricematching.xlsx")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. // 获取sheet1部分的所有行。
  13. rows, err := f.GetCellValue("sheet1", "A2")
  14. fmt.Print(rows, "\t")
  15. }

请注意,我只提供了代码的翻译部分,不包括其他内容。

英文:

I was wondered how to loop over column cells of excel sheet in golang, here is my excel file:

在Go语言和Excelize中循环遍历列单元格。

I have tried this piece of code for other reason

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile("pricematching.xlsx")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. // Get all the rows in the sheet1 section.
  13. rows, err := f.GetCellValue("sheet1", "A2")
  14. fmt.Print(rows, "\t")
  15. }

答案1

得分: 3

无论你的 Excel 文件如何,这是读取每个单元格的方法:

  1. xlsxFile, error := excelize.OpenFile(filePath)
  2. for _, sheetName := range xlsxFile.GetSheetMap() {
  3. for rowIndex, rowValues := range xlsxFile.GetRows(sheetName) {
  4. for columnIndex, columnValue := range rowValues {
  5. // 在这里进行你想要的操作
  6. }
  7. }
  8. }
英文:

No matter how's your excel file, this is the way to read each cell:

  1. xlsxFile, error := excelize.OpenFile(filePath)
  2. for _, sheetName := range xlsxFile.GetSheetMap() {
  3. for rowIndex, rowValues := range xlsxFile.GetRows(sheetName) {
  4. for columnIndex, columnValue := range rowValues {
  5. // do what ever you want here
  6. }
  7. }
  8. }

答案2

得分: 0

不确定你具体需要什么,但这是一种获取某一列中所有单元格的简单方法(如果你知道要读取的行数):

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile("pricematching.xlsx")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. columnName := "A"
  13. sheetName := "sheet1"
  14. totalNumberOfRows := 20
  15. for i := 1; i < totalNumberOfRows; i++ {
  16. cellName := fmt.Sprintf("%s%d", columnName, i)
  17. // fmt.Println(cellName)
  18. cellValue, err := f.GetCellValue(sheetName, cellName)
  19. fmt.Printf("%s\t", cellValue)
  20. }
  21. }
英文:

Not sure what exactly you need, but this is a simple way to get all cells in a column (if you know how many rows you want to read):

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;github.com/xuri/excelize/v2&quot;
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile(&quot;pricematching.xlsx&quot;)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. columnName := &quot;A&quot;
  13. sheetName := &quot;sheet1&quot;
  14. totalNumberOfRows := 20
  15. for i := 1; i &lt; totalNumberOfRows; i++ {
  16. cellName := fmt.Sprintf(&quot;%s%d&quot;, columnName, i)
  17. // fmt.Println(cellName)
  18. cellValue, err := f.GetCellValue(sheetName, cellName)
  19. fmt.Printf(&quot;%s\t&quot;, cellValue)
  20. }
  21. }

huangapple
  • 本文由 发表于 2022年6月18日 18:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/72668673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定