是否有人对Java操作Excel比较熟悉的求赐教?
解决方案 »
- 谁能告诉我www.hibernate.org里面的hibernatetools在哪里下载呀。直接给能下的链接。谢谢
- jsp隐式对象session的疑惑
- 求Hibernae的insert例子(Save & 配置到XML除外)
- 有人了解jive吗 帮个忙
- 请教一个设计问题?
- jbuilder9(weblogic8)中发布session Bean好使,可是发布Antity Bean就出错! 在线等 立马给分
- JSF(JavaServer Faces)将来肯定火爆,Struts?????
- cmp简单问题
- java socket实现两个客户端之间的聊天
- weblogic部署程序报500错误,但是在tomcat上部署没问题
- 用struts2开发,在表单提交时为什么找不到对应的action,配置文件一切正常,很苦脑。
- 有没有一个营销系统很好的架构
poi是apache旗下的开源项目,excel2003和2007都支持,而且还在维护
boolean isDelete) throws BusinessException {
if (!excelFile.exists()) {
throw new BusinessException("要读取的文件不存在");
} List<Map<String, Object>> chits = new ArrayList<Map<String, Object>>();
InputStream stream = null;
boolean errFlag = false;
try {
stream = new FileInputStream(excelFile);
Map<String, Integer> orgs = getOrgs(); // 所有部门
Workbook wb = null;
if(excelFile.getName().endsWith("xls")){
wb = new HSSFWorkbook(stream); //读取2003
}else{
wb = new XSSFWorkbook(stream); //读取2007
}
if (wb.getNumberOfSheets() == 0) {
throw new BusinessException("要导入的Excel文件中不存在数据");
}
Sheet sheet = wb.getSheetAt(0);
Set<String> numSet = new HashSet<String>();
int rowIndex = 1;
boolean isNext = true;
while(isNext){
int lineNo = rowIndex+1;
Row row = sheet.getRow(rowIndex);
Map<String, Object> chit = new HashMap<String, Object>();
if (row.getCell(0) == null || row.getCell(0).getStringCellValue().equals("")) {
throw new BusinessException("导入的代金券第" + lineNo + "行名称为空");
} else {
String name = row.getCell(0).getStringCellValue();
chit.put("name", name);
}
if (row.getCell(1) == null || row.getCell(1).getStringCellValue().equals("")) {
throw new BusinessException("导入的代金券第" + lineNo + "行条码为空");
}else{
String num = row.getCell(1).getStringCellValue();
numSet.add(num);
chit.put("num", num); // 代金券面值
}
if (row.getCell(2) == null || row.getCell(2).getNumericCellValue() == 0.0) {
throw new BusinessException("导入的代金券第" + lineNo + "行面值为空");
} else {
String faceValue = row.getCell(2).getNumericCellValue()+"";
BigDecimal DecFaceValue = new BigDecimal(faceValue);
chit.put("faceValue", DecFaceValue);
} // 所属部门编号
String orgName = row.getCell(3)!=null?row.getCell(3).getStringCellValue():"";
if (orgs.containsKey(orgName)) {
int orgId = orgs.get(orgName);
chit.put("orgId", orgId);
} else {
chit.put("orgId", null);
} chit.put("orgName",orgName); String indate = dateFormat(row.getCell(4).getDateCellValue());
if (indate == null) {
throw new BusinessException("导入的代金券第" + lineNo + "行有效期为空");
} else {
chit.put("indate", indate);
}
String onsetDate = dateFormat(row.getCell(5).getDateCellValue());
if (onsetDate == null) {
throw new BusinessException("导入的代金券第" + lineNo + "行生效时间为空");
} else {
chit.put("onsetDate", onsetDate);
}
chit.put("re", row.getCell(6)!=null?row.getCell(6).getStringCellValue():"");
chits.add(chit);
rowIndex = rowIndex + 1;
row = sheet.getRow(rowIndex);
if(row == null){
isNext = false;
rowIndex = rowIndex - 1;
}
} if (rowIndex > numSet.size()) {
throw new BusinessException("导入代金券条码不能重复");
}
} catch (BusinessException e) {
errFlag = true;
throw e;
} catch (Exception e) {
errFlag = true;
throw new BusinessException(e.getMessage());
} finally {
if (isDelete || errFlag) {
// 关闭文件流
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
} excelFile.delete();
}
} return chits;
}
poi是apache的开源项目,以OO为原则设计,并且持续维护,也支持最新版本的excel格式相对来说,还是比较推荐poi,jxl已经过时