我有一种情况,我将读取多行,并且在经过一些逻辑之后我需要在 Excel 工作表中写入这些行。为此,我正在使用 Apache POI。但是,我面临的问题是,只有最后一行(来自循环)被写入 Excel
有人可以帮我解决这个问题或提供一些代码片段吗?
谢谢
最佳答案
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();
关于java - 无法使用 POI 在 Excel 中写入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967965/