我正在尝试使用 org.apache.poi.ss.usermodel 中的 XSSFWorkbook 来读取 Excel 文件。出于某种原因,当我尝试从其中一种工作表方法中提取数据时,我得到了一个空指针异常。我对 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());
}
}