Dims
Dims

Reputation: 51039

How to distinguish between XSSF and HSSF automatically in Apache POI?

I would like to be able to open Excel file of arbotrary type. Is it possible to select between HSSFWorkbook and XSSFWorkbook automatically?

Currently I write

Workbook workbook = new HSSFWorkbook(excelFile);

Can I write universal?

Upvotes: 1

Views: 1373

Answers (1)

Gagravarr
Gagravarr

Reputation: 48326

Yes! All you need to do is use WorkbookFactory

As per this part of the docs, it's better to use a File than an InputStream. So, just do something like:

File file = new File("input.xls");
Workbook wb = WorkbookFactory.create(file);

That will create whichever of HSSFWorkbook or XSSFWorkbook your file needs

Upvotes: 5

Related Questions