EPPlus
EPPlus copied to clipboard
Exception occurs when reading a downloaded file
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

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?
Can you provide some code to reproduce this issue.
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;
......
}
Sorry for the late replay. You get an exception on load, so I will need your Excel file as well.
Any update on this?
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.
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 -
cellStyleXfselement. 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.
Closing this due to inactivity