简体   繁体   中英

File contains corrupted data error when opening Excel sheet with OpenXML

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM