Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use *excelize.File.Close #1956

Closed
honlu opened this issue Jul 18, 2024 · 2 comments
Closed

How to use *excelize.File.Close #1956

honlu opened this issue Jul 18, 2024 · 2 comments

Comments

@honlu
Copy link

honlu commented Jul 18, 2024

Description
当excel文件内容较少时,比如只有几行数据,文件大小十几或几百KB,在下面函数GetRowIterator中是否file.Close,对main中行读取/打印结果没有影响。
但是当excel文件70MB时,上万行数据,GetRowIterator中是否file.Close会对main函数中数据读取/打印结果有着不一样的结果。

Steps to reproduce the issue:

  1. Run code
package main

import (
   "fmt"

   "github.com/xuri/excelize/v2"
)

func main() {
   path := `.\test\data\content.xlsx`
   rows, err := GetRowIterator(path)
   if err != nil {
   	fmt.Printf("GetRowIterator error:%v\n", err)
   	return
   }
   defer func() {
   	if err := rows.Close(); err != nil {
   		fmt.Printf("Close error:%v\n", err)
   	}
   }()
   i := 0
   for rows.Next() {
   	i++
   	if i > 5 {
   		break
   	}
   	row, err := rows.Columns()
   	if err != nil {
   		fmt.Printf("Columns error:%v\n", err)
   		return
   	}
   	fmt.Printf("row:%v\n", row)
   }

}

func GetRowIterator(targetFile string) (*excelize.Rows, error) {
   file, err := excelize.OpenFile(targetFile)
   if err != nil {
   	return nil, err
   }

   defer func() { // !!!---这里defer几行是否注释对读大文件结果不一样---!!!
   	if err := file.Close(); err != nil {
   		fmt.Printf("Close error:%v\n", err)
   	}
   }()

   sheetName := file.GetSheetName(0)
   if sheetName == "" {
   	return nil, fmt.Errorf("GetSheetName error")
   }

   rows, err := file.Rows(sheetName)
   if err != nil {
   	return nil, err
   }

   return rows, nil
}

在读取70MB文件时:
Describe the results you received:
1721307756445

Describe the results you expected:
excel只有一个sheet,一万多行四列数据。
当注释掉GetRowIterator中defer file.Close()时:可以正常打印excel每行的内容。不像上面没注释时打印只有索引值。

出现这种情况,是为什么呢?是因为对于大文件file.Close会清除内容,对于返回的*excelize.Rows打印每一行只有索引值吗?
但是对于小文件行数少时,是正常的,是否在GetRowIterator中执行file.Close都没有影响,这是为什么呢?

Output of go version:

go 1.21.3

Excelize version or commit ID:

v2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):: windows, mac都试过出现此问题

@xuri xuri changed the title *excelize.File.Close 在使用时是否有注意事项? How to use *excelize.File.Close Jul 18, 2024
@xuri
Copy link
Member

xuri commented Jul 18, 2024

Just like the documentation says, Close closes and cleanup the open temporary file for the spreadsheet, so you should not read any data after called it.

@xuri xuri closed this as completed Jul 18, 2024
@honlu
Copy link
Author

honlu commented Jul 18, 2024

Ok, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants