下载工程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);
        }
    }
相关推荐
Eiceblue16 小时前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
acaad18 小时前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel
周杰伦fans18 小时前
.NET 轻量级处理 Excel 文件库 - MiniExce
windows·.net·excel
维持好习惯19 小时前
复杂Excel文件导入功能(使用AI快速实现)
java·spring boot·excel
揭老师高效办公19 小时前
格式刷+快捷键:Excel和WPS表格隔行填充颜色超方便
excel·wps表格
上官浩仁1 天前
springboot excel 表格入门与实战
java·spring boot·excel
yanlaifan2 天前
Excel批量处理一列数据---分列功能
excel
我命由我123452 天前
Word - Word 的 5 种视图(页面视图、阅读视图、Web 版式视图、大纲视图、草稿视图)
ui·word·excel·photoshop·表格·ps·美工
YAY_tyy2 天前
基于 Vue3 + VueOffice 的多格式文档预览组件实现(支持 PDF/Word/Excel/PPT)
前端·javascript·vue.js·pdf·word·excel
一路向北North2 天前
apache poi 导出复杂的excel表格
apache·excel