下载工程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);
        }
    }
相关推荐
智航GIS1 小时前
9.3 Excel 自动化
python·自动化·excel
骑着蜗牛闯宇宙2 小时前
TP8上传Excel地址数据批量标注到高德地图
数据库·php·excel
魅惑青花瓷2 小时前
【EXCEL动态获取数据范围并整列自动填充公式】
excel
zbguolei19 小时前
上传 Excel 文件进行数据库比对--增加导出功能
数据库·excel
!chen1 天前
Excel高性能异步导出完整方案
excel
杰瑞不懂代码1 天前
使用pandas操作EXCEL表格入门教程
python·excel·pandas·办公自动化·数据处理
yiSty1 天前
win10/win11安装Word、EXCEL、PPT、VISIO
word·powerpoint·excel
zbguolei2 天前
上传 Excel 文件进行数据库比对
数据库·excel
南风花海2 天前
使用ERP时出现Excel插件(ESClient10)加载失败,请检查是否被禁用了。
excel