While working with the Apache POI Implementation I ran into a strange behaviour. I cannot explain the cause, so if anybody can give some hints, I would love to hear them. It is not even a big blocker for the problem that I was solving - at this point it is more a curiosity thing. So here it goes:
public static void main(String[] args) throws EcatException, SQLException, IOException, Exception {
long ts = System.currentTimeMillis();
SXSSFWorkbook wb = new SXSSFWorkbook();
SXSSFSheet test = wb.createSheet("Test");
SXSSFRow r = test.createRow(0);
Cell c = r.createCell(0);
c.setCellValue("TEST");
wb.write(new FileOutputStream("D:/wb-" + ts + ".xlsx"));
wb.close();
XSSFWorkbook wb2 = new XSSFWorkbook("D:/wb-" + ts + ".xlsx");
XSSFSheet s = wb2.getSheet("Test");
s.getRow(0).getCell(0).setCellType(CellType.STRING);
System.out.println(s.getRow(0).getCell(0).getStringCellValue());
wb2.close();
}
As you can see, this will create a SXSSFWorkbook
with one row and one cell with the value "TEST".
Then opening the workbook againg, and print the content of that one cell to the console.
My expectation is to see "TEST" on the console, but I do not. The output is empty.
- If I remove the line
s.getRow(0).getCell(0).setCellType(CellType.STRING);
the output is as expected.
If I switch from using a
SXSSFWorkbook
toXSSFWorkbook
the output is as expected.And most curious, if I open the resulting xlsx file, save it and close it again, then running the read part of the above code, the output is as expected.
Is that something that someone has an explanation for? Btw. I tried with different version of POI, it had the same results everytime.