Unable to write multiple rows in Excel using POI

I have a situation where I will read several lines, and after some logic, I need to write lines in an Excel worksheet. For this purpose I use Apache POI. However, the problem I ran into is that only the last row (from the loop) is written to Excel

Can someone help me with this or provide a piece of code?

thanks

+7
source share
1 answer
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(2); Cell cell = row.createCell(2); cell.setCellValue("Use \n with word wrap on to create a new line"); //to enable newlines you need set a cell styles with wrap=true CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); //increase row height to accomodate two lines of text row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); //adjust column width to fit the content sheet.autoSizeColumn((short)2); FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); wb.write(fileOut); fileOut.close(); 
+15
source

All Articles