使用 Apache POI 将列添加到 Excel

2024-04-03

我想知道如何使用 apache poi 在 .xlsx 文件中添加新列。但我找不到任何东西。有什么办法可以做到这一点吗?或者是否有其他库可以解决这个问题?提前致谢。


如果您的 Excel 文件包含已定义的现有行,则添加列的最快方法是在行上迭代一次,并在每次迭代中在末尾添加一列作为下面的代码

    FileInputStream excelFile = new FileInputStream(new File(fileDirectory+file));
    Workbook workbook = new XSSFWorkbook(excelFile);
    Sheet datatypeSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = datatypeSheet.iterator();

    // Add additional column for results
    while (iterator.hasNext()) {
        Row currentRow = iterator.next();
        Cell cell = currentRow.createCell(currentRow.getLastCellNum(), CellType.STRING);
        if(currentRow.getRowNum() == 0)
            cell.setCellValue("NEW-COLUMN");
    }

希望它有帮助,我假设你的第一行是标题,其余的将为空以供将来修改

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Apache POI 将列添加到 Excel 的相关文章

随机推荐