簡體   English   中英

無法從具有POI的STRING單元中獲取NUMERIC值

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

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

我注意到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文件中的所有單元格都帶有數字類型...

您要做的是使用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

如果確定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