无法使用Apache POI读取Excel数值

时间:2018-05-21 11:17:26

标签: java apache selenium-webdriver apache-poi

您好我正在尝试在执行时读取Excel数据。在我将所有数据作为字符串发送到Excel工作表之前我的代码工作正常但是经过一些更改后我需要使用相同的代码读取一个数字列。但我收到错误

java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
        at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1062)
        at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:395)
        at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:347)





This is my code .

public String[][] readFileAndExcelData(String sheetName) {
        int maxLAs = 1000;
        int maxRegs = 1000;
        String listUsers[][] = new String[maxLAs+1][9];
        try {
            //-Load the properties
            System.out.println("\n" + "---------------------------------------------------------------------------------"+ "\r\n");
            System.out.println("Read in PROPERTIES file"+ "\r\n");
            System.out.println("---------------------------------------------------------------------------------"+ "\r\n");

            Properties prop = new Properties();
            InputStream input = null;

            String inputFS = getAutomationInputDataPath()+"//Validation.properties";
            input = new FileInputStream(inputFS);
            prop.load(input);

            //-Input file with test data 
            String userAuthenticationFile = prop.getProperty("users_test_file");
            System.out.println("\t" + "userAuthenticationFile =" + userAuthenticationFile + "\r\n");

            //-Load the path of the resources folder, containing files to be tested 
            String resourcesPath = getAutomationInputDataPath()+"/";
            System.out.println("\t" + "resourcesPath =" + resourcesPath + "\r\n");

            System.out.println("\n" + "---------------------------------------------------------------------------------"+ "\r\n");
            System.out.println("Read in INPUT file describing 051 User Authentication"+ "\r\n");
            System.out.println("---------------------------------------------------------------------------------"+ "\r\n");

            String inputDataFS = resourcesPath + userAuthenticationFile;
            InputStream inputData = null;
            inputData = new FileInputStream(inputDataFS);

            Workbook workbook = new XSSFWorkbook(inputData);        
            Sheet lasSheet = workbook.getSheet(sheetName);      
            Iterator<Row> rowIterator = lasSheet.iterator();

            //-Maximum maxLAs learning activities may be specified, at this time
            //-First row must contain labels

            int numberUserAuthentication = 0;
            int r = 0;
            int c = 0;
            while (rowIterator.hasNext()) {
                Row nextRow = rowIterator.next();                                
                Iterator<Cell> cellIterator = nextRow.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    listUsers[r][c] =cell.getStringCellValue();
                    c = c + 1;
                }
                r = r + 1;
                c = 0;
            }

            workbook.close();
            inputData.close();
            numberUserAuthentication = r-1;
            System.out.println("\t" + "Completed reading Excel file that describes User Authentication list.  # LAs = " + numberUserAuthentication + "\r\n");

        }catch (Exception e) {      
            e.printStackTrace();
        }
        return listUsers;
    }

任何人都可以帮助我,我想阅读完整的Excel数据。我总共有6张纸,在每张纸上我都有行和列中的多个数据。 无法从Excel工作表中读取数字数据。

0 个答案:

没有答案