This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Finding file type XLS or XLSX using POI APIs. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Finding file type XLS or XLSX using POI APIs." Watch "Finding file type XLS or XLSX using POI APIs." New topic
Author

Finding file type XLS or XLSX using POI APIs.

Samir Banerjee
Ranch Hand

Joined: Jun 21, 2010
Posts: 72
This one is weird but if some one can help me out with this...
There is on function in our application where we pass the excel data as byte format :


now depending upon the file type xls or xlsx I want to do.

OR

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

An XLSX file is actually just a special ZIP file. As such, its first two bytes are always PK (0x50 and 0x4B). You can even check the source of java.util.zip.ZipInputStream how it reads the first few bytes. Check the "src.zip" file inside your JDK installation.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
Also be aware that the org.apache.poi.ss package contains classes that can hide the differences between both formats. For example, WorkbookFactory.create(InputStream) returns a Workbook you can use regardless of actual file format. Much better than having to code that yourself :-)


Ping & DNS - my free Android networking tools app
Samir Banerjee
Ranch Hand

Joined: Jun 21, 2010
Posts: 72
Thank you friends. Using org.apache.poi.ss package solved my problem as now I do not need to bother about the file type.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding file type XLS or XLSX using POI APIs.
 
Similar Threads
Locking Excel 2003 sheets using Apache POI
Read a .xls,.xlsx file format using XSSF
Accessing an Excel file inside a WAR file
IO - POI - HSSF
hassle with jakarta poi