$("#btnImport").click(function() {
$.jBox($("#importBox").html(), {title:"导入数据", buttons:{"关闭":true},
bottomText:"导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!"});
});
$("#btnExport").click(function(){
var index=document.getElementById('remark5').selectedIndex;
var zhi=document.getElementById('remark5').options[index].text;
$("#remark6").val(zhi);
top.layer.confirm('确认要导出Excel吗?', {icon: 3, title:'系统提示'}, function(index){
//do something
//导出之前备份
var url = $("#searchForm").attr("action");
var pageNo = $("#pageNo").val();
var pageSize = $("#pageSize").val();
//导出excel
$("#searchForm").attr("action","${ctx}/hp/hpCloth/export");
/* $("#pageNo").val(-1);
$("#pageSize").val(-1);*/
$("#searchForm").submit();
//导出excel之后还原
$("#searchForm").attr("action",url);
$("#pageNo").val(pageNo);
$("#pageSize").val(pageSize);
top.layer.close(index);
});
});
<div id="importBox" class="hide">
<form id="importForm" action="${ctx}/hp/hpCloth/import" method="post" enctype="multipart/form-data"
class="form-search" style="padding-left:20px;text-align:center;" onsubmit="loading('正在导入,请稍等...');"><br/>
<input id="uploadFile" name="file" type="file" style="330px"/><br/><br/>
<input id="btnImportSubmit" class="btn btn-primary" type="submit" value="确认导入"/>
<a href="${ctx}/hp/hpCloth/exportTemp" class="btn btn-primary">下载模板</a>
</form>
</div>
<li class="btns">
<button id="btnImport" type="button" class="btn btn-primary " data-toggle="tooltip" data-placement="left" title="导入">
<i class="fa fa-file-excel-o"></i> 导入</button>
</li>
<li class="btns">
<button id="btnExport" type="button" class="btn btn-primary " data-toggle="tooltip" data-placement="left" title="导出">
<i class="fa fa-file-excel-o"></i> 导出</button>
</li>
//Excel导入数据
@RequestMapping(value = "import")
public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
try {
int successNum = 0;
int failureNum = 0;
StringBuilder failureMsg = new StringBuilder();
ImportExcel imEx = new ImportExcel(file, 1, 0);
List<HpCloth> wlCatalogList = imEx.getDataList(HpCloth.class);
for (HpCloth wlCatalog : wlCatalogList) {
try {
hpClothService.save(wlCatalog);
successNum++;
} catch (ConstraintViolationException ex) {
failureMsg.append("<br/>物料名称 " + wlCatalog.getBrand() + " 导入失败:");
List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
for (String message : messageList) {
failureMsg.append(message + "; ");
failureNum++;
}
} catch (Exception ex) {
failureMsg.append("<br/>物料名称 " + wlCatalog.getBrand() + " 导入失败:" + ex.getMessage());
}
}
if (failureNum > 0) {
failureMsg.insert(0, ",失败 " + failureNum + " 条数据,导入信息如下:");
}
addMessage(redirectAttributes, "已成功导入 " + successNum + " 条数据" + failureMsg);
} catch (Exception e) {
addMessage(redirectAttributes, "导入物料数据失败!失败信息:" + e.getMessage());
}
return "redirect:" + Global.getAdminPath() + "/wl/wlCatalog/list?repage";
}
@RequestMapping(value = "export")
public String exportFile(HpCloth hpCloth, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "当前库存数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
Page<HpCloth> page = hpClothService.findPage(new Page<HpCloth>(request, response), hpCloth);
new ExportExcel("当前库存数据", HpCloth.class).setDataList(page.getList()).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导出当前库存数据失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/hp/hpCloth/list?repage";
}