Hello James,

The most obvious answer is the WorkbookFactory class -
http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html
- if you have a valid Excel workbook then it will return an instance of
either the XSSF or HSSFWorkbook class. That does impose some overhead of
course as the Excel file will effectively be opened which could take a few
moments and tie up some memory.

The other option would be to look at the file header, the first few bytes of
the file. There is a website - filext.com - that includes provides this sort
of information. For example, here is the information for the .xls file
format http://filext.com/file-extension/XLS and this for the .xlsx
http://filext.com/file-extension/xlsx. In essence, you would open a stream
onto the file, recover the first few bytes and see if they match either
pattern; but I do not know whether this is an entirely fail safe option.

Yours

Mark B

PS. You have posted this onto the the dev list when if really ought to be
posted onto the user list. The dev list is where you would post if you were
experiencing problems with the API - for example a particular file provoking
exceptions - or if you wanted to ask for an enhancement. Furthermore, fewer
people view the dev list and you are reducing your chances of receiving a
response to your question.



James Geroge wrote:
> 
> Hi Friends,
> Is there a way to know the file is an excel file without manipulating the
> file extension, as the users can send the excel files in format like
> below.
> Test
> Test.xls
> Test.xlsx
> Test.xlsxxlsx(by renaming the file using windows explorer)
> Test.xlsabcd (by renaming
> 
> Thanks,
> James George.
> 

-- 
View this message in context: 
http://old.nabble.com/How-to-check-for-valid-excel-files-using-POI-without-checking-the-file-extension-tp28287650p28287694.html
Sent from the POI - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to