这几天改个 问题,读取excel97的没问题,读取07的出现报错。后来网上查了下原因是07是用什么xml技术。。。只能从新找方法。
以前用
HSSFWorkbook(new FileInputStream(target));//转03版本
现在又加了个
new XSSFWorkbook(target);//转07版本
POI3.7-版本jar包及源码地址:
http://download.csdn.net/detail/hateson/3946267
public String getExportExcleList(String target, List<Object> list,
String[] condition) {
StringBuilder strBuilder = new StringBuilder("");
Workbook workbook;
if (StringUtils.isEmpty(strBuilder.toString())) {
} else {
strBuilder.delete(0, strBuilder.length() - 1);
}
try {
try {
workbook = (Workbook) new XSSFWorkbook(target);//转07版本
} catch (Exception ex) {
workbook = (Workbook) new HSSFWorkbook(new FileInputStream(//转03版本
target));
}
Sheet sheet = workbook.getSheetAt(0);
Cell excleTitle = sheet.getRow(1).getCell((short) 0);
int rows = sheet.getPhysicalNumberOfRows();
if (rows < 10) {
strBuilder.append("导入表的记录行数少于为10行");
}
for (int r = 3; r < rows; r++) {
Row row = sheet.getRow(r);
if (row == null)
break;
SplBean bean = new SplBean();
bean.setNd(condition[0]);
bean.setYf(condition[1]);
Cell jhhCell = row.getCell((short) COLMNSINDEX[0]);
if (jhhCell == null) {
break;
}
bean.setDw(getString(jhhCell));
jhhCell = row.getCell((short) COLMNSINDEX[1]);
if (!"".equals(getString(jhhCell))) {
bean.setJsdw(getString(jhhCell));
}
jhhCell = row.getCell((short) COLMNSINDEX[2]);
if (!"".equals(getString(jhhCell))) {
bean.setBywc(Double.valueOf(getString(jhhCell, 4)));
}
jhhCell = row.getCell((short) COLMNSINDEX[3]);
if (!"".equals(getString(jhhCell))) {
bean.setByzlj_jh(Double.valueOf(getString(jhhCell, 4)));
}
jhhCell = row.getCell((short) COLMNSINDEX[4]);
if (!"".equals(getString(jhhCell))) {
bean.setByzlj_sj(Double.valueOf(getString(jhhCell, 4)));
}
jhhCell = row.getCell((short) COLMNSINDEX[5]);
if (!"".equals(getString(jhhCell))) {
bean.setByzlj_bfb(Double.valueOf(getString(jhhCell, 4)));
}
list.add(bean);
}
strBuilder.append("year=" + condition[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
strBuilder.append("文件无法找到错误");
} catch (IOException e) {
e.printStackTrace();
strBuilder.append("文件无法读写错误");
} catch (Exception e) {
e.printStackTrace();
strBuilder.append("系统出现异常");
}
return strBuilder.toString();
}
注意jar包冲突,如果出现org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException。检查是否有xbean.xml,删除一般都可以解决。
其他帮助学习地址: