.netexcelnugetclosedxml

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))
            {
                
                ....
            }
    }
}

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 :)


Solution

  • 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.