使用POI无法从字符串单元格中获取数字值

3

使用POI和Java读取Excel文件中的列,我会这样做:

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());
}
}

我注意到 setColumn1,setColumn2,setColumn3 方法的变量是 double

当我尝试运行程序读取数据时,出现了以下错误:

     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)

有什么建议吗?我在Excel文件中所有单元格都是数字类型...


您能否发布您的输入Excel文件。从列的名称“Name”、“Id”来看,它们似乎是“数值类型”。 - Vikas Sachdeva
2个回答

3
你需要使用DataFormatter类。将单元格传递给它,它会尽力返回一个包含Excel显示内容的字符串。如果你传递一个字符串单元格,你将得到该字符串。如果你传递一个应用了格式规则的数字单元格,它将根据这些规则格式化数字并返回一个字符串。
针对你的情况,如果你要求DataFormatter格式化那些单元格,它会返回包含双精度字符串的字符串。
请尝试使用:
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);

为了更多参考,让我们看看这些链接,我认为它会帮助你。 http://massapi.com/class/da/DataFormatter.html


2
如果你确定三个值都是数字,你可以在读取之前修改它们的类型,像这样:

如果您确定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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接