在jsp页面
<html>
<body>
<head>
<script type="text/javascript">
function fileChange(input){
var fileName = input.value;
if(fileName.length > 1 && fileName ) {
var ldot = fileName.lastIndexOf(".");
var type = fileName.substring(ldot + 1);
if(type=="xls" || type=="xlsx") {
$("#type").val(type);
var form=document.getElementById("form_query");
form.method = 'post';
form.action = 'config/department/importExcle';
form.submit();
}else{
alert("文件格式不准确!");
input.outerHTML=input.outerHTML.replace(/(value=\").+\"/i,"$1\"")
}
}
};
</script>
</head>
<table>
<tr>
<td>
<input type="file" id="excleFile" name="excleFile"onchange="fileChange(this);" style="display:none"/>
<button onclick="$('input[id=excleFile]').click();" class="save-btn">导入</button>
</td>
<td>
<button onclick="exportDepart()" class="save-btn">导出</button>
</td>
</tr>
</table>
</body>
</html>
本人用的struts2 的框架,在jsp页面写好以后,数据会通过struts.xml 找到action中对应的方法,在action方法中的具体代码如下:
//导入科室信息
public String readXls() throws IOException{
try {
Workbook workBook=null;
InputStream is = new FileInputStream(excleFile);
String type = request.getParameter("type");
if(type.toLowerCase().endsWith("xls")){
workBook=new HSSFWorkbook(is);
}
if(type.toLowerCase().endsWith("xlsx")){
workBook= new XSSFWorkbook(is);
}
//调用service层方法,把数据传到service层处理。
departmentsService.readXls(workBook);
return SUCCESS;
} catch (FileNotFoundException e) {
log.error(e);
return ERROR;
}
}
接在在service实现层进行处理,具体代码如下:
@Override
public List<DepartmentVO> readXls(Workbook workBook) {
try {
DepartmentVO departVO=null;
for(int numSheet=0;numSheet<=workBook.getNumberOfSheets();numSheet++){
Sheet sheet=workBook.getSheetAt(numSheet);
if(sheet==null){
continue;
}
//处理excel表格中的数据,其实很简单对应好就可以了。
for(int rowNum=1;rowNum<=sheet.getLastRowNum();rowNum++){
Row row=sheet.getRow(rowNum);
if(row!=null){
departVO=new DepartmentVO();
Cell dId=row.getCell(1);
Cell dName=row.getCell(2);
//Cell pId=row.getCell(2);
//Cell dStatus=row.getCell(3);
Cell dInfo=row.getCell(4);
Cell dAddr=row.getCell(5);
Cell dPhone=row.getCell(6);
Cell dNo=row.getCell(0);
departVO.setDepartmentName(ExcleUtils.getValue(dName));
departVO.setDepartmentId(ExcleUtils.getValue(dId));
departVO.setDisplay_no(ExcleUtils.getValue(dNo));
departVO.setParentId("0");
departVO.setPhone(ExcleUtils.getValue(dPhone));
departVO.setDepartmentAddr(ExcleUtils.getValue(dAddr));
departVO.setIntroduction(ExcleUtils.getValue(dInfo));
departVO.setDepartment_status(1);
saveOrUpdaeDepartment(departVO);
}
}
}
} catch (Exception e) {
log.error(e);
}
return null;
}
@Override
public ServiceResult saveOrUpdaeDepartment(DepartmentVO d) {
try{
departmentDAO.saveOrUpdaeDepartment(d);
return ServiceResult.getSucInstance();
}catch(DataAccessException dacEx){
ServiceResult sr = ServiceResult.getFailedInstance(""
, "系统异常,更新科室失败!");
return sr;
}catch(Exception ex){
log.error(null, ex);
ServiceResult sr = ServiceResult.getFailedInstance(""
, "参数错误,更新科室失败!");
return sr;
}
}
剩下的就不写了,剩下就是封装好的数据传到bean中存到数据库的操作。我代码的例子可能不是很好,是将科室信息导入数据库的。所以各位看官轻喷嘿嘿
欢迎大家关注个人公众号
分享各种学习资料,包含java,linux,大数据等。资料包含视频文档以及源码,仅供学习交流和分享,不涉及任何商业用途。同时分享本人及投递的优质技术博文。