1.前端
location.href = urls.exportExg+"?namsNo="+namsNo+"&dataSource="+dataSource+"&chgTmsCnt="+chgTmsCnt;
2.后端代码
@RequestMapping(value = "/export_nams_exg",method = RequestMethod.GET)
public void exportNamsExg(HttpServletResponse response,String namsNo,Integer dataSource,Integer chgTmsCnt){
try {
Map<String,Object> data = new HashMap<>();
/*获取成品数据*/
if(EmsCommon.DATA_SOURCE_HIS==dataSource){
List<NamsExgHis> list = exgAppService.listNamsExgHis(namsNo,chgTmsCnt);
data.put("list",list);
}
/*获取模板路径*/
URL url = ResourceUtils.getURL("classpath:exceltemplet/nams_exg_expt.xls");
FileUtils.downloadTemplate(url.getPath(),data,"成品",response);
}catch (Exception e){
log.error(e.getMessage(),e);
e.printStackTrace();
}
工具代码
public static void downloadTemplate(String path, Map<String, Object> map, String fileName, HttpServletResponse response) {
try {
logger.info("路径:" + path);
path = path.replaceAll("%20", " ");
/*设置模板路径*/
TemplateExportParams params = new TemplateExportParams(path);
if (map == null) {
map = new HashMap<>();
}
/*加载模板*/
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
/*设置返回表头*/
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
3.导出excel模板

本文介绍了一个从前端到后端实现成品数据导出为Excel文件的完整过程。前端通过修改location.href的方式触发导出操作,后端使用@RequestMapping注解处理请求,通过Service获取成品数据并填充到Excel模板中,最终将生成的Excel文件返回给前端进行下载。文章详细展示了代码实现,包括异常处理和日志记录。

1万+

被折叠的 条评论
为什么被折叠?



