java - 无法使用 POI 在 Excel 中写入多行

标签 java apache-poi

我有一种情况,我将读取多行,并且在经过一些逻辑之后我需要在 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/

相关文章:

java - 如何从上到下然后从左到右填充 GridLayout?

java - JPA 实体和 Hibernate 实体的区别

java - 保存 Excel 文档 Apache POI

java - 如何使用apache-poi获取文件的全部内容?

excel - Grails Excel导入超链接

java - JasperReports 是否会取代 Apache POI (M$) 和 iText (PDF)?

java - Jboss 不使用 hibernate-tools 映射我自动生成的实体类

java - eclipse : Can we make modifications on to the source code while debugging the Application inside Eclipse

java - 不使用 mkdir() 或 mkdirs() 创建的目录

java - 如何使用 apache poi 4.0 XDDFChartData、XDDFPieChartData 生成饼图;在使用 java 的 excel 表中,我不想使用 jfree