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

3

9

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!

Jesus answered 23/4, 2014 at 8:38 Comment(0)
S
5

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.
Sudorific answered 23/4, 2014 at 8:42 Comment(7)
I tried with xlsx file and I get the same exception.Jesus
Make sure you save your file with the correct file type. You must use Excel 2007 or 2010 and in the Save dialog where it says: "Save as type:" choose "Excel Workbook" and NOT "Excel 97-2003 Workbook".Sudorific
That exactly what I did.Jesus
Make sure the file is not open in Excel whiule you try to read it, also see this question: #11212874Sudorific
The file is close during run time. The link above is about xml file I am using xlsx file that I created myself.Jesus
Good.I created file by code and the exception didn't arise again. Thank you.Jesus
@Sudorific i have a docx file and yet i get this error. Can you explain why this is happening?Barbera
C
6

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.

Carpogonium answered 6/10, 2015 at 13:0 Comment(0)
S
5

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.
Sudorific answered 23/4, 2014 at 8:42 Comment(7)
I tried with xlsx file and I get the same exception.Jesus
Make sure you save your file with the correct file type. You must use Excel 2007 or 2010 and in the Save dialog where it says: "Save as type:" choose "Excel Workbook" and NOT "Excel 97-2003 Workbook".Sudorific
That exactly what I did.Jesus
Make sure the file is not open in Excel whiule you try to read it, also see this question: #11212874Sudorific
The file is close during run time. The link above is about xml file I am using xlsx file that I created myself.Jesus
Good.I created file by code and the exception didn't arise again. Thank you.Jesus
@Sudorific i have a docx file and yet i get this error. Can you explain why this is happening?Barbera
C
-3

try using SpreadsheetDocument.Create

instead of "Open"

Cupriferous answered 6/5, 2016 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.