0

org.apache.poi.ss.usermodel の XSSFWorkbook を使用して Excel ファイルを読み取ろうとしています。Sheet メソッドの 1 つからデータを取得しようとすると、何らかの理由で Null Pointer 例外が発生します。私は Java プログラミングを始めたばかりで、非常に基本的なことかもしれません。

import org.apache.poi.ss.usermodel.*;
public class ReadFile {

public static void main(String[] args) throws InterruptedException, IOException {

    File f = new File("C:/Users/munish/Documents/Training/Selenium/sample.xlsx");
    FileInputStream file = new FileInputStream(f);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    Sheet sheet = workbook.getSheetAt(0);

    int rowsCount = sheet.getPhysicalNumberOfRows();
    System.out.println("Total number of rows: " + (rowsCount + 1));
    for (int i = 0; i <= rowsCount; i++) {
        Row row = sheet.getRow(i);
        int colCounts = row.getPhysicalNumberOfCells();
        System.out.println("Total number of Cols: " + colCounts);
        for (int j = 0; j < colCounts; j++) {
            Cell cell = row.getCell(j);
            System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
        }
    }
4

1 に答える 1

-2

ループforは次のようになります。

for (int i = 0; i < rowsCount; i++)

いいえ

for (int i = 0; i <= rowsCount; i++)
于 2014-02-20T20:17:06.983 に答える