下载工程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);
        }
    }
相关推荐
xlxxy_2 小时前
ABAP数据库表的增改查
开发语言·前端·数据库·sql·oracle·excel
程序猿多布4 小时前
Unity Excel导表工具转Lua文件
unity·excel
The god of big data8 小时前
Excel核心函数VLOOKUP全解析:从入门到精通
excel
专注VB编程开发20年17 小时前
除了 EasyXLS,加载和显示.xlsx 格式的excel表格,并支持单元格背景色、边框线颜色和粗细等格式化特性
c++·windows·excel·mfc·xlsx
诸神缄默不语1 天前
Excel如何给单元格填色,以及如何用Python 3实现单元格填色
开发语言·python·excel·openpyxl·patternfill
程序员小续1 天前
Excel 表格和 Node.js 实现数据转换工具
前端·javascript·react.js·前端框架·vue·excel·reactjs
jiemidashi1 天前
WPS 中 Excel 密码取消方法大解密
经验分享·excel·wps
赵域Phoenix2 天前
挑选出行数足够的excel文件
开发语言·python·excel
oh,huoyuyan2 天前
火语言RPA--Excel读取内容
excel·rpa
初九之潜龙勿用2 天前
C# 实现完善 Excel 不规则合并单元格数据导入
开发语言·c#·.net·excel