使用Windows-7并不断出错以编写代码以准备好Excel
   尝试在Java Maven中读取Excel数据文件继续出现错误
    line#49 sheet = book.getSheet(sheetname);我已经添加了所有依赖项并导入了,但是仍然无法清除此错误。

package com.newTour.qa.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.newTour.qa.Base.TestBase;


public class TestUtil extends TestBase {


public static String TESTDATA_SHEET_PATH = "C:\\Users\\shahgee\\newtour.qu\\src\\main\\java\\"
            + "com\\qa\\newtour\\testdata\\MercutyTourTestData.xlsx" ;

    static Workbook book;
    static Sheet sheet;



public static Object[][]getTestData(String sheetname){
    FileInputStream file = null;
    try {
        file = new FileInputStream(TESTDATA_SHEET_PATH);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

     try {
        book= WorkbookFactory.create(file);
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

     sheet = book.getSheet(sheetname);
     Object[][]data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

     for (int i =0; i <sheet.getLastRowNum();i++){

         for (int k =0;k <sheet.getRow(0).getLastCellNum(); k++){
             data[i][k]= sheet.getRow(i+1).getCell(k).toString();

         }
     }

     return data;

    }


}

最佳答案

尝试更改Sheet类的导入
org.apache.poi.ss.usermodel.Sheet

您现在使用的是Powerpoint文档的图纸。
这里是您现在使用的库的参考:
https://www.oschina.net/uploads/doc/poi-3.1-FINAL/org/apache/poi/hslf/model/Sheet.html

关于java - 在页面对象模型中读取Excel数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57983801/

10-12 17:42