davidpelfree/sjxlsx

Streaming without going through a file?

Opened this issue · 1 comments

I'm trying to use this library in order to generate an execel file on the fly. The end goal is not to generate an excel file on disk but rather generate it from a database and push it to an outputstream. Thus this doesn't make any sense to me:

File file = ..
SimpleXLSXWorkbook workbook = new SimpleXLSXWorkbook(file);

Why do I need the file? I've tried to just generate an empty file like this:

File file = Files.createTempFile(UUIDUtil.newUUID(), "xlsx").toFile();
SimpleXLSXWorkbook workbook = new SimpleXLSXWorkbook(file);

but this throws:

java.lang.RuntimeException: java.util.zip.ZipException: zip file is empty

Do you require an existing valid excel file for this to work?

When I want to create a new .xlsx Excel file on the fly, I start from an empty .xlsx that I create first.
Then I write code similar to that found in the samples for appending new rows

SimpleXLSXWorkbook workbook= new SimpleXLSXWorkbook(new File("EmptyWorkbook.xlsx"));
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("MyFirstExcelGithub.xlsx"));
Sheet sheet = workbook.getSheet(0, false);
int newRow= sheet.modify(-1, 0, "value for column A", null);
sheet.modify(newRow, 1, "value for column B", null);
newRow= sheet.modify(-1, 0, "other value for column A", null);
sheet.modify(newRow, 1, "other value for column B", null);
workbook.commit(outputStream);
outputStream.close();