按楼上的 改了以后 结果抛出异常 org.apache.poi.hssf.usermodel.HSSFRow.getCell(I)Lorg/apache/poi/hssf/usermodel/HSSFCell; ERROR [org.apache.struts.actions.DispatchAction] - <Dispatch[/standard] to metho d 'readExcel' returned an exception> java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
org.apache.poi.hssf.usermodel.HSSFRow.getCell(I)Lorg/apache/poi/hssf/usermodel/HSSFCell;
ERROR [org.apache.struts.actions.DispatchAction] - <Dispatch[/standard] to metho
d 'readExcel' returned an exception>
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
cell = row.getCell((short) 1);变成cell = row.getCell(1); 了 不需要强转成short型了,POI3.2版本这个取得cell的函数已经不用在转换成short了,直接就是一个整型。
你出现异常的的那个HSSFRow.getCell(I)是要循环取得变量?? 你先直接写1,不要写变量,试验下。正常来说应该没问题的,我就是用POI3.2写的,没有问题,你跟踪代码看看具体是那行的错误就清楚了。
我的部分代码如下:
/*判断完基本的容错信息后,就开始读取存储导入的信息了*/
String teamName = "";
String teamerName = "";
String re = "";
String teamNameTemp = "";
for (int i = firstNum + 1; i <= lastNum; i++) {
// 跳过第一行的标题
HSSFRow row = sheet.getRow(i);
// 如果出现空行,则跳过 ,直接读取下一行
if (row == null){
continue;
}
HSSFCell cell = null;
HSSFCell tempCell = null;
ComQueryManagerVO tempVo = new ComQueryManagerVO(); cell = row.getCell(1);
teamerName = cell.toString().trim();
tempVo.setTeamerName(teamerName);
cell = row.getCell(2);
re = cell.toString().trim();
tempVo.setRe(re);
cell = row.getCell(0);
teamName = cell.toString().trim();
//判断球队的名字不能相同
for(int j = i+1; j <= lastNum; j++){
HSSFRow rowConp = sheet.getRow(j);
if (rowConp == null){
continue;
}
tempCell = rowConp.getCell(0);
teamNameTemp = tempCell.toString().trim();
if(teamNameTemp.equals(teamName)){
errorCellConp += ("(" + (i) + ",A)")
+ ("(" + (j + 1) + ",A)"); }
}
if (errorCellConp.length() > 1) {
System.out.println("该单元格的字段不容许重复"+errorCellConp);
errorCellConp = "单元格读数据错误:坐标" + errorCellConp;
throw new ServiceException("该单元格的字段不容许重复");
}
tempVo.setTeamName(teamName);
reInfoList.add(tempVo);
}
return reInfoList;
}