在我的项目中,我使用
qax-os/excelize
库版本v2.7.1
来读取Go语言中的excel文件。但是当我在带有 32Mi
ram 的机器上运行我的应用程序时,出现内存不足错误。
这是我的代码示例:
func main() {
f, err := excelize.OpenFile("electricinfo.xlsx")
if err != nil {
fmt.Println(err)
return
}
defer func() {
// Close the spreadsheet.
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Get all the rows in the sheet mydistrict.
rows, err := f.GetRows("mydistrict")
if err != nil {
fmt.Println(err)
return
}
for _, row := range rows {
for _, colCell := range row {
// ...Business logic
}
fmt.Println()
}
}
如何逐行读取 Excel 行,类似
func main() {
rowNumber := 1
for {
// Loop through each row in the csv
record, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
// ... Block of code
continue
}
}
}
您可以尝试使用 xlsxreader 包,这是一个低内存高性能库,用于从 xlsx 文件中读取数据以进行流数据,而不是加载到 RAM 中。或者,您可以使用 xlsx-tables。