Using Apache POI HSSF, we can create xls file like this
private void write(HSSFWorkbook workbook) {
POIFSFileSystem filesystem = new POIFSFileSystem();
filesystem.createDocument(new ByteArrayInputStream(workbook.getBytes()),
"Workbook");
FileOutputStream stream = new FileOutputStream("test.xls");
filesystem.writeFilesystem(stream);
}
Similarly, how can I write with XSSFWorkbook
? This does not have the getBytes()
method.
I tried to create ByteArrayInputStream
from XSSFWorkbook
like this -
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos); //XSSFWorkbook here
ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray());
But the xlsx file created was corrupt. How can I write the workbook to disc using POIFSFileSystem
?
The same XSSFWorkbook
was written sucessfully when I did like this -
FileOutputStream stream = new FileOutputStream("test.xlsx");
workbook.write(stream);
When I extracted and compared the xlsx files, there was no difference. However, when I do a plain text compare on the xlsx files directly (without extracting), there are few differences in the bytes.
So the problem should be in the createDocument()
and/or writeFilesystem()
methods of POIFSFileSystem
. Can someone let me know how to write XSSFWorkbook
using POIFSFileSystem
?