欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springboot +easypoi模板导出Excel

程序员文章站 2022-06-24 23:49:37
...

依赖:

<!-- easypoi简单导出所需要的jar包 start -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.1.0</version>
        </dependency>
<!-- easypoi简单导出所需要的jar包 end-->

excel:

Springboot +easypoi模板导出Excel

将表格放入Springboot +easypoi模板导出Excel 

前端用什么框架什么框架都可以Controller主要接收两个参数response和前端传过来的参数params

Controller: 

  @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("fysjtsygt:fysjtsygt:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<FysjtsygtDTO> list = fysjtsygtService.list(params);
        String fileNmae = "土地台账";
        String date = DateUtils.format(new Date());
        String year = params.get("ydYear").toString();
        String orgName = params.get("orgName").toString();
        TemplateExportParams param = new TemplateExportParams("/excelTemplates/土地台账.xlsx", true);
        // 标题开始行
        // param.setHeadingStartRow(0);
        // 标题行数
        // param.setHeadingRows(2);
        // 设置sheetName,若不设置该参数,则使用得原本得sheet名称
        //param.setSheetName("班级信息");
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("date", date);//导出一般都要日期
        data.put("year", year);//导出一个对象
        data.put("orgName", orgName);//导出一个对象
        data.put("list", list);//导出list集合
        try {
            Workbook book = ExcelExportUtil.exportExcel(param, data);
            //下载方法
            export(response, book, fileNmae);
        } catch (Exception e) {
            System.out.println("导出模板Excel,失败:" + e);
        }
    }

    /**
     * export导出请求头设置
     *
     * @param response
     * @param workbook
     * @param fileName
     * @throws Exception
     */
    private static void export(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
        response.reset();
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName+".xlsx", "UTF-8"));
        ServletOutputStream outStream = null;
        try {
            outStream = response.getOutputStream();
            workbook.write(outStream);
        } finally {
            outStream.close();
        }

    }