我无法使用.NET中的ClosedXML包读取一些Excel文件。

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

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,但仍然不起作用。文件并没有损坏。我找不到任何解决方法。

提前谢谢 我无法使用.NET中的ClosedXML包读取一些Excel文件。

英文:
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 我无法使用.NET中的ClosedXML包读取一些Excel文件。

答案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.

huangapple
  • 本文由 发表于 2023年7月27日 21:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定