下载工程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);
        }
    }
相关推荐
程序员杰哥28 分钟前
Pytest与Unittest测试框架对比
自动化测试·软件测试·python·测试工具·测试用例·excel·pytest
cehuishi95271 小时前
excel中关联word邮件合并使用
word·excel·批量打印·邮件合并
曾令胜8 小时前
excel导出使用arthas动态追踪方法调用耗时后性能优化的过程
spring·性能优化·excel
我命由我1234511 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展
Lucky GGBond14 小时前
Vue + Spring Boot 实现 Excel 导出实例
vue.js·spring boot·excel
缺点内向1 天前
C# 中 Excel 工作表打印前页面边距的设置方法
c#·.net·excel
极智-9961 天前
Excel如何合并单元格?【图文详解】Excel合并单元格技巧?单元格合并高阶操作?
excel·excel如何合并单元格·excel合并单元格技巧·单元格合并高阶操作·单元格合并·取消单元格合并·重复数据合并
大气层煮月亮2 天前
Oracle EBS ERP之报表开发—条件筛选按钮和组件开发
excel
Chen不旧2 天前
easyexcel实现excel读取
java·excel·easyexcell