package com.persagy.proxy.adm.controller; import java.io.File; import java.io.FileNotFoundException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.excel.ExcelWriter; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.alibaba.excel.EasyExcel; import com.persagy.proxy.adm.constant.AdmCommonConstant; import lombok.extern.slf4j.Slf4j; /** * 报表/模板下载 * * @version 1.0.0 * @company persagy * @author zhangqiankun * @date 2021年8月30日 下午7:41:56 */ @Slf4j @RestController public class ReportDownloadController { /** * 下载报告-设备静态信息维护模板 * * @param request * @param response */ @RequestMapping("/graphic/downloads/equip/template") public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) { ExcelWriter excelWriter = null; try { response.setContentType("application/vnd.ms-excel"); String encode = StandardCharsets.UTF_8.name(); response.setCharacterEncoding(encode); // 防止中文乱码 String fileName = URLEncoder.encode("台账信息导入模板", encode); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); File file = null; try { file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/equip_template.xlsx"); } catch (FileNotFoundException e) { } if (file == null || !file.exists()) { file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/equip_template.xlsx"); } excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build(); } catch (Exception e) { log.error("设备静态信息维护模板下载失败", e); } finally { if (excelWriter != null) { excelWriter.finish(); } } } /** * 下载报告-点表 * * @param request * @param response */ @RequestMapping("/graphic/downloads/point") public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) { ExcelWriter excelWriter = null; try { response.setContentType("application/vnd.ms-excel"); String encode = StandardCharsets.UTF_8.name(); response.setCharacterEncoding(encode); response.setHeader("Content-disposition", "attachment;filename=" + key); File file = null; try { file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key); } catch (FileNotFoundException e) { file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx"); } if (file == null || !file.exists()) { file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx"); } excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build(); } catch (Exception e) { log.error("设备静态信息维护模板下载失败", e); } finally { if (excelWriter != null) { excelWriter.finish(); } } } }