C# – File contains corrupted data error when opening Excel sheet with OpenXML

cexcelopenxmlsharepoint

I'm writing an Event Receiver for ItemAdded with SharePoint 2013. I'm using OpenXML to read the spreadsheets. When I try to open the logfile that lives in the SharePoint Library programmatically, I get the following error:

File contains corrupted data

The block of code throwing this error is:

using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mstream, false))
using (SpreadsheetDocument logsheet = SpreadsheetDocument.Open(logstream, false))

The first line passes fine, the second line (logsheet) breaks. They are both being read from a stream. spreadsheet is the item being added when the event fires and logsheet is an item that currently exists in SharePoint.

spreadsheet is opened from a data stream using the path:

string workbookpath = properties.ListItem.Web.Url + "/" + properties.ListItem.File.Url;

and logsheet is opened using a datastream using its direct URL in SharePoint

string logFilePath = "http://SPSiteName/sites/SP/Dropbox/RuntimeLog.xml";

Any help or insight is greatly appreciated.

Best Answer

Your logsheet is an XML file [.xml]. It's not an Excel file [.xlsx] (Open XML doesn't mean just XML). That's why it failed.

Related Topic