0

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?

3
  • 2
    You need to call test() before writing the workbook to FileOutputStream. Commented Dec 10, 2019 at 10:52
  • 1
    @Smile Gosh, thanks! Never would have noticed! Commented Dec 10, 2019 at 10:54
  • Moved my comment to answer Commented Dec 10, 2019 at 10:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.