使用jxl api/Apache POI编辑现有的Excel文件

24 java excel

我很感兴趣,想了解更多关于java,如何写入现有的Excel工作表/操纵现有数据.我想知道你是否可以给我一个关于如何编辑现有excel文件并使用jxl api/Apache POI保存它的想法,或者给我一个关于如何编辑现有excel文件中的一些数据然后保存它的示例程序提前致谢 !!

Zab*_*ala 31

这里的教程非常有用且编写得很好.他们使用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();
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你


小智 5

一个非常重要的提示,我学到了很多困难.只有在完成对Excel工作簿的写入后才能打开OutputStream .Zabbala的例子是正确的,并正确地显示了这一点.如果您之前打开OutputStream,则在程序退出后您的更改将不会写入文件,您会像我一样搔脑.