parsecer
parsecer

Reputation: 5136

Apache POI: Cannot change cell value

I have a program that takes a workbook, writes some data into it and then sets all cells on the first row to wow, using this method:

    public void test(String sheetName, int columnSize)  {
        Row headerRow = workbook.getSheet(sheetName).getRow(0);
        for (int i=0; i<columnSize; i++)  {
            cell.setCellValue("wow");
        }
    }

It is called from inside this method:

    public void write(List<List<String>> rows, List<String> columns,
                      String sheetName, int startRow)
                                        throws IOException {
        Sheet sheet = workbook.getSheet(sheetName);
        if (sheet==null) {
            sheet = workbook.createSheet(sheetName);
        }

        if (startRow==0)  {
            clearWorkbook();
            Row headerRow = sheet.createRow(0);

            int k=0;
            for(int c=0; c<columns.size(); c++) {
                String column = columns.get(c);
                Cell cell = headerRow.createCell(c);
                cell.setCellValue(column);
            }
        }

        int currentRow = Math.max(1, startRow);
        for (int i = 0; i <rows.size(); i++)  {
            List<String> rowData = rows.get(i);

            Row row = sheet.createRow(currentRow++);
            row.setHeight((short) 2400);

            for (int j=0; j<columns.size(); j++)  {
                Cell cell = row.createCell(j);
                cell.setCellValue(rowData.get(j));
            }
        }

        System.out.println("finished editing");
        // Resize all columns to fit the content size
        for(int z = 0; z < columns.size(); z++) {
            sheet.autoSizeColumn(z);
            sheet.setColumnWidth(z, Math.min(20000, sheet.getColumnWidth(z)));
        }

        System.out.println("finished resizing");
        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream(workbookName);
        workbook.write(fileOut);
        fileOut.close();

        System.out.println("closing");

        test(sheetName, columns.size());  //<<--------------------------
        System.out.println("closed: " + workbookFile.getAbsolutePath());
    }

However it doesn't effect cells at all. What am I doing wrong?

Upvotes: 0

Views: 369

Answers (1)

Smile
Smile

Reputation: 4088

You need to call test() before writing the workbook to FileOutputStream.

Upvotes: 2

Related Questions