通过apache-poi更改Excel中单元格的值 [英] Changing value of cell in Excel via apache-poi

查看:273
本文介绍了通过apache-poi更改Excel中单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在.xls文件中,我只有一个单元格 - A1与 abc 的值。



我的代码:

  File fo = new File(D:\\TMP\\Zeszyt1.xls); 
HSSFWorkbook a =新的HSSFWorkbook(新的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);

如何提交此更改?当我打开.xls文件时,我仍然在A1中有 abc 值。

解决方案

您必须

  FileOutputStream outputStream = new FileOutputStream(new File(abc.xls)); 
workbook.write(outputStream);
outputStream.close(); //如果可能,最终关闭


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.

解决方案

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

这篇关于通过apache-poi更改Excel中单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆