EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Exception occurs when reading a downloaded file

Open assadnazar opened this issue 3 years ago • 7 comments
trafficstars

I am unable to get sheets populated in the object if it has a space in it. If i remove the space from sheet name, it gets populated (ExcelPackage).

EPPlus Version 5.8.3

Here is the code

using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
    ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets.FirstOrDefault(); // Exception here... excelPackage.Workbook.Worksheets null here
    var rowCount = excelWorksheet.Dimension.End.Row;
    var colCount = excelWorksheet.Dimension.End.Column;
......
}

And the exception

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=EPPlus StackTrace: at OfficeOpenXml.ExcelWorksheet.get_DefaultColWidth() at OfficeOpenXml.ExcelWorksheet.LoadColumns(XmlReader xr) at OfficeOpenXml.ExcelWorksheet.CreateXml() at OfficeOpenXml.ExcelWorksheet..ctor(XmlNamespaceManager ns, ExcelPackage excelPackage, String relID, Uri uriWorksheet, String sheetName, Int32 sheetID, Int32 positionID, Nullable`1 hide) at OfficeOpenXml.ExcelWorksheets..ctor(ExcelPackage pck, XmlNamespaceManager nsm, XmlNode topNode) at OfficeOpenXml.ExcelWorkbook.get_Worksheets() at Talabat.Controllers.TransactionController.QPayReportImporter(IFormFile TransactionFile, DateTime ReportFromDate, DateTime ReportToDate) in TransactionController.cs:line 177

File properties screenshot image

UPDATE: The issue seems to be different. If I read a downloaded file from some source, it gives exception. But if I open that same file in MS Excel and then save it once there, then this code reads with success.

Any idea?

assadnazar avatar Dec 22 '21 08:12 assadnazar

Can you provide some code to reproduce this issue.

JanKallman avatar Dec 23 '21 07:12 JanKallman

using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
    ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets.FirstOrDefault(); // Exception here... excelPackage.Workbook.Worksheets null here
    var rowCount = excelWorksheet.Dimension.End.Row;
    var colCount = excelWorksheet.Dimension.End.Column;
......
}

assadnazar avatar Dec 25 '21 07:12 assadnazar

Sorry for the late replay. You get an exception on load, so I will need your Excel file as well.

JanKallman avatar Jan 14 '22 11:01 JanKallman

Any update on this?

assadnazar avatar Jan 31 '22 08:01 assadnazar

I don't get an exception when accessing the workbook object in version 5.8.6. However, when saved the workbook is corrupt due to a missing record in the styles - cellStyleXfs element. I'll see if I can fix that. Note that the attached workbook has a loot of errors in the styles.xml if you validate it with the ooxml validation tool.

JanKallman avatar Feb 03 '22 11:02 JanKallman

I don't get an exception when accessing the workbook object in version 5.8.6. However, when saved the workbook is corrupt due to a missing record in the styles - cellStyleXfs element. I'll see if I can fix that. Note that the attached workbook has a loot of errors in the styles.xml if you validate it with the ooxml validation tool.

The exception happens when reading downloaded file from internet which is actually readonly. To solve this, I need to open the file. Click on the Enable Content button and then save the file. This way, the same file can be read.

assadnazar avatar Feb 20 '22 06:02 assadnazar

Closing this due to inactivity

swmal avatar Feb 21 '24 08:02 swmal