1

This is the exception I am getting:

org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

The file I am trying to open is an .xls file, I searched for possible solutions, I found this and this, but I am already doing this correctly, so this is not the problem.

My code:

InputStream file = new FileInputStream(new File(path));
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheetNumber);

sheetNumber is an int (It´s always 0 in my software) and the path is correct, I tested these two many times before posting this, to make sure I am not failing there. The line that is throwing the exception is:

org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);

(If you want more details on the code, you can find the whole class Here)

Does anyone know where can I be failing? Thank you all.

Community
  • 1
  • 1
Mayuso
  • 1,291
  • 4
  • 19
  • 41
  • If you try to open the file with Excel, can it open it? And if you save-as in Excel, does the new file work fine with POI? – Gagravarr Mar 14 '16 at 16:44
  • I can open it without any problem (Ubuntu 14.04), and when I save it again from my PC (Overwrite it using "Save as...") my software throws no exception with the new file. (I must say I do not create the excels). – Mayuso Mar 14 '16 at 16:50
  • I am not sure what that means though. File extension is incorrect before overwriting it? – Mayuso Mar 14 '16 at 16:51
  • If you grab a copy of the [Apache Tika CLI App](http://tika.apache.org/download.html) and run that on the file with `--detect foo.xlsx` and `--detect < foo.xlsx`, what does it report for the two cases? – Gagravarr Mar 14 '16 at 16:57
  • The first one (Without `<`) throws exception: `Exception in thread "main" java.net.MalformedURLException: no protocol: foo.xlsx`. The second one (With `<`) tells me the file does not exist. – Mayuso Mar 15 '16 at 07:44
  • If I run the exact same two commands using `.xls` instead of `.xlsx`, it gives me `application/vnd.oasis.opendocument.spreadsheet` in both cases. – Mayuso Mar 15 '16 at 07:45
  • A classmate has told me he can´t open these excel files in Windows 7 (MS Excel). I can with Ubuntu 14.04 (LibreOffice) though. – Mayuso Mar 15 '16 at 07:54
  • You need to replace `foo.xlsx` with the name of your problematic file! – Gagravarr Mar 15 '16 at 10:04
  • Yes, I know, I did (I am dumb, but not so much xD), but my file is .xls (Not .xlsx), that´s why it says it does not exist, and that´s why I added the report when I run the command for `.xls`. – Mayuso Mar 15 '16 at 10:48
  • `application/vnd.oasis.opendocument.spreadsheet` = Open Document Format = ODS. You need to use OpenOffice / LibreOffice or similar. From java, try Apache ODFToolkit. Apache POI can't handle these files – Gagravarr Mar 15 '16 at 11:38
  • Thank you. In fact, this is really bad news, I need to learn about a new API now, without much time :(. Thanks anyway :) – Mayuso Mar 15 '16 at 12:06

1 Answers1

1

Promoting some comments to an answer

Your first issue is the version of Apache POI you're using. If you were to use POI 3.15 beta 1 or newer, you'd get a much more helpful Exception in this situation.

In POI 3.15 beta 1 and newer, you'll get a ODFNotOfficeXmlFileException with a message like:

ODFNotOfficeXmlFileException: The supplied data appears to be in ODF (Open Document) Format. Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit

.

As it is, you can find (and have found, thanks!) out what your file really is by using the Apache Tika CLI App in --detect mode. As you found, that gave application/vnd.oasis.opendocument.spreadsheet which is an OpenDocument Format Spreadsheet, normally with the .ods extension. So, someone has renamed the .ods file to .xlsx, which while it may make Excel open it, doesn't magically change the format!

Apache POI doesn't support the ODF formats, so you'll either need to convert the file to a true Excel format, or use something like Apache ODF Toolkit to process it.

Mayuso
  • 1,291
  • 4
  • 19
  • 41
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Yes, I am in fact using POI 3.13. Thank for your answer, time for me to start learning about ODF Toolkit. – Mayuso Mar 15 '16 at 12:47
  • Offtopic update: I told the one creating the excel files about all of this and he realised he was creating the excel files wrong. We fixed it, and Apache POI works fine now. However, I must say ODF Toolkit is interesting (I won´t quit learning about it xD), and the answer still 100% correct. – Mayuso Mar 15 '16 at 15:38