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

查看:61
本文介绍了如何遍历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天全站免登陆