简体   繁体   English

在页面对象模型中读取Excel数据

[英]Read excel data in page object model

Using windows-7 and keep getting errors to write code to ready excel Trying to read excel data file in java maven Keep getting error on line#49 sheet= book.getSheet(sheetname); 使用Windows-7并不断出错,以将代码编写为现成的excel尝试在java maven中读取excel数据文件不断在第49行上出错。sheet = book.getSheet(sheetname); I have added all dependencies and imported but still can not clear this error. 我已经添加了所有依赖项并导入了,但是仍然无法清除此错误。

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;

    }


}

try to change the import of Sheet class with org.apache.poi.ss.usermodel.Sheet 尝试使用org.apache.poi.ss.usermodel.Sheet更改Sheet类的导入

what you use right now is Sheet for Powerpoint Document. 您现在使用的是Powerpoint文档的图纸。 here the reference of the library you use right now: https://www.oschina.net/uploads/doc/poi-3.1-FINAL/org/apache/poi/hslf/model/Sheet.html 这里是您现在使用的库的参考: https : //www.oschina.net/uploads/doc/poi-3.1-FINAL/org/apache/poi/hslf/model/Sheet.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM