Trashy
Trashy

Reputation: 3

write more than one row into EXCEL with OI-3.5-FINAL

I have some Data to put them into an Excel sheet, 4 values, n rows. I tried this:

final HSSFWorkbook wb = new HSSFWorkbook();
final HSSFSheet sheet = wb.createSheet();
FileOutputStream stream;
HSSFRow row = null;
try {
    stream = new FileOutputStream(targetDirectory + "workbook.xls");

    for (int i = 1; i < data.size(); i++) {

        row = sheet.createRow((short) i);
        cell = row.createCell(0);
        cell.setCellValue("Das");
        cell = row.createCell(1);
        cell.setCellValue("hier");
        cell = row.createCell(2);
        cell.setCellValue("soll");
        cell = row.createCell(3);
        cell.setCellValue("rein");
        wb.write(stream);

    }

    stream.close();


} catch ...

While writing into the EXCEL file I can watch growing the size to 1.600 kB. When I open the sheet I see one row. Closing the file without saving decrease the size to 4 kB. It seems as the rows where all there but invisible?

Any idea, what am I doing wrong?

Upvotes: 0

Views: 651

Answers (1)

Gagravarr
Gagravarr

Reputation: 48326

I'm not sure why, but you seem to be writing the workbook out on every single row, always to the same file. That means you'll end up with multiple copies of the workbook in the same file, which isn't really what you wanted at all!

Move the wb.write call to the end, and it should all work just fine. Excel is a binary format, it's not like CSV, so you should only write the file out when you're finished

Upvotes: 2

Related Questions