Edit existing excel files using jxl api / Apache POI

Solution 1:

The tutorials here are very helpful and well-written. They use an external JAR developed by the Apache POI project. Here's an simple example of editing one cell:

    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();

Hope it helps

Solution 2:

One very important tip that I learned the hard way. Open the OutputStream only after you have completed writing to your excel workbook. Zabbala's example is spot on and shows this correctly. If you open the OutputStream any earlier, your changes would not be written to the file after your program exits and you would be scratching your head as I did.

Solution 3:

I refresh the formulas with another tab for this I use the next sentence

HSSFSheet worksheetse = workbook.getSheetAt(0);
worksheetse.setForceFormulaRecalculation(true); 

but it's necesary that you apply the method setForceFormulaRecalculation for all the tabs that have the formulas.

Sorry for my English

Solution 4:

Hello i have the same problem than neXGen. But strangely if i open the file with openoffice, it works!

Edit: perhaps i found a solution, put this after changing the values:

HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);