猿问

无法从数字单元格java中获取文本值

当我尝试读取 excel 表时,我遇到了这个代码问题,如果它包含它将读取的文本,但它不读取它的数字并给我这个错误 Cannot get a text value from a numeric cell java


public static void main(String[] args) throws IOException, Exception {

    /*WebDriver dr;

    // TODO Auto-generated method stub

         dr = new FirefoxDriver();

        dr.get("https://www.example.com/");

        ArrayList<String> username=readExcelData(0);

        ArrayList<String> pass=readExcelData(1);

        for(int i=0; i<username.size();i++) {

        dr.findElement(By.id("email")).sendKeys(username.get(i));

        dr.findElement(By.id("pass")).sendKeys(pass.get(i));

        dr.findElement(By.id("loginbutton")).click();

        Thread.sleep(6000);

        dr.findElement(By.id("email")).clear();

        dr.findElement(By.id("pass")).clear();

        }*/


// it work with txt 

//readExcelData(0);

// it will not work with the number 

//readExcelData(1);


}


public static  ArrayList<String> readExcelData(int colNo) throws IOException {

    FileInputStream fis= new FileInputStream("C:\\dat.xls");

    HSSFWorkbook wb=new HSSFWorkbook(fis);

    HSSFSheet s=wb.getSheet("sh1");

    Iterator<Row> rowIterator=s.iterator();

    rowIterator.next();

    ArrayList<String> list=new ArrayList<String>();

    while (rowIterator.hasNext()) {


        list.add( rowIterator.next().getCell(colNo).getStringCellValue());

    }

    System.out.println("List :::"+list);

    return list;

}


开满天机
浏览 220回答 1
1回答

慕哥9229398

试试下面的代码:public static&nbsp; ArrayList<String> readExcelData(int colNo) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fis= new FileInputStream("C:\\dat.xls");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HSSFWorkbook wb=new HSSFWorkbook(fis);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HSSFSheet s=wb.getSheet("sh1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<Row> rowIterator=s.iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowIterator.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataFormatter formatter = new DataFormatter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (rowIterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String val = formatter.formatCellValue(rowIterator.next().getCell(colNo));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(val);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("List :::"+list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return list;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答