Thanks Mark, That’s exactly what I was after. And, just so I’m clear, with the Workbook factory, I just check if the Workbook is an instance of either HSSF or XSSF and process accordingly.
On a related topic it would be great if the specifics for the two event based readers (for HSSF and XSSF) could be abstracted away so the we could use one set of methods regardless of the document type. Much like the user model. Z. > > So, to be clear, all you want to do is identify which files use the OpenXML > file format and which are binary? > > If so, then take a look at the org.apache.poi.ss.usermodel.WorkbookFactory > (http://poi.eu.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory. > html) > class. All you need to do is call the static create method pasing an > InputStream and you will receive back an instance of either the HSSF or > XSSFWorkbook class depending upon the type of the file. Myself, I have never > tried using it with files that lack extensions but it ought to work and I > would certainly suggest giving it a try. Alternatively, you can simply catch > exceptions; i.e. try to open the file as an HSSFWorkbook, catch the > exception if the format is not correct and try to open it as an XSSFWorkbook > then catch and handle the exception thrown if the format is again invalid. > Finally, you could open an InputStream onto the file and examine the first > few bytes - I think it is safe to assume that the xml header would be the > first thing you read from an OpenXML based file. > > Yours > > Mark B > > > Sparecreative wrote: >> > >> > We¹re currently successfully using poi in a webapp to read from uploaded >> > excel files. >> > >> > At present we do a check on the filename extension. If xls we use HSSF and >> > if xslx we use XSSF and this is working reasonably well. >> > >> > The problem we have is that some of our users are uploading files with no >> > extension. Is there a way to determine the type of excel file that we are >> > dealing with programmatically? >> > >> > I¹d appreciate any pointers as I haven¹t had a lot of luck finding >> > anything >> > on the web. >> > >> > Z. >> > >> >
