Shir
Shir

Reputation: 263

OpenXml SpreadsheetDocument.Open(...) get exception - The main part is missing

I have an xlsx doc with headers and it is located in my project path. I want to insert data using OpenXML but I get an exception while opening the file:

using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(MyxlsFileName, true))
{
    // ...
}

The exception:

"The specified package is invalid. The main part is missing"

What is the meaning of the exception and what is wrong?

Thank in advance!

Upvotes: 9

Views: 16787

Answers (3)

imprezza
imprezza

Reputation: 1

try using SpreadsheetDocument.Create

instead of "Open"

Upvotes: -3

tom_imk
tom_imk

Reputation: 165

While this might be very obvious, it happened to me: Another situation that might trigger the excact same error is when you get the name of the file wrong. So check that you really have the path right, for me I ended up with a newly created file (with the wrong spelling) but still got the very same error.

Upvotes: 6

helb
helb

Reputation: 7773

You cannot open XLS files with OpenXml. You must save your Excel file as an XLSX file using Microsoft Excel 2007 or newer. (Assuming you are using the correct file extension here...)

Files with XLS extension are in a proprietary binary format which cannot be processed with OpenXML.

So you have two options:

  1. Convert your XLS files to XLSX manually before processing them.
  2. Find a library or API to process XLS files.

Upvotes: 5

Related Questions