【问题标题】:Can we append two excel sheets in java?我们可以在java中附加两个excel表吗?
【发布时间】:2020-06-22 19:50:45
【问题描述】:

我正在尝试用新数据附加现有 Excel 工作表的数据,可以使用来自 Java - Appending data to same Excel file with FileOutputStream 的参考代码(这是我从中获取参考的链接)。

是否有任何其他方法可以将整个 Excel 工作表直接附加到现有 Excel 工作表而不覆盖现有 Excel 工作表中的当前数据?

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    您可以执行以下操作:

    // Create an POI Workbook from your FileInput:
    try (InputStream in = new BufferedInputStream(new FileInputStream("c:\\path\\to\\your\\file.xlsx")); 
        Workbook workbook = new XSSFWorkbook(in);
        FileOutputStream out = new FileOutputStream("c:\\path\\to\\your\\newfile.xlsx")) {
    
        // get the first sheet
        Sheet firstSheet = workbook.getSheetAt(0);
    
        // get the last row in the sheet
        int lastRowNum = firstSheet.getLastRowNum()
    
        // add data after the lastRow
        Row newRow = firstSheet.createRow(lastRowNum + 1);
        Cell newCell = newRow.createCell(0);
        newCell.setCellValue("newData");
    
        // and finally write the file somewhere
        workbook.write(out);
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多