今天用poi操作excel 2007 的时候突然报错,出现poi不支持office 2007
后来换成更高版本的jar包后问题解决,步骤如下
1。加入3.6版本jar包 ,图片中3.2的是旧的jar包
2.更改程序,其实就是将低版本的类名中的H换成了X
XSSFWorkbook workbook;
List oraList = new ArrayList();
String custIds = "";
try {
workbook = new XSSFWorkbook(absPathName);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
String custAim = "";
XSSFRow row = sheet.getRow(i);
Map map = new LinkedHashMap();
if (row.getCell(0) != null) {
if (row.getCell(0).getCellType() == 1) {
custAim = row.getCell(0).getStringCellValue();
} else {
custAim = row.getCell(0).getNumericCellValue() + "";
if (custAim.equals("0.0")) {
custAim = "";
} else {
custAim = custAim.substring(0, custAim.length() - 2);
}
}
}
List list=imd.getOneCust(custAim);
map.put("公司名称", custAim);
if(list!=null && list.size()>0){
String cust_id=(String)((Map)list.get(0)).get("cust_id");
custIds=custIds+"'"+cust_id+"',";
map.put("状态", "成功");
}else{
map.put("状态", "失败");
map.put("原因", "数据库中没有找到该企业");
}
oraList.add(map);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
一个简单的例子
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(strPath);
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
String cell;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
cell = row.getCell(j).toString();
System.out.print(cell + "\t");
}
System.out.println("");
}
好了,问题搞定
jar在附件中