Java: byte[] Array to Excel to BLOB

LuckyMalaka picture LuckyMalaka · Aug 22, 2011 · Viewed 7k times · Source

i have an byte[] array that needs to be converted into an valid excel spreadsheet. After converting the byte array, the excel spreadsheet must be cached into the database preferably as BLOB.

First I tried to create an WritableWorkbook with:

WritableWorkbook workbook = Workbook.createWorkbook(byteArrayOutputStream);
...
workbook.write();

This would work fine for me, but i have no idea how to store a workbook as BLOB into the database. Is it even possible? Or is there another way?

Optionally: Instead of the byte[] array I also could use a deserialized object.

Workbook API: http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html

Answer

Jim Garrison picture Jim Garrison · Aug 22, 2011

The jdbc method PreparedStatement#setBlob() takes an InputStream as the data source argument. Just create a ByteArrayInputStream over the buffer of your byteArrayOutputStream and pass that to setBlob().