阿里Exclel

2022/9/6 23:26:07

本文主要是介绍阿里Exclel,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 模板导出
    • 注意点
    • controller
    • html
    • Service

模板导出

注意点

  • 默认解析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();
	}


这篇关于阿里Exclel的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程