记录一次EasyPoi导出excel上线报空指针

编程入门 行业动态 更新时间:2024-10-19 01:28:18

记录一次EasyPoi导出excel上线报空<a href=https://www.elefans.com/category/jswz/34/1768268.html style=指针"/>

记录一次EasyPoi导出excel上线报空指针

问题源头:springBoot项目打jar包后文件资源加载方式不同,打包后Spring试图访问文件系统路径,但无法访问jar中的路径。

解决方案:可以使用 resource.getInputStream() 获取模板的文档流,重写到tomcat容器中并生成新的模板路径,按新的路径,导出excel即可。

public void exportExcel(List<Integer> ids, HttpServletResponse response) {//取到要导出的模板TemplateExportParams params = new TemplateExportParams(convertTemplatePath("\\static\\template\\溯源报表.xls"));System.out.println();//遍历信息使用工具类导出到if (null != params){//根据ids查询出对应的信息List<QrCodeBind> list = qrCodeBindMapper.selectByIds(ids);Map<String, Object> map = new HashMap<>();List<Map<String, Object>> listMap = new ArrayList<>();for (int i = 0; i < list.size(); i++) {Map<String, Object> lm = new HashMap<>();//put的键要跟excel模板中的名称一致String qrCodeNum = list.get(i).getQrCode();byte[] bytes = createQrCode(baseUrl+qrCodeNum, 256, 256);ImageEntity imageEntity = new ImageEntity(bytes, 256, 256);Date createTime = list.get(i).getCreateTime();lm.put("qrCodeNum", qrCodeNum);lm.put("batchNumberName", list.get(i).getBatchNumberName());lm.put("createTime",StringUtils.fromDate(createTime));lm.put("url",baseUrl+qrCodeNum);lm.put("qrCodeImg",imageEntity);listMap.add(lm);}map.put("maplist", listMap);Workbook workbook =  ExcelExportUtil.exportExcel(params, map);ServletOutputStream out = null;try {response.setContentType("application/vnd.ms-excel;charset=UTF-8");//设置导出Excel的名称response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode("溯源报表.xls","UTF-8"));out=response.getOutputStream();workbook.write(out);}catch (IOException e){e.printStackTrace();}finally {if (null!=out){try {out.close();} catch (IOException e) {e.printStackTrace();}}}}}/*** 生成二维码* @param content* @param width* @param height* @return*/private byte[] createQrCode(String content, int width, int height){QrConfig config = new QrConfig(width, height);// 设置边距,既二维码和背景之间的边距config.setMargin(3);// 高纠错级别config.setErrorCorrection(ErrorCorrectionLevel.H);// 设置前景色,既二维码颜色(青色)config.setForeColor(new Color(0,0,0).getRGB());// 设置背景色(灰色)config.setBackColor(new Color(242,242,242).getRGB());return QrCodeUtil.generatePng(content, config);}/*Resource读取文件存到Tomcat获取新的地址*/public static String convertTemplatePath(String path) {// 如果是windows则直接返回
//        if (System.getProperties().getProperty("os.name").contains("Windows")) {
//            return path;
//        }Resource resource = new ClassPathResource(path);FileOutputStream fileOutputStream = null;// 将模版文件写入到tomcat临时目录String folder = System.getProperty("catalina.home");File tempFile = new File(folder + File.separator + path);// 文件存在时不再写入if (tempFile.exists()) {return tempFile.getPath();}File parentFile = tempFile.getParentFile();// 判断父文件夹是否存在if (!parentFile.exists()) {parentFile.mkdirs();}try {BufferedInputStream inputStream = new BufferedInputStream(resource.getInputStream());fileOutputStream = new FileOutputStream(tempFile);byte[] buffer = new byte[10240];int len = 0;while ((len = inputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}return tempFile.getPath();}

更多推荐

记录一次EasyPoi导出excel上线报空指针

本文发布于:2024-03-23 23:44:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   上线   EasyPoi   excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!