title: Error reading xlsx (2007) file in EPPLUS
body: <p>I have an error while trying to read excel files (<code>xlsx</code>) which were saved in <code>Excel 2007</code> using <code>EPPlus</code> library.</p>
  
  <p>Some workaround:</p>
  
  <ol>
  <li>ASP.net mvc 5 app with EPPlus v. <code>4.0.4.0</code></li>
  <li>User can download template file from my site, then fill required data in it, and upload it back. File contains <code>4</code> worksheets, one of them is <code>hidden</code>. Workbook is password protected, worksheets are proteced too. All of them have different passwords. Template file (<code>xlsx</code>) produced in <code>Excel 2007 or 2010</code>.</li>
  <li>When user posts file back I'm trying to open and read it using <code>EPPlus</code></li>
  <li>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 <code>ExcelPackage</code> ctor.</li>
  </ol>
  
  <p>Ok. Some code snippets now. There is my initial code of uploading and reading file using streams.</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  [ActionName("ImportPublications")]
  public async Task&lt;JsonResult&gt; 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);
  }
  </code></pre>
  
  <p>The code above works with Excel 2010, but doesn't with 2007. <strong>Exception is thrown then calling <code>ExcelPackage()</code> ctor.</strong></p>
  
  <p>Debug data:</p>
  
  <p>updloaded file <code>ContentType</code> is <code>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"</code></p>
  
  <p>Exception details (disk write error):</p>
  
  <pre><code>Message:    "Ошибка на диске в процессе записи. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))"
  
  StackTrace: 
  at OfficeOpenXml.Utils.CompoundDocument.ILockBytes.WriteAt(Int64 ulOffset, IntPtr pv, Int32 cb, UIntPtr&amp; 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.&lt;AjaxImportPublications&gt;d__56.MoveNext() ...
  </code></pre>
  
  <p>After that I tried to add workbook protection password to <code>ctor</code> paramters and changed code to </p>
  
  <pre><code>using (var package = new ExcelPackage(xlsImport.InputStream, "my_passwrod")) {
  </code></pre>
  
  <p>after that I've got new exception type: <strong>"The stream must be read/write"</strong>. It looks Ok as I do not save uploaded file, so I decided to save file and changed code to (without pwd):</p>
  
  <pre><code>var fi = new FileInfo(target);    
  using (var package = new ExcelPackage(fi)) {
  </code></pre>
  
  <p>and Exception details are:</p>
  
  <pre><code>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."
  </code></pre>
  
  <p>At this point, when I upload 2010-saved file - code works fine.
  Adding workbook password to calling code gives me <strong>Invalid password</strong> Exception.</p>
  
  <p>Actually it also looks Ok, as file itself is not password protected/encrypted. </p>
  
  <p><strong>Does anyone know what is going on, how to fix this?</strong></p>
  
  <p><em>IMPORTANT UPDATE:</em></p>
  
  <p>I've removed <code>woorkbook</code> protection and file opens successfully.</p>