最近期在负责的一个项目中,用到很多Excel文件导入web页面这个方法,现在总结归纳一下。
**html===============**
<!-- 导入Excel -->
<form style="color:white;border:none;cursor:pointer" class="impor" id="form1" method="POST" enctype="multipart/form-data">
导入excel
<input style="display:none" class="import" type="file" name="upfile" value="" >
</form>
<script src="../plugins/jquery/jquery.form.min.js"></script>
<script type="text/javascript">
var impor = document.querySelector('.impor');
var imports = document.querySelector('.import');
impor.onclick = function(){
imports.click();
};
imports.onchange = function(){
console.log(this.files[0]);
// people.innerHTML = this.file;
}
=controller.js================
//文件上传
$("input[type=file]").change(function (e) {
if (window.confirm('你确定要上传文件吗?')) {
$('#form1').ajaxSubmit({
url: '/PostalBank/user/uploadexcel.do',
dataType: 'text',
success: resutlMsg,
error: errorMsg
});
function resutlMsg(msg) {
alert(msg);
$("#upfile").val("");
//调用方法
$scope.reloadList();//刷新列表
}
function errorMsg() {
alert("导入excel出错!请选择正确的文件!");
}
controller.java==============
``
/**
* 完成度设置后上传excel
* @return
*/
@RequestMapping(value="uploadTemplet.do")
public Result uploadTemplet(Integer tindex,HttpServletRequest request, HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
InputStream in = null;
List<List<Object>> listob = null;
MultipartFile file = multipartRequest.getFile("upfile");
if (file.isEmpty()) {
throw new CustomerException("上传文件不能为空");
}
in = file.getInputStream();
String fileName = file.getOriginalFilename();
listob = new ImportExcelUtil().readExcelWithTitle(in, fileName);
/* log.info("输入文件名为: " + fileName + " 数据为:" + listob);
*/
//获取当前的季度
Integer quarter = DateUtils.currentQuarter();
//录入库,得到相关录入岗位信息(新)
for (int i = 0; i < listob.size(); i++) {
// 得到每页中有多少个map集合数据
for (int j = 0; j < listob.get(i).size(); j++) {
Map dataMap = new HashMap<>();
//岗位ID 通过输入获取
TCompanyAssess tCompanyAssess = new TCompanyAssess();
TTime tTime = new TTime();
TIndex item=new TIndex();
dataMap = (Map) listob.get(i).get(j);
//录入指标与库
if (dataMap.containsKey("考核指标")) {
//根据获取到的id查找findCategory
TUserCategory category = userCategoryService.findCategory(tindex);
item.setCreatetime(new Date());
item.setPermissionid(tindex);
item.setPermissionname(category.getCategoryname());
if(dataMap.containsKey("指标类别")) {
String indexNum = (String) dataMap.get("指标类别");
item.setType(indexNum);
if(indexNum.equals("基本指标")) {
item.setTypeid(0);
}
if(indexNum.equals("分类指标")) {
item.setTypeid(1);
}
dataMap.remove("指标类别");
}
if(dataMap.containsKey("考核指标")) {
String accname = (String) dataMap.get("考核指标");
item.setTitle(accnam e);
dataMap.remove("考核指标");
}
if(dataMap.containsKey("考核岗位")) {
String khgw = (String) dataMap.get("考核岗位");
TPost tPost = tPostService.selectPostByPostName(khgw);
item.setPostid(tPost.getId());
dataMap.remove("指标名称");
}
TIndex tcoma=tIndexService.selectBytitle(item.getTitle());
if(tcoma!=null)
{
tIndexService.update(item);
}else
{
tIndexService.add(item);
}
}
}else
{
return new Result(false, "数据不正确");
}
}
}
return new Result(true, "文件上传成功");
}
`