如何遍历Excel文件中的所有行和单元格 [英] How to loop through all the rows and cells in an excel file

查看:742
本文介绍了如何遍历Excel文件中的所有行和单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用foreach来遍历excel文件中的所有单元格,以便设置单个前景色.这就是我到目前为止所拥有的.

I want to use foreach to iterate through all the cells in my excel file in order to set a single foreground color. This is what I have so far.

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet = wb.getSheetAt(0);

for (HSSFRow myrow : sheet){
    for (HSSFCell mycell : myrow){
        //set foreground color here
    }
}

问题出在我得到的语句for (HSSFRow myrow : sheet)for (HSSFCell mycell : myrow):

The problem is for the statements for (HSSFRow myrow : sheet) and for (HSSFCell mycell : myrow) I am getting:

只能遍历java.lang.Iterable

我检查了HSSFSheetHSSFRow-它们分别实现了java.lang.Iterable(Row)java.lang.Iterable(Cell).

I checked HSSFSheet and HSSFRow - they implement java.lang.Iterable(Row) and java.lang.Iterable(Cell) respectively.

推荐答案

尝试一下.可以编译

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet = wb.getSheetAt(0);

for (Row myrow : sheet) {
    for (Cell mycell : myrow) {
        //set foreground color here
    }
}

我正在使用 POI 3.7稳定

这篇关于如何遍历Excel文件中的所有行和单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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