1.前台代码
<!DOCTYPE html>
<html decorator="empty">
<head>
<meta charset="utf-8" />
</head>
<body>
<header>
</header>
<section>
<div class="contant">
<div class="tab-content-body list_false">
<div class="row">
<div class="tab-content form panel panel-default">
<form id="thdnForm" class="form-horizontal form-validate"
autoComplete="off" action="/zjsb/fhq/zjsbcxryhz/edit.action"
method="post">
<div class="well panel-heading">
<div class="row panel-title">
<i class="fa fa-credit-card"></i> <a class="accordion-toggle"
data-toggle="collapse" data-parent="#accordion3"
href="#accordion3_1" data_id="zcdx_xmpt">
入驻企业自主创新成果及荣誉情况汇总表 -> 修改 <i
class="fa fa-angle-down pull-right"></i>
</a>
</div>
</div>
<div class="well panel-heading">
<div class="row panel-title">
<a class="accordion-toggle" data-toggle="collapse"
data-parent="#accordion3" href="#qycz123" data_id="qycz">入驻高新技术企业成长性情况明细表<i
class="fa fa-angle-down pull-right"></i></a>
</div>
</div>
<div id="qycz123" class="panel-collapse collapse in">
<jsp:include page="/fhq/zjsbcxryhz/qycz.jsp"></jsp:include>
</div>
</form>
</div>
</div>
</div>
<form action="importqycz" method="post" id="formQycz" name="formQycz"
enctype="multipart/form-data">
<div class="modal" id="modal-file2" tabindex="-1" role="dialog"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<input name="user_code" value="${user_code}" type="hidden" /><br>
<input name="user_name" value="${user_name}" type="hidden" /><br>
<input name="apply_id" value="${apply_id}" type="hidden" /><br>
<input name="id" value="${id}" type="hidden" /><br>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true"></button>
<h4 class="modal-title">请按模板导入excel数据!</h4>
</div>
<div class="modal-body">
<input type="file" name="pic" />
</div>
<div class="modal-footer">
<button type="button" class="btn blue" id="uploadqycz">上传</button>
<button type="button" class="btn gray" data-dismiss="modal">取消</button>
</div>
</div>
</div>
</div>
</form>
<div class="modal fade bs-modal-lg" id="ajax" role="dialog"
aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content" id="ajax_content1"></div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$("#uploadqycz").click(function() {
var url = "importqycz";
var user_code = document.formQycz.user_code.value;
var user_name = document.formQycz.user_name.value;
var apply_id = document.formQycz.apply_id.value;
var id = document.formQycz.id.value;
var pic = document.formQycz.pic.files[0];
var fm = new FormData();
fm.append('user_code', user_code);
fm.append('user_name', user_name);
fm.append('apply_id', apply_id);
fm.append('id', id);
fm.append('pic', pic);
$.ajax({
type : 'post',
url : url,
data : fm,
contentType : false,
processData : false,
success : function(qyczList) {
//alert(dates);
$('#modal-file2').modal('hide');
$("#qycz123").html(qyczList);//要刷新的div
},
error : function(fail) {
$.error("请您检查Excel内容格式!", false);
}
});
});
function removeqycz(e1, id) {
bootbox.dialog({
"message" : "即将执行删除操作,请确定。",
"title" : "删除确认!",
"buttons" : {
"danger" : {
"label" : "删除",
"className" : "btn red",
"callback" : function() {
$.ajax({
"url" : "deleteqycz.action",
"data" : {
"id" : id
},
"success" : function(e) {
$.success("删除成功", false);
e1.parentNode.parentNode.remove();
}
});
}
},
"cancel" : {
"label" : "取消",
"className" : "btn default"
}
}
});
return false;
}
</script>
</body>
</html>
2.后台代码
@RequestMapping(value = "importqycz", method = RequestMethod.POST)
public ModelAndView importqycz(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws Exception {
ModelAndView mav = new ModelAndView();
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
Map<String, Object> map = new HashMap<String, Object>();
// 解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
InputStream in = null;
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照传统方式获取数据
return mav;
}
try {
// 4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
// 如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
// 解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
map.put(name, value);
} else {// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
String filename = item.getName();
String filedsname = item.getFieldName();
if (filename == null || filename.trim().equals("")) {
continue;
}
// 注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:
// c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
// 处理获取到的上传文件的文件名的路径部分,只保留文件名部分
filename = filename
.substring(filename.lastIndexOf("\\") + 1);
filename = filename.substring(0, filename.lastIndexOf("."));
// 获取item中的上传文件的输入流
in = item.getInputStream();
if ("入驻高新技术企业成长性情况明细表".equals(filename)) {
String id = (String) map.get("id");
String apply_id = (String) map.get("apply_id");
String user_code = (String) map.get("user_code");
String user_name = (String) map.get("user_name");
model.addAttribute("apply_id", apply_id);
// 读取Excel文件并存入数据库
Workbook wb0 = new XSSFWorkbook(in);
// 获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0);
/*
* List<FileItem> list1 = upload.parseRequest(request);
* String l=String.valueOf(list1.size());
* logger.info(l);
*/
try {
for (int i = 2; sht0.getRow(i).getCell(1)
.getStringCellValue() != null
&& !sht0.getRow(i).getCell(1)
.getStringCellValue().equals(""); i++) {
// 如果当前行的行号(从0开始)未达到3(第四行)则从新循环
Row r = sht0.getRow(i);
ZjsbQyczmx info = new ZjsbQyczmx();
info.setApply_id(apply_id);
info.setUser_code(user_code);
info.setUser_name(user_name);
r.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
info.setFilm_name(r.getCell(1)
.getStringCellValue());
r.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
info.setCgx_num(r.getCell(2)
.getStringCellValue());
r.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
info.setAnnual_income(r.getCell(3)
.getStringCellValue());
zjsbcxryhzService.addZjsbQyczmx(info);
}
mav.addObject("success", "上传成功");
List<ZjsbQyczmx> qyczList = zjsbcxryhzService
.getQyczList(apply_id);
mav.addObject("qyczList", qyczList);
mav.setViewName("/fhq/zjsbcxryhz/qycz");
/*
* return
* "redirect:/fhq/zjsbcxryhz/list?id="+id+"&apply_id="
* +apply_id;
*/
/* return mav; */
} catch (Exception e) {
mav.addObject("fail", "请检查Excel格式");
/* return mav; */
}
} else {
mav.addObject("fail", "请检查Excel格式");
/* return mav; */
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileUploadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return mav;
}