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

Pau*_*rto 57 java apache-poi

我正在尝试使用Excel中的电子表格中的数据,但总是出现此错误,已尝试将工作表格式化为文本和数字,但错误仍然存​​在.

我看到一个人使用它解决cell.setCellType ( Cell.CELL_TYPE_STRING ) ;但我不知道我的代码在哪里适合这段.

WebElement searchbox = driver.findElement(By.name("j_username"));
WebElement searchbox2 = driver.findElement(By.name("j_password"));         

try {
    FileInputStream file = new FileInputStream(new File("C:\\paulo.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file);
    HSSFSheet sheet = workbook.getSheetAt(0);

    for (int i=1; i <= sheet.getLastRowNum(); i++){
        String j_username = sheet.getRow(i).getCell(0).getStringCellValue();
        String j_password = sheet.getRow(i).getCell(0).getStringCellValue();

        searchbox.sendKeys(j_username);
        searchbox2.sendKeys(j_password);
        searchbox.submit();  

        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
    }

    workbook.close();
    file.close();
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
Run Code Online (Sandbox Code Playgroud)

小智 48

在这种情况下,Formatter可以正常工作.

import org.apache.poi.ss.usermodel.DataFormatter;

FileInputStream fis = new FileInputStream(workbookName);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheet(sheetName);
DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(sheet.getRow(row).getCell(col));
list.add(val);   //Adding value to list
Run Code Online (Sandbox Code Playgroud)

  • 不过这东西对我不起作用,它的输出类似于:SUM(I38+D39+F39-G39-H39) (3认同)

Ana*_*oly 47

    Cell cell = sheet.getRow(i).getCell(0);
    cell.setCellType ( Cell.CELL_TYPE_STRING );
    String j_username = cell.getStringCellValue();
Run Code Online (Sandbox Code Playgroud)

UPDATE

好吧,正如在评论中所说的那样,尽管这样做有效,但从Excel的单元格中检索数据并不正确.

根据这里的手册:

如果您要做的是获取数字单元格的String值,请停止!这不是这样做的方法.相反,要获取数字或布尔或日期单元格的字符串值,请改用DataFormatter.

并根据DataFormatter API

DataFormatter包含用于格式化存储在Cell中的值的方法.当您需要完全按照Excel中显示的数据显示数据时,这对于报表和GUI演示非常有用.支持的格式包括货币,SSN,百分比,小数,日期,电话号码,邮政编码等.

因此,显示数字单元格值的正确方法如下:

 DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
 Cell cell = sheet.getRow(i).getCell(0);
 String j_username = formatter.formatCellValue(cell); //Returns the formatted value of a cell as a String regardless of the cell type.
Run Code Online (Sandbox Code Playgroud)


Gag*_*arr 8

Apache POI Javadocs中所述,您不应该使用cell.setCellType(Cell.CELL_TYPE_STRING)获取数字单元格的字符串值,因为您将丢失所有格式

相反,正如javadocs解释的那样,你应该使用DataFormatter

DataFormatter所做的是将表示单元格的浮点值与文件中存储的格式规则一起存储在文件中,并返回一个类似于单元格在Excel中的字符串.

所以,如果你是在一个单元格的字符串之后,看起来就像在Excel中查找一样,只需:

 // Create a formatter, do this once
 DataFormatter formatter = new DataFormatter(Locale.US);

 .....

 for (int i=1; i <= sheet.getLastRowNum(); i++) {
        Row r = sheet.getRow(i);
        if (r == null) { 
           // empty row, skip
        } else {
           String j_username = formatter.formatCellValue(row.getCell(0));
           String j_password =  formatter.formatCellValue(row.getCell(1));

           // Use these
        }
 }
Run Code Online (Sandbox Code Playgroud)

格式化程序将按原样返回String单元格,对于Numeric单元格,将样式的格式规则应用于单元格的编号


小智 7


cell.setCellType(Cell.CELL_TYPE_STRING);
在读取字符串值之前使用代码,它可以帮助您。
我正在使用 POI 版本 3.17 Beta1 版本,确保版本兼容性也..


小智 5

使用DataFormatter此问题已解决。感谢“Gagravarr”的初始帖子。

DataFormatter formatter = new DataFormatter();

String empno = formatter.formatCellValue(cell0);
Run Code Online (Sandbox Code Playgroud)