下载工程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);
        }
    }
相关推荐
Cxiaomu2 小时前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707783 小时前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong3 小时前
B生所学EXCEL
人工智能·excel
城数派3 小时前
2014-2025年全国监测站点的逐年空气质量数据(15个指标\Excel\Shp格式)
数据分析·excel
亮子AI3 小时前
【Excel】免费并且有暗黑模式的 Excel
excel
罗政3 小时前
Excel打印格式批量调整方法,上千份表格一键统一设置
excel
城数派1 天前
2005-2025年我国逐日露点温度栅格数据
数据分析·excel
LAM LAB1 天前
【VBA】Excel实现批量对单元格替换、加粗文字
excel·vba
海兰1 天前
【接上篇】多格式文档支持扩展方案(PDF_Word_Excel)
pdf·word·excel
小趴菜克鲁里1 天前
游戏Excel配置自动化导出二进制工具链并生成对应配置类详解
游戏·自动化·excel