Changing value of cell in Excel via apache-poi

2019-09-16 12:47发布

问题:

I'm trying to change value of cell in .xls document.

In .xls file i have got only 1 cell - A1 with abc value inside.

My code:

        File fo = new File("D:\\TMP\\Zeszyt1.xls");
        HSSFWorkbook a = new HSSFWorkbook(new FileInputStream(fo));
        HSSFSheet my_sheet = a.getSheetAt(0);
        HSSFRow my_row = my_sheet.getRow(0);

        HSSFCell myCell;
        myCell = my_row.getCell(0);
        myCell.setCellValue("NEW VALUE");

How to commit this changes? When i open .xls file i still have got abc value inside A1.

回答1:

You have to write to the file.

FileOutputStream outputStream = new FileOutputStream(new File("abc.xls"));
workbook.write(outputStream);
outputStream.close();//Close in finally if possible