【问题标题】:Adding data to a new cell in excel without deleting the previous cell将数据添加到excel中的新单元格而不删除前一个单元格
【发布时间】:2015-12-25 02:53:50
【问题描述】:

当我尝试向此 xls 文件添加新数据时,我丢失了以前的数据。如何将新信息添加到新单元格并保存?

我的代码是 Apache POI:

Workbook w = new HSSFWorkbook();

Sheet s = w.createSheet("new");   

Cell a = s.createRow(0).createCell(0);    
Cell b = s.createRow(0).createCell(1);    
a.setCellValue(jTextField1.getText());    

try {
    FileOutputStream f = new FileOutputStream("C://NEW.xls");
    w.write(f);
    w.close();`
} catch (Exception e) {

}

【问题讨论】:

  • 检查我们的这个问题,它有很好的答案link
  • 使用 save() 保存数据

标签: java excel apache cell savechanges


【解决方案1】:

您需要输入工作表(使用 InputStream),添加记录,然后再次保存。现在您正在创建一个新的 Workbook 对象,然后使用 OutputStream 编写它,这将覆盖已经存在的内容。

【讨论】:

    【解决方案2】:

    ORIGINAL

    教程here 非常有用且写得很好。他们使用由 Apache POI 项目开发的外部 JAR。 这是一个编辑一个单元格的简单示例:

        InputStream inp = new FileInputStream("wb.xls");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt([sheet index]);
        Row row = sheet.getRow([row index]);
        Cell cell = row.getCell([cell index]);
        String cellContents = cell.getStringCellValue(); 
        //Modify the cellContents here
        // Write the output to a file
        cell.setCellValue(cellContents); 
        FileOutputStream fileOut = new FileOutputStream("wb.xls");
        wb.write(fileOut);
        fileOut.close();
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多