下载工程resources目录下的模板excel文件

一、添加依赖

复制代码
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>

二、编写接口

复制代码
@GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request,
                                 HttpServletResponse response){
        String fileName = "template"+File.separator+"字段标准导入模板.xlsx";
        InputStream is = ClassLoader.getSystemResourceAsStream(fileName);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("下载导入模板异常{}", e.getMessage());
        } finally {
            IOUtils.closeQuietly(wb);
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

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

复制代码
@GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request,
                                 HttpServletResponse response){
        String fileName = "template"+File.separator+"ColumnStandardImportTemplate.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try (InputStream is = new ClassPathResource(fileName).getInputStream();
             Workbook wb = new XSSFWorkbook(is);
             ServletOutputStream os = response.getOutputStream()) {
            wb.write(os);
            os.flush();
        } catch (Exception e) {
            log.error("下载导入模板异常", e);
        }
    }
相关推荐
淡海水8 小时前
01-10-认知篇-对比-方案选型矩阵
矩阵·c#·excel·游戏开发·luban
小宋102112 小时前
知识库里的 Excel 不能当纯文本读:表头行识别、合并单元格回填与公式缓存值的结构化入库
前端·人工智能·缓存·excel
Logic10113 小时前
Excel定义名称5:定义名称+图片链接,做一个产品图库
excel·excel技巧·下拉菜单·定义名称·动态图片·图片链接·产品图库
灵析表格1 天前
灵析表格函数精讲|AI_NLP_提取重点关键词:抓商品标题核心词
excel·办公自动化·wps·效率工具·数据处理·ai办公·函数公式
ComPDFKit1 天前
扫描对账单 PDF 转 Excel,怎么查出金额错行?
python·pdf·excel
Logic1011 天前
Excel定义名称4:省市县三级联动菜单
excel·indirect·excel函数·定义名称·联动菜单·数据有效性·省市县联动菜单
SunnyDays10111 天前
Java 填充 Excel 模板:用命名区域告别写死的单元格地址
java·excel
淡海水2 天前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban
优选资讯2 天前
合格证套打怎么落地:Luck标签软件把 Excel 字段、编号和打印预览一次做顺
excel·标签打印·luck标签软件
Logic1012 天前
Excel定义名称2,跨表汇总求最值
excel·indirect·sumif·excel函数·定义名称·跨表查询