码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 下载工程resources目录下的模板excel文件


    一、添加依赖

    1. <dependency>
    2. <groupId>org.apache.poi</groupId>
    3. <artifactId>poi-ooxml</artifactId>
    4. <version>5.1.0</version>
    5. </dependency>

    二、编写接口

    1. @GetMapping("/downloadTemplate")
    2.     public void downloadTemplate(HttpServletRequest request,
    3.                                  HttpServletResponse response){
    4.         String fileName = "template"+File.separator+"字段标准导入模板.xlsx";
    5.         InputStream is = ClassLoader.getSystemResourceAsStream(fileName);
    6.         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    7.         response.setCharacterEncoding("utf-8");
    8.         Workbook wb = null;
    9.         try {
    10.             wb = new XSSFWorkbook(is);
    11.         } catch (IOException e) {
    12.             throw new RuntimeException(e);
    13.         }
    14.         try {
    15.             wb.write(response.getOutputStream());
    16.         } catch (Exception e) {
    17.             log.error("下载导入模板异常{}", e.getMessage());
    18.         } finally {
    19.             IOUtils.closeQuietly(wb);
    20.             if(is!=null){
    21.                 try {
    22.                     is.close();
    23.                 } catch (IOException e) {
    24.                     throw new RuntimeException(e);
    25.                 }
    26.             }
    27.         }
    28.     }


    三、这种代码,在windows上调试没问题,打成jar包发到linux环境上会有问题,改成如下代码就行:

    1. @GetMapping("/downloadTemplate")
    2.     public void downloadTemplate(HttpServletRequest request,
    3.                                  HttpServletResponse response){
    4.         String fileName = "template"+File.separator+"ColumnStandardImportTemplate.xlsx";
    5.         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    6.         response.setCharacterEncoding("utf-8");
    7.         try (InputStream is = new ClassPathResource(fileName).getInputStream();
    8.              Workbook wb = new XSSFWorkbook(is);
    9.              ServletOutputStream os = response.getOutputStream()) {
    10.             wb.write(os);
    11.             os.flush();
    12.         } catch (Exception e) {
    13.             log.error("下载导入模板异常", e);
    14.         }
    15.     }


        

  • 相关阅读:
    Spring WebFlux简单使用
    Pytorch实战教程(十一)-卷积神经网络
    文心一言(ERNIE Bot)初体验
    el-table合并单元格-会超出表格内容问题
    【目标检测竞赛总结】IEEE UV 2022 “Vision Meets Algae” Object Detection Challenge
    AI全球气象预报模型;开源数据标注平台;『统计学习导论及R语言应用』Python版源码;『数学』自学路线图与资源;前沿论文 | ShowMeAI资讯日报
    Spring Boot魔法:简化Java应用的开发与部署
    Ubuntu22.04_如何调试ROS2_humble的源代码
    FPGA-HDMI-彩条显示实验(ZYBO Z7)
    ARM 版银河麒麟桌面系统下 Qt 开发环境搭建指南
  • 原文地址:https://blog.csdn.net/heming20122012/article/details/139781743
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号