使用apache的POI读取excel文件首先准备所需jar包:
dom4j-1.6.1.jar
poi-3.10-FINAL-20140208.jarpoi-examples-3.10-FINAL-20140208.jar
poi-ooxml-3.10-FINAL-20140208.jar
poi-ooxml-schemas-3.10-FINAL-20140208.jar
xmlbeans-2.3.0.jar
下面是我在项目中读取excel表的代码
StringBuffer errorInfo = new StringBuffer();
int length = contentFileName.lastIndexOf(".");
String suffix = contentFileName.substring(length + 1);
//文件是通过sturts2的from表单上传的文件
if (content != null) {
Workbook workbook = null;
List<SceneContent> sceneContents = null;
try {
try {
InputStream in = new FileInputStream(content);
if ("xls".equals(suffix)) {
workbook = (Workbook) new HSSFWorkbook(in);
} else if ("xlsx".equals(suffix)) {
workbook = new XSSFWorkbook(in);
}
} catch (IOException e) {
BasalException ex = new BasalException(BasalException.NO,
"读取Excel文件出错");
throw ex;
}
} catch (Exception e) {
errorInfo.append("读取Excel文件出错<br><br>");
}
try {
StringBuffer colError = null;
Sheet sheet = workbook.getSheetAt(0);
sceneContents = new LinkedList<SceneContent>();
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Row xssfRow = sheet.getRow(rowNum);
if (xssfRow != null) {
SceneContent sc = new SceneContent();
if ((xssfRow.getCell(0) == null)
|| (xssfRow.getCell(0).toString().trim()
.equals(""))) {
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容标题输入有误,不能为空,请检查!</span><br>");
} else {
sc.setTitle(xssfRow.getCell(0).toString());
}
if ((xssfRow.getCell(1) == null)
|| (xssfRow.getCell(1).toString().trim()
.equals(""))) {
if ((xssfRow.getCell(2) == null)
|| (xssfRow.getCell(2).toString()
.trim().equals(""))) {
// workbook.close();
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容和链接不能全为空,请检查!</span><br>");
} else {
sc.setLink(xssfRow.getCell(2).toString());
sc.setType("0");// 如果链接存在,类型设置为0
}
} else {
sc.setDescription((xssfRow.getCell(1).toString()));// 如果内容栏不为空,不在判断链接栏是否存在数据,默认只存储内容
sc.setType("1");// 如果内容存在,类型设置为1
}
if ((xssfRow.getCell(3) == null)
|| (xssfRow.getCell(3).toString().trim()
.equals(""))) {
// workbook.close();
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容推送次数输入有误,不能为空,请检查!</span><br>");
} else {
String pushNum = xssfRow.getCell(3).toString()
.trim();
int index = pushNum.lastIndexOf(".");
if(index>0){
pushNum = pushNum.substring(0,index);
}
if ("0".equals(pushNum) || "1".equals(pushNum)) {
sc.setPushNum(Integer.parseInt(pushNum));
} else {
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容推送次数输入有误,应为阿拉伯数字0或1,请检查!</span><br>");
}
}
if ((xssfRow.getCell(4) == null)
|| (xssfRow.getCell(4).toString().trim()
.equals(""))) {
// workbook.close();
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容生效时间输入有误,不能为空,请检查!</span><br>");
} else {
String date = xssfRow.getCell(4).toString()
.trim();
try {
Date d = sd.parse(date);
sc.setEffectiveTime(d);
} catch (Exception e) {
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容生效时间格式有误,请检查!</span><br>");
}
}
if ((xssfRow.getCell(5) == null)
|| (xssfRow.getCell(5).toString().trim()
.equals(""))) {
// workbook.close();
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容失效时间输入有误,不能为空,请检查!</span><br>");
} else {
String date = xssfRow.getCell(5).toString()
.trim();
try {
Date d = sd.parse(date);
sc.setFailureTime(d);
} catch (Exception e) {
if (colError == null)
colError = new StringBuffer();
colError.append("<span style='color:red'>错误:内容失效时间格式有误,请检查!</span><br>");
}
}
sc.setDelFlag("0");
sc.setIsUse("Y");
if (colError == null) {
sceneContents.add(sc);
errorInfo.append("导入第").append(rowNum)
.append("条记录成功!<br><br>");
} else {
errorInfo.append("导入第").append(rowNum)
.append("条记录失败,错误信息如下:<br>");
errorInfo.append(colError).append("<br>");
}
} else {
colError.append("<span style='color:red'>错误:excel表为为空,请检查!</span><br>");
}
}
try {
if (sceneContents != null && sceneContents.size() > 0) {
this.sceneContentService
.saveSceneContentList(sceneContents);
}
} catch (Exception e) {
// workbook.close();
BasalException ex = new BasalException(
BasalException.ERROR, "保存数据库出错", e.getCause());
throw ex;
}
} catch (Exception e) {
errorInfo.append("Excel表格式有问题<br><br>");
}
}
this.message = errorInfo.toString();
return SUCCESS;