Springboot +easypoi模板导出Excel

依赖:


        
            cn.afterturn
            easypoi-base
            3.1.0
        
        
            cn.afterturn
            easypoi-annotation
            3.1.0
        
        
            cn.afterturn
            easypoi-web
            3.1.0
        

excel:

Springboot +easypoi模板导出Excel_第1张图片

将表格放入 

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

Controller: 

  @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("fysjtsygt:fysjtsygt:export")
    public void export(@ApiIgnore @RequestParam Map params, HttpServletResponse response) throws Exception {
        List 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 data = new HashMap();
        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();
        }

    }

 

你可能感兴趣的:(Java)