简体   繁体   English

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

[英]Cannot get a text value from a numeric cell java

i have this problem with the code when i try to read the excel sheet if it contain an text it will read but the number it does't read it and give me this error Cannot get a text value from a numeric cell java 当我尝试阅读excel工作表时,如果它包含将要读取的文本,则代码存在此问题,但数字无法读取,并给我此错误。无法从数字单元格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;
}

Try below code: 试试下面的代码:

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>();
            DataFormatter formatter = new DataFormatter();

            while (rowIterator.hasNext()) {
                 String val = formatter.formatCellValue(rowIterator.next().getCell(colNo));
                list.add(val);
            }
            System.out.println("List :::"+list);
            return list;
        }

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

相关问题 无法从NUMERIC单元格获取STRING值,但单元格为“文本” - Cannot get a STRING value from a NUMERIC cell, but cell is “text” java.lang.IllegalStateException:无法从数字公式单元格获取文本值 - java.lang.IllegalStateException: Cannot get a text value from a numeric formula cell java.lang.IllegalStateException:无法从文本单元格获取数值 - java.lang.IllegalStateException: Cannot get a numeric value from a text cell 收到此错误:消息:java.lang.IllegalStateException:无法从数字单元格获取文本值 - Getting this error: Message: java.lang.IllegalStateException: Cannot get a text value from a numeric cell 线程“主”java.lang.IllegalStateException 中的异常:无法从数字单元格中获取文本值 - Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell 无法从数字单元格“Poi”中获取文本值 - Cannot get a text value from a numeric cell “Poi” 尽管添加了dateformatter,但仍无法从数字单元格中获取文本值 - Cannot get a text value from a numeric cell despite adding dateformatter 数据核-无法从数字单元格获取文本值 - Datanucleus - Cannot get a text value from a numeric cell java - 无法从 STRING 单元格异常中获取 NUMERIC 值 - java - Cannot get a NUMERIC value from a STRING cell Exception 无法从 NUMERIC 单元格中获取 STRING 值? - Cannot get a STRING value from a NUMERIC cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM