我在使用Apache POI时遇到了一个excel问题.我可以跨行阅读,但有时我处于某种情况,我只想阅读一个特定的列.
因此,只能读取任何特定的列,只有“A”列或“C”列.
我正在使用Java语言.
解决方法
heikkim是对的,这里是一些从我的一些代码改编的示例代码:
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Row;
- ...
- for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
- row = sheet.getRow(rowIndex);
- if (row != null) {
- String cellValueMay = null;
- for (int colIndex = 0; colIndex < colCount; colIndex++) {
- if (colIndex == theColIndexYouWant) {
- cell = row.getCell(colIndex);
- if (cell != null) {
- // Found column and there is value in the cell.
- cellValueMaybeNull = cell.getStringCellValue();
- break;
- }
- }
- // Do something with the cellValueMaybeNull here ...
- }
- }
对于colCount,使用像row.getPhysicalNumberOfCells()