Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向excel中的新单元格添加数据而不删除上一个单元格_Java_Excel_Apache_Cell_Savechanges - Fatal编程技术网

Java 向excel中的新单元格添加数据而不删除上一个单元格

Java 向excel中的新单元格添加数据而不删除上一个单元格,java,excel,apache,cell,savechanges,Java,Excel,Apache,Cell,Savechanges,当我尝试将新数据添加到此xls文件时,我会丢失以前的数据。如何将新信息添加到新单元格并保存 我的代码是ApachePOI: 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.getTe

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

我的代码是ApachePOI:

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) {

}

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

这些教程非常有用,而且写得很好。他们使用ApachePOI项目开发的外部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();

希望它有助于检查我们的问题,它有很好的答案使用save()保存数据