Abhay
Abhay

Reputation: 31

Creating an xssf workbook from an uploaded .xlsx(2007 Excel file)

In the code the client uploads a standard .xlsx file through struts.

In the action we take the file using

FormFile myfile = ABCForm.getTheFile();

My query is how would I create a XSSFWorkbook out of this file.

I also have a parallel process for accessing 2003 excel file this way

FormFile myfile = ABCForm.getTheFile();
byte[] fileData = myFile.getFileData();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);

in the Helper class

POIFSFileSystem fs = new POIFSFileSystem(byteArrayInputStream);
HSSFWorkbook wb = new HSSFWorkbook(fs);

and then further processing of the workbook.

I would like to know how to create a XSSFWorkbook as I have created the HSSFWorkbook above.

I have all the ooxml and related jars in the classpath and lib folder.

XSSFWorkbook cannot be created from byteArrayInputStream. Apache XSSF Workbook . It can only be created from Inputstream and OPCPackage.

Any ideas/pointers on how to go about further. I will be glad to give further details about any other code snippets that would help.

Upvotes: 3

Views: 25522

Answers (1)

Gagravarr
Gagravarr

Reputation: 48376

You can happily create a XSSFWorkbook from an InputStream

One option is to use WorkbookFactory, which auto-detects the appropriate type and returns a HSSFWorkbook or XSSFWorkbook for you:

Workbook wb = WorkbookFactory.create(inputStream);

Otherwise, you can create a XSSFWorkbook from a stream like this:

XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(inputStream));

However, be aware that opening a XSSFWorkbook from a File needs less memory than from a Stream - with a Stream the whole file will need to be buffered into memory

Upvotes: 10

Related Questions