POI修改Excel的数据

java操作poi怎么更改excel中的数据
如题 现在要更改excel表中的数据 在网上找到个方法 按照他的写法数据没有修改成功

Java code
public static void test() {
        String fileToBeRead = "C:\\project_TMP\\testPrj\\新規.xls"; // excel位置
        int coloum = 3; // 比如你要获取第三列
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
                    fileToBeRead));
            HSSFSheet sheet = workbook.getSheet("Sheet1");

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                HSSFRow row = sheet.getRow((short) i);
                if (null == row) {
                    continue;
                } else {
                        HSSFCell cell = row.getCell((short) 3);
                        if (null == cell) {
                            continue;
                        } else {
                            System.out.println(cell.getStringCellValue());
                            int temp = Integer.parseInt(cell.getStringCellValue());
                            cell.setCellValue(temp + 1);
                        }
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



这个代码是网上的 我按照他的写 但是操作后 数据还是原来的 没有更改 请大神教教 谢谢

------解决方案--------------------
修改文件最后还需要通过IO流操作来保存更改,这其实是很关键的一步,你代码里面没有IO的关闭操作,导致了数据的修改没有保存 

------解决方案--------------------
修改完cell的值 还需要保存一下excel
Java code
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ChangeCell {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        String fileToBeRead = "C:\\exp.xls"; // excel位置
        int coloum = 1; // 比如你要获取第1列
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
                    fileToBeRead));
            HSSFSheet sheet = workbook.getSheet("Sheet1");

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                HSSFRow row = sheet.getRow((short) i);
                if (null == row) {
                    continue;
                } else {
                    HSSFCell cell = row.getCell((short) coloum);
                    if (null == cell) {
                        continue;
                    } else {
                        System.out.println(cell.getNumericCellValue());
                        int temp = (int) cell.getNumericCellValue();
                        cell.setCellValue(temp + 1);
                    }
                }
            }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(fileToBeRead);
                workbook.write(out);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值