File contains corrupted data error when opening Excel sheet with OpenXML

23,673

AH, stupid me forgot to set permissions for the Excel service to read and write. I also found plenty of good information in the following places:

Programmatically Reading excel file from SharePoint library

http://www.sharepointwithattitude.com/archives/61

Read Data From Excel File

http://alancejacob.blogspot.in/2012/06/read-data-from-excel-file-and-insert-in.html

Share:
23,673
Tangster
Author by

Tangster

Updated on November 05, 2020

Comments

  • Tangster
    Tangster over 3 years

    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.