tealeg / xlsx

Go library for reading and writing XLSX files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v3: got empty string in some cells with content

sunxia0 opened this issue · comments

I got a strange xlsx file from one of my clients. Content of some cells in this file cannot be accessed correctly. Instead, empty strings were returned.

However, v1 works corretly on this file. You can remove comments to see the difference.

Here's my testing code.

package main

import (
	"fmt"
	"github.com/tealeg/xlsx"
	xlsx_v3 "github.com/tealeg/xlsx/v3"
)

func GetRowCell(m map[string]int, row *xlsx.Row, index string) string {
	val, ok := m[index]
	if !ok {
		return ""
	}
	return row.Cells[val].Value
}

func GetRowCellV3(m map[string]int, row *xlsx_v3.Row, index string) string {
	val, ok := m[index]
	if !ok {
		return ""
	}
	return row.GetCell(val).Value
}

func main() {
	//file, err := xlsx.OpenFile("/path/to/issue.xlsx")
	file, err := xlsx_v3.OpenFile("/path/to/issue.xlsx")

	if err != nil {
		fmt.Println(err)
		return
	}

	sheet := file.Sheets[0]
	maxRow := sheet.MaxRow
	nameCol := map[string]int{"品牌名称": 8, "备案品名": 14}

	for i := 0; i < maxRow; i++ {
		//row := sheet.Row(i)
		//brandName := GetRowCell(nameCol, row, "品牌名称")
		//recordName := GetRowCell(nameCol, row, "备案品名")

		row, _ := sheet.Row(i)
		brandName := GetRowCellV3(nameCol, row, "品牌名称")
		recordName := GetRowCellV3(nameCol, row, "备案品名")

		fmt.Printf("BrandName:%v RecordName:%v\n", brandName, recordName)
	}
}

module version

  • V1: v1.0.5
  • V3: v3.2.3

issue file
issue.xlsx

Stale issue message