Bug 58510 - Comment on a cell creates a RuntimeException
Summary: Comment on a cell creates a RuntimeException
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.13-dev
Hardware: PC All
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2015-10-20 15:02 UTC by Sam
Modified: 2015-10-22 06:30 UTC (History)
0 users

The file which is causing the exception. (11.72 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2015-10-20 15:02 UTC, Sam

Note You need to log in before you can comment on or make changes to this bug.
Description Sam 2015-10-20 15:02:55 UTC
Created attachment 33190 [details]
The file which is causing the exception.

I have a Excel 2010 file with only one cell that have a comment on it (added with a right-click on the cell).

If I copy the cell into the clipBoard (with right-click "copy") and then paste into my application which find the Excel format in the clipboard and extract the content.

The content is then given to a HSSFWorkbook : 
new HSSFWorkbook(new ByteArrayInputStream(((ByteBuffer) clipboard.getContent(excelFormat)).array()));

If I have no comment, everything is fine, otherwise I have this Exception : 
"java.lang.RuntimeException: Unexpected record type (org.apache.poi.hssf.record.NoteRecord)
	at org.apache.poi.hssf.record.aggregates.RowRecordsAggregate.<init>(RowRecordsAggregate.java:97)
	at org.apache.poi.hssf.model.InternalSheet.<init>(InternalSheet.java:217)
	at org.apache.poi.hssf.model.InternalSheet.createSheet(InternalSheet.java:156)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:354)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)"

The file is in attachment.
Comment 1 Dominik Stadler 2015-10-20 17:04:36 UTC
As this is a xlsx-file, the correct class to use is XSSFWorkbook, HSSFWorkbook is for the older binary format (.xls) and won't be able to read this file.
Comment 2 Sam 2015-10-21 09:41:08 UTC
Actually I've tried with XSSFWorkbook but it is not working.

When I copy the cell into the clipboard, some formats are available. I used to get the "Biff8" format and it was working fine.

Now this is a Excel 2010 file, and apparently XSSFWorkbook is not able to open any of these format including Biff8.

Formats available when copying (XML Spreadsheet, text/rtf, cf5, cf4,cf129, Biff5 
, Biff12, text/plain, Link,text/html, Biff8, Object Descriptor, Csv, Link Source Descriptor)

I have the exception : "org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]"
Comment 3 Javen O'Neal 2015-10-21 19:42:04 UTC
XSSFWorkbook can only open xlsx and xlsm files. HSSF can only open xls files. POI cannot read other formats such as RTF, CSV, XML, BIFF5, or BIFF12.

If you do not know the format of the workbook, but you know it is xslx/m or xls, you can use WorkbookFactory.create. If you need something that works with RTF and other formats, check out Apache Tika--though this project was designed to give a text representation of a document (useful for search engines), and might not meet your needs.
Comment 4 Dominik Stadler 2015-10-22 06:30:34 UTC
The file that you attached is an XLSX file. If you try to unzip it, you see the XML-Files that make up the newer XML-based format. Therefore only XSSFWorkbook will be able to read the file. 

I also quickly tried it and I could read the file without any issue.

If you still have issues working with the file, then please send a question with you code-snippet to the user-list, as this is the best place to discuss about how to use poi.