简体   繁体   中英

Apache POI setting cells to 0

I'm using Apache POI to generate cells but I'm noticing that it seems to set all my cells to the same value. In this case 0. I don't really see why. When I am about to save my sheet to file I can see that the cells have values, but when looking at the generated file it is empty. Any help would be greatly appreciated.

Here is a screen of the debugging when I can see that some cells have values(such as the "5" in the debug window):

在此处输入图片说明

Here is the code I am using to generate the sheet:

@Override
public void generate(TableRepresentation tableRepresentation) {
    int rows = tableRepresentation.getRows();
    int columns = tableRepresentation.getColumns();
    final HSSFWorkbook workbook = new HSSFWorkbook();
    final HSSFSheet sheet = workbook.createSheet();
    for (int i = 0; i < rows; i++) {

        final HSSFRow row = sheet.createRow(i);
        for (int j = 0; j < columns; j++) {
            row.createCell(j).setCellValue(tableRepresentation.get(i, j));
        }

    }

    try {
        final FileOutputStream fos = new FileOutputStream("tjockis.xls");

        workbook.write(fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("tjockis.xls" + " written successfully");

}

public interface TableRepresentation {
    int getRows();

    int getColumns();

    String get(int i, int j);
}

EDIT:

Here is how the exported sheet looks:

在此处输入图片说明

If you make any variable as final , you cannot change the value of final variable(It will be constant).

A final variable may only be assigned to once and its value will not change and can help avoid programming errors.

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM