阿里Exclel
作者:互联网
目录
模板导出
注意点
- 默认解析03版,07版文件注意报错信息
- 这个方法有坑,此代码是导出成功的写法,
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateDirPath+templateName)
.autoCloseStream(Boolean.FALSE).build(); - 单个变量:{属性名}
- 列表属性变量:{.属性名} 多个点
controller
点击查看代码
/**
* 导出
* @param id
* @param type 和faultRecordRelation.js的showOneFile函数值保持一致
* @param request
* @param response
* @throws Exception
*/
@RequestMapping("/exportData")
@ResponseBody
public void exportData(@RequestParam String id,@RequestParam String type,
HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
onesluiceOneFileDirecService.exportData(id,type,request,response);
} catch (Exception e) {
e.printStackTrace();
logger.error("一闸一档_导出失败", e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().println("导出失败,系统内部错误!");
}
}
html
点击查看代码
parent.window.location = ctx+'/pdms/oneFile/exportData?id='+id+'&type='+type;
Service
点击查看代码
/**
* 导出 表格
* @param id
* @param type
* @param request
* @param response
*/
public void exportData(String id, String type, HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileName = "一闸一档_";
String templateDirPath = request.getSession().getServletContext().getRealPath("/")
+ File.separator + "exporttemp" +File.separator;
String templateName = "";
if("1".equals(type)){
fileName += "AT方式";
templateName = "yizhayidang-at.xls";
}else if("2".equals(type)){
fileName += "直供方式";
// templateName = "yizhayidang-zhigong.xls";
}else if("3".equals(type)){
fileName += "开闭所越级跳闸";
// templateName = "yizhayidang-.xls";
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
fileName = URLEncoder.encode(fileName+".xls", "utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
// responseHeader(response, fileName); 上面四行可抽成方法
//读取Excel模板
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateDirPath+templateName)
.autoCloseStream(Boolean.FALSE).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
if("1".equals(type)){
OnesluiceOneFileAt at = findById(OnesluiceOneFileAt.class, id);
excelWriter.fill(at, writeSheet);
}else {
OnesluiceOneFileDirec direc = findById(OnesluiceOneFileDirec.class, id);
excelWriter.fill(direc, writeSheet);
}
excelWriter.finish();
}
标签:String,param,response,阿里,Exclel,fileName,type,id 来源: https://www.cnblogs.com/jf666/p/16663218.html