【问题标题】:How to copy/write particular row using apache poi如何使用 apache poi 复制/写入特定行
【发布时间】:2020-02-17 18:17:23
【问题描述】:

我知道我在这里缺少主要功能。我想使用 apache POI 仅将源 excel 中的特定行复制/写入目标 excel。

例如。我的源 excel 有 10 行。我只需要将第 5 行复制到我的目标 excel。

我的班级,

public class Test1 {
    public static void main(String[] args) throws Exception{
        File srcFile=new File("C:\\Test\\Read.xlsx");
        FileInputStream fis=new FileInputStream(srcFile);

        XSSFWorkbook wb=new XSSFWorkbook(fis);
        XSSFSheet sheet1=wb.getSheetAt(0);

        File desFile=new File("C:\\Test\\Write.xlsx");
        FileOutputStream fout=new FileOutputStream(desFile);

        wb.write(fout);

        wb.close();     
    }
}

【问题讨论】:

  • 为什么在此处标记 Selenium?我错过了什么吗?

标签: java excel apache-poi


【解决方案1】:

根据Apache POI docs,您可以使用XSSFSheet.getRow(int) 方法获取特定索引处的行。

// 5th row
Row row = sheet.getRow(4);

要将此行添加到新工作簿,您必须遍历 row 对象中的每个单元格,并将新工作簿中的单元格值设置为这些值。一个例子可以在here找到。

【讨论】:

  • sheet.getRow(4) 返回空异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多