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

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

Loop through column cells in golang and excelize

问题

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

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

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

package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {

	f, err := excelize.OpenFile("pricematching.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	// 获取sheet1部分的所有行。
	rows, err := f.GetCellValue("sheet1", "A2")

	fmt.Print(rows, "\t")

}

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

英文:

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

package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {

	f, err := excelize.OpenFile("pricematching.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Get all the rows in the sheet1 section.
	rows, err := f.GetCellValue("sheet1", "A2")

	fmt.Print(rows, "\t")

}


答案1

得分: 3

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

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

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

xlsxFile, error := excelize.OpenFile(filePath)
for _, sheetName := range xlsxFile.GetSheetMap() {
   for rowIndex, rowValues := range xlsxFile.GetRows(sheetName) {
      for columnIndex, columnValue := range rowValues {
         // do what ever you want here
      }
   }
}

答案2

得分: 0

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

package main

import (
    "fmt"
    "github.com/xuri/excelize/v2"
)

func main() {
    f, err := excelize.OpenFile("pricematching.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }

    columnName := "A"
    sheetName := "sheet1"
    totalNumberOfRows := 20

    for i := 1; i < totalNumberOfRows; i++ {
        cellName := fmt.Sprintf("%s%d", columnName, i)
        // fmt.Println(cellName)
        cellValue, err := f.GetCellValue(sheetName, cellName)
        fmt.Printf("%s\t", cellValue)
    }
}
英文:

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):

package main

import (
    &quot;fmt&quot;
    &quot;github.com/xuri/excelize/v2&quot;
)

func main() {
    f, err := excelize.OpenFile(&quot;pricematching.xlsx&quot;)
    if err != nil {
        fmt.Println(err)
        return
    }

	columnName := &quot;A&quot;
	sheetName := &quot;sheet1&quot;
    totalNumberOfRows := 20

	for i := 1; i &lt; totalNumberOfRows; i++ {
		cellName := fmt.Sprintf(&quot;%s%d&quot;, columnName, i)
		// fmt.Println(cellName)
		cellValue, err := f.GetCellValue(sheetName, cellName)
		fmt.Printf(&quot;%s\t&quot;, cellValue)
	}
}

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:

确定