![](/img/trans.png)
[英]Read and write data from particular column in excel file dynamically using java apache poi
[英]Error when read Column value from excel with java using POI
我需要阅读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.getStringCellValue());
rr.setColumn2(cell2.getStringCellValue());
rr.setColumn3(cell3.getStringCellValue());
listOfDataFromReport.add(rr);
}
for(int j = 0; j< listOfDataFromReport.size();j++){
System.out.println("Column 1 Value: " + listOfDataFromReport.get(j).getColumn1());
//etc...
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
当我运行程序时,我在输出中得到了这个:
null
我已经在正确的目标位置并具有正确的列名的excel文件。
编辑
当我添加e.printStackTrace();
,我得到这个:
java.lang.NullPointerException
at main.ReadExcel.main(ReadExcel.java:51)
null
EDIT2
我注意到setColumn1,setColumn2,...
方法的变量是double
。
我这样做:
rr.setColumn1(cell1.getNumericCellValue());
rr.setColumn2(cell2.getNumericCellValue());
rr.setColumn3(cell3.getNumericCellValue());
当我尝试运行程序以读取数据时,出现以下错误:
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)
好吧,我和@maytham-ɯɐɥʇʎɐɯ的看法有点不同。 假设列名称在您的代码中正确,我认为这可能是问题所在:
for(int x = 1; x<=sheet.getPhysicalNumberOfRows(); x++){
假设物理行的数量为3
,所以它们的编号分别为0, 1, 2
并且您要迭代到3
,因此,当您稍后尝试获取该单元格时,它会抛出NullPointerException
,因为没有这样的行。
尝试做x < sheet.getPhysicalNumberOfRows();
我之前提到的for
循环中的(小于,不小于/等于)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.