简体   繁体   English

无法从具有POI的STRING单元中获取NUMERIC值

[英]Cannot get a NUMERIC value from a STRING cell with POI

To read columns from excel file using POI and java I do: 要使用POI和Java从e​​xcel文件中读取列,请执行以下操作:

package main;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadExcel {

 public static void main(String[] args) {

try {

    InputStream fs = new FileInputStream("/.../ListProducts.xls");
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);



    Map<String, Integer> map = new HashMap<String,Integer>(); //Create map
    HSSFRow row = sheet.getRow(0); //Get first row
    //following is boilerplate from the java doc
    short minColIx = row.getFirstCellNum(); //get the first column index for a row
    short maxColIx = row.getLastCellNum(); //get the last column index for a row
    for(short colIx=minColIx; colIx<maxColIx; colIx++) { //loop from first to last index
    HSSFCell cell = row.getCell(colIx); //get the cell
    map.put(cell.getStringCellValue(),cell.getColumnIndex()); //add the cell contents (name of column) and cell index to the map
    }

    List<ReportRow> listOfDataFromReport = new ArrayList<ReportRow>();
    for(int x = 1; x<=sheet.getPhysicalNumberOfRows(); x++){
     ReportRow rr = new ReportRow(); 
     HSSFRow dataRow = sheet.getRow(x); 

     int idxForColumn1 = map.get("Id"); 
     int idxForColumn2 = map.get("Name"); 
     int idxForColumn3 = map.get("Price"); 

     HSSFCell cell1 = dataRow.getCell(idxForColumn1); 
     HSSFCell cell2 = dataRow.getCell(idxForColumn2); 
     HSSFCell cell3 = dataRow.getCell(idxForColumn3);  

     rr.setColumn1(cell1.getNumericCellValue()); 
     rr.setColumn2(cell2.getNumericCellValue());
     rr.setColumn3(cell3.getNumericCellValue());

     listOfDataFromReport.add(rr);

    }

} catch (Exception e) {
    System.out.println(e.getMessage());
}
}

I notice that variables of setColumn1,setColumn2,setColumn3 methods are double . 我注意到setColumn1,setColumn2,setColumn3方法的变量是double

I get the following error when I try to run the program to read data: 当我尝试运行程序以读取数据时,出现以下错误:

     Exception in thread "main" java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:654)
at org.apache.poi.hssf.usermodel.HSSFCell.getNumericCellValue(HSSFCell.java:679)

Any suggestion please, I have a all cells in excel file with numeric type... 请提出任何建议,我在excel文件中的所有单元格都带有数字类型...

What you want to do is use the DataFormatter class. 您要做的是使用DataFormatter类。 You pass this a cell, and it does its best to return you a string containing what Excel would show you for that cell. 您为此传递一个单元格,它会尽力返回一个字符串,其中包含Excel将对该单元格显示的内容。 If you pass it a string cell, you'll get the string back. 如果将字符串单元格传递给它,则将字符串取回。 If you pass it a numeric cell with formatting rules applied, it will format the number based on them and give you the string back. 如果您向其传递一个应用了格式设置规则的数字单元格,它将根据它们格式化数字并返回字符串。

For your case, If you ask DataFormatter to format those cells, it'll give you back a string with the double string in it. 对于您的情况,如果您要求DataFormatter格式化这些单元格,它将返回一个带有双精度字符串的字符串。

Try to use 尝试使用

DataFormatter formatter = new DataFormatter();
String str = formatter.formatCellValue(cell1);
double dnum = Double.parseDouble(str );
System.out.println("In formated Cell Value--" + dnum);
rr.setColumn1(dnum);

for more reference let's have a look to these links, i think it will help you. 要获取更多参考,请看一下这些链接,我认为它将对您有所帮助。 http://massapi.com/class/da/DataFormatter.html http://massapi.com/class/da/DataFormatter.html

If you are sure 3 values are numeric, you can modify it's types before reading like this: 如果确定3个值是数字,则可以在读取之前修改它的类型:

HSSFCell cell1 = dataRow.getCell(idxForColumn1); 
HSSFCell cell2 = dataRow.getCell(idxForColumn2); 
HSSFCell cell3 = dataRow.getCell(idxForColumn3);  

cell1.setCellType(CellType.NUMERIC);
cell2.setCellType(CellType.NUMERIC);
cell3.setCellType(CellType.NUMERIC);

rr.setColumn1(cell1.getNumericCellValue()); 
rr.setColumn2(cell2.getNumericCellValue());
rr.setColumn3(cell3.getNumericCellValue());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM