英文:
I can't read some Excel files with ClosedXML package in .NET
问题
private List<Dictionary<string, object>> ReadExcelData(IFormFile file)
{
using (var stream = file.OpenReadStream())
{
using (var workbook = new XLWorkbook(stream))
{
....
}
}
}
在 using (var workbook = new XLWorkbook(stream))
这行代码中,我遇到了一个错误。我的 "file" 对象不为空。但是在这行代码中,它给了我 System.IO.FileFormatException: '文件包含损坏的数据。'
错误。
我尝试读取 Excel1.xls 文件,它能够正常工作,但当我尝试读取 Excel2.xls 文件时,它给了我这个错误。我进行了比较,它们的编码标准都是相同的,都是 ANSI。我将 .xls 更改为 .xlsx,但仍然不起作用。文件并没有损坏。我找不到任何解决方法。
提前谢谢
英文:
private List<Dictionary<string, object>> ReadExcelData(IFormFile file)
{
using (var stream = file.OpenReadStream())
{
using (var workbook = new XLWorkbook(stream))
{
....
}
}
}
In using (var workbook = new XLWorkbook(stream))
line, I have an error. My "file" object is not empty. But on that line, it gives me System.IO.FileFormatException: 'File contains corrupted data.'
error.
I am trying to read Excel1.xls file, it reads very well but when I try to read Excel2.xls file, it gives me this error. I compared them, their coding standarts are same, ANSI. I changed .xls with .xlsx. Still Excel1 file works, other not. Files are not corrupted. I couldnt't find any solution.
Thanks in advance
答案1
得分: 0
ClosedXML不支持旧式的.xls文件,只支持Excel 2007+的.xlsx和.xlsm文件。你需要另一个库,比如NPOI来读取.xls文件。
英文:
ClosedXML doesn't support old school .xls files, but only Excel 2007+ .xlsx and .xlsm files. You'll need another library, e.g. NPOI to read .xls.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论