I am a real newb programer trying to make a small java program that edits spreadsheets for a friend using Apache Poi. Currently I go through the excel file using this code:
我是一个真正的新手程序员试图制作一个小型java程序,使用Apache Poi为朋友编辑电子表格。目前我使用以下代码浏览excel文件:
Sheet paxgs = rs1.getSheetAt(0);
for (Row row : paxgs) {
Cell secCell = row.getCell(1);
Cell firstCell = row.getCell(0);
if (firstCell!=null && firstCell.getCellType()==firstCell.CELL_TYPE_STRING)
}
The problem is that I don't know the exact number of sheets that the excel file is going to have. I tried :
问题是我不知道excel文件将具有的确切页数。我试过了 :
Workbook rs1 = WorkbookFactory.create(new FileInputStream(filename));
for (Sheet sheet : rs1)
but it didn't work so I was wondering if there is any way to go through all the sheet of a workbook.
但它没有用,所以我想知道是否有任何方法可以浏览所有工作簿。
2 个解决方案
#1
10
A Workbook
doesn't implement Iterable<Sheet>
like a Sheet
implements Iterable<Row>
, so you can't use an "enhanced" for loop. But you can loop through the Sheet
objects yourself, with a traditional for loop. Use getNumberOfSheets()
to find the number of sheets, and getSheetAt
to retrieve the individual Sheet
objects.
工作簿不实现Iterable
for (int i = 0; i < workbook.getNumberOfSheets(); i++)
{
Sheet sheet = workbook.getSheetAt(i);
// Process your sheet here.
}
#2
-1
The workbook contains a protected field "_sheets" which is of type java.util.List so:-
该工作簿包含一个受保护的字段“_sheets”,其类型为java.util.List,因此: -
rs1._sheets.size();
Should get you the number of sheets. You should also be able to iterate through this list.
应该得到你的床单数量。您还应该能够遍历此列表。
#1
10
A Workbook
doesn't implement Iterable<Sheet>
like a Sheet
implements Iterable<Row>
, so you can't use an "enhanced" for loop. But you can loop through the Sheet
objects yourself, with a traditional for loop. Use getNumberOfSheets()
to find the number of sheets, and getSheetAt
to retrieve the individual Sheet
objects.
工作簿不实现Iterable
for (int i = 0; i < workbook.getNumberOfSheets(); i++)
{
Sheet sheet = workbook.getSheetAt(i);
// Process your sheet here.
}
#2
-1
The workbook contains a protected field "_sheets" which is of type java.util.List so:-
该工作簿包含一个受保护的字段“_sheets”,其类型为java.util.List,因此: -
rs1._sheets.size();
Should get you the number of sheets. You should also be able to iterate through this list.
应该得到你的床单数量。您还应该能够遍历此列表。