使用 Apache POI 向 Excel 添加列 [英] Adding column to Excel using Apache POI

查看:120
本文介绍了使用 Apache POI 向 Excel 添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering about adding new column in a .xlsx file using apache poi. But I could not found anything. Is there any way to do this? Or is there exists any other library to solve this? Thanks in advance.

推荐答案

如果您的 excel 文件中包含现有行且定义明确,添加列的最快方法是在行上迭代一次,并在每次迭代中添加一列以流水代码结束

If you have excel file with existing rows, well defined, the fastest way to add column is to iterate once over the rows and in each iterations add a column at end as beflow code

    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");
    }

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

Hope it helps, I assume your first row is header, the rest will be empty for future modifications

这篇关于使用 Apache POI 向 Excel 添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆