Error reading xlsx (2007) file in EPPLUS

跟風遠走 提交于 2019-12-22 10:34:04

问题


I have an error while trying to read excel files (xlsx) which were saved in Excel 2007 using EPPlus library.

Some workaround:

  1. ASP.net mvc 5 app with EPPlus v. 4.0.4.0
  2. User can download template file from my site, then fill required data in it, and upload it back. File contains 4 worksheets, one of them is hidden. Workbook is password protected, worksheets are proteced too. All of them have different passwords. Template file (xlsx) produced in Excel 2007 or 2010.
  3. When user posts file back I'm trying to open and read it using EPPlus
  4. When I post file which was filled in Excel2010 everythins works fine. But when I post file from Excel 2007 a have an exception in ExcelPackage ctor.

Ok. Some code snippets now. There is my initial code of uploading and reading file using streams.

[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("ImportPublications")]
public async Task<JsonResult> AjaxImportPublications(int id, HttpPostedFileBase xlsImport) {
    AjaxExtResult result = new AjaxExtResult();

    if (xlsImport == null || xlsImport.ContentLength == 0) {
        result.Error = new[] { "Ошибка загрузки файла" };
        return Json(result);
    }

     try {
         using (var package = new ExcelPackage(xlsImport.InputStream)){ 
            // do something here
         }
         result.Success = true;
     }
     catch (Exception e) {
         result.Error = new[] { e.Message };
     }

     return Json(result);
}

The code above works with Excel 2010, but doesn't with 2007. Exception is thrown then calling ExcelPackage() ctor.

Debug data:

updloaded file ContentType is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Exception details (disk write error):

Message:    "Ошибка на диске в процессе записи. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))"

StackTrace: 
at OfficeOpenXml.Utils.CompoundDocument.ILockBytes.WriteAt(Int64 ulOffset, IntPtr pv, Int32 cb, UIntPtr& pcbWritten)
at OfficeOpenXml.Utils.CompoundDocument.GetLockbyte(MemoryStream stream)
at OfficeOpenXml.ExcelPackage.Load(Stream input, Stream output, String Password)
at OfficeOpenXml.ExcelPackage.Load(Stream input)
at OfficeOpenXml.ExcelPackage..ctor(Stream newStream)
at webDCReports.Controllers.MembersController.<AjaxImportPublications>d__56.MoveNext() ...

After that I tried to add workbook protection password to ctor paramters and changed code to

using (var package = new ExcelPackage(xlsImport.InputStream, "my_passwrod")) {

after that I've got new exception type: "The stream must be read/write". It looks Ok as I do not save uploaded file, so I decided to save file and changed code to (without pwd):

var fi = new FileInfo(target);    
using (var package = new ExcelPackage(fi)) {

and Exception details are:

Message: Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password.
Inner exception: "The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor."

At this point, when I upload 2010-saved file - code works fine. Adding workbook password to calling code gives me Invalid password Exception.

Actually it also looks Ok, as file itself is not password protected/encrypted.

Does anyone know what is going on, how to fix this?

IMPORTANT UPDATE:

I've removed woorkbook protection and file opens successfully.

来源:https://stackoverflow.com/questions/33779054/error-reading-xlsx-2007-file-in-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!