Java 调用getcellData()时,从excel中不获取任何值-Selenium WebDriver

Java 调用getcellData()时,从excel中不获取任何值-Selenium WebDriver,java,apache-poi,Java,Apache Poi,这是我的excel utils类: package utility; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import or

这是我的excel utils类:

package utility;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.CellType;
public class ExcelUtils {
    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell cell;
    private static XSSFRow row;
    //This method is to set the File path and to open the Excel 
    file, Pass Excel Path and Sheetname as Arguments to this method
    public static void setExcelFile(String Path, String SheetName) throws Exception {
        try {
            // Open the Excel file
            FileInputStream ExcelFile = new FileInputStream(Path);
            // Access the required test data sheet
            ExcelWBook = new XSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        } catch (Exception e) {
            throw (e);
        }
    }
    //This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num
    public static String getCellData(int RowNum, int ColNum) throws Exception {
        try {
            String cellData = "";
            cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            cell.setCellType(CellType.STRING);
            cellData = cell.getStringCellValue();
            return cellData;
        } catch (Exception e) {
            return "undefined";
        }
    }
    //This method is to write in the Excel cell, Row num and Col num are the parameters
    public static void setCellData(String Result, int RowNum, int ColNum) throws Exception {
        try {
            row = ExcelWSheet.getRow(RowNum);
            cell = row.getCell(ColNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
            if (cell == null) {
                cell = row.createCell(ColNum);
                cell.setCellValue(Result);
            } else {
                cell.setCellValue(Result);
            }
            // Constant variables Test Data path and Test Data file name
            FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData + Constants.File_TestData);
            ExcelWBook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (Exception e) {
            throw (e);
        }
    }
}
下面是我调用getCellData从Excel获取值的脚本:

String cellData = ExcelUtils.getCellData(1, 1);
System.out.println("CellData :" + cellData);
以下是excel文件格式:

TestCaseName | Username | Password 
TC_01        | TestData |          
输出:

   Exception in thread "main" java.lang.NullPointerException
    at utility.ExcelUtils.getCellData(ExcelUtils.java:63)
    at testScripts.Category_creation.main(Category_creation.java:47)
这是我正在使用的。无法从excel文件中获取数据。我使用的是页面对象框架,因此excel utils文件只包含代码,并通过传递行和列号在testScript中获取数据。

POI 3.9

请添加cell.setCellTypeCell.cell\u TYPE\u字符串;单元格后=ExcelWSheet.GetRowNum.getCellColNum

i、 e

或者您可以使用此构造

switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;

                    default:
来源

并请重命名变量private static XSSFCell Cell;去牢房

更新1

POI 3.17您还可以取消对开关大小写块的注释,它适用于POI 3.17

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excel {

    private static XSSFSheet ExcelWSheet;

    private static XSSFWorkbook ExcelWBook;

    private static XSSFCell xCell;

    private static XSSFRow xRow;  

    public static void main(String... args) {

     try {
        InputStream is = readInputStreamFromFile();

        XSSFWorkbook myWorkBook = new XSSFWorkbook(is);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        ExcelWSheet = mySheet;

        System.out.println(getCellData(1, 0));
        System.out.println(getCellData(1, 1));

        is.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }


    public static String getCellData(int RowNum, int ColNum) throws Exception{

          try {

            String cellData = "";

            xCell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            xCell.setCellType(CellType.STRING);
            cellData = xCell.getStringCellValue();

//            switch (xCell.getCellTypeEnum()) {
//            case STRING:
//                //System.out.print(xCell.getStringCellValue() + "\t");
//                cellData = xCell.getStringCellValue();
//                break;
//            case NUMERIC:
//              cellData = String.valueOf(xCell.getNumericCellValue());
//                //System.out.print(xCell.getNumericCellValue() + "\t");
//                break;
//            case BOOLEAN:
//              cellData = String.valueOf(xCell.getBooleanCellValue());
//                //System.out.print(xCell.getBooleanCellValue() + "\t");
//                break;
//
//            default:
//              cellData = "undefined";
//            }

            return cellData;

            } catch (Exception e){

                return "undefined";

            }
    }

    private static InputStream readInputStreamFromFile() throws Exception {
        try {

            File f = new File("C:\\path to your file\\TestData.xlsx");

            InputStream is = new FileInputStream(f);
            try {
                return new ByteArrayInputStream(IOUtils.toByteArray(is));
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

}

删除[硒],因为这个问题与硒无关。您应该为用于访问Excel的任何库添加标记。您可能需要cell.getCellTypeEnum-取决于您使用的poi版本using@Kaustubh,请添加您的excel文件。我已经用ApachePOI版本3.9进行了测试,效果很好。您使用的poi版本是什么?@John:我已经附上了我正在使用的excel文件。poi版本是3。17@Kaustubh,我已更新我的答案,请参阅更新1@John:谢谢。但在获取getcellData的打印结果时,将其视为未定义。我已经更新了excel utils的全部代码。请看一看。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excel {

    private static XSSFSheet ExcelWSheet;

    private static XSSFWorkbook ExcelWBook;

    private static XSSFCell xCell;

    private static XSSFRow xRow;  

    public static void main(String... args) {

     try {
        InputStream is = readInputStreamFromFile();

        XSSFWorkbook myWorkBook = new XSSFWorkbook(is);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        ExcelWSheet = mySheet;

        System.out.println(getCellData(1, 0));
        System.out.println(getCellData(1, 1));

        is.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }


    public static String getCellData(int RowNum, int ColNum) throws Exception{

          try {

            String cellData = "";

            xCell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            xCell.setCellType(CellType.STRING);
            cellData = xCell.getStringCellValue();

//            switch (xCell.getCellTypeEnum()) {
//            case STRING:
//                //System.out.print(xCell.getStringCellValue() + "\t");
//                cellData = xCell.getStringCellValue();
//                break;
//            case NUMERIC:
//              cellData = String.valueOf(xCell.getNumericCellValue());
//                //System.out.print(xCell.getNumericCellValue() + "\t");
//                break;
//            case BOOLEAN:
//              cellData = String.valueOf(xCell.getBooleanCellValue());
//                //System.out.print(xCell.getBooleanCellValue() + "\t");
//                break;
//
//            default:
//              cellData = "undefined";
//            }

            return cellData;

            } catch (Exception e){

                return "undefined";

            }
    }

    private static InputStream readInputStreamFromFile() throws Exception {
        try {

            File f = new File("C:\\path to your file\\TestData.xlsx");

            InputStream is = new FileInputStream(f);
            try {
                return new ByteArrayInputStream(IOUtils.toByteArray(is));
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

}