Springboot基于easyexcel实现一个excel文件包含多个sheet表格的数据导出

Springboot基于easyexcel实现一个excel文件包含多个sheet表格的数据导出

前言

Easyexcel 是一款基于Java的开源Excel操作工具,它提供了简单且强大的 API,使开发人员可以轻松地读写、操作和生成Excel文件。

EasyExcel 支持 Excel 文件的导入和导出,可以处理大量数据,具有高性能和低内存占用。它可以读取 Excel 文件中的数据,并将数据转换为 Java 对象,也可以将Java对象写入Excel文件。

EasyExcel 还提供了丰富的格式化选项和功能,如设置单元格样式、合并单元格、设置公式等。同时,EasyExcel 还支持多线程操作,可以在处理大量数据时提高处理效率。由于其简单易用的特点,EasyExcel 被广泛应用于数据导入导出、报表生成、数据分析等领域。

一、引入easyexcel依赖

             <dependency>
                <groupId>***.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>3.2.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>poi-ooxml-schemas</groupId>
                        <artifactId>org.apache.poi</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

二、创建实体类

package ***.ruoyi.exportData.vo;

import ***.alibaba.excel.annotation.ExcelProperty;
import ***.alibaba.excel.annotation.write.style.*;
import ***.alibaba.excel.enums.poi.FillPatternTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.A***essors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@A***essors(chain = true)
@ColumnWidth(25)
@ContentRowHeight(30)
@HeadRowHeight(50)
@Builder
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)
@HeadFontStyle(fontHeightInPoints = 12)
@ContentFontStyle(fontHeightInPoints = 11)
public class ExportListVo {
    @ExcelProperty(value = "名称")
    private String name;
    @ExcelProperty(value = "数据")
    private String value;
}

easyexcel常用注解

@ExcelProperty 用于标识excel中的字段,可以指定字段在Excel中的列索引或列名

@ColumnWith::设置列宽

@ColumnWidth: 全局列宽

@ContentFontStyle: 用于设置单元格内容字体格式的注解

               

@ContentLoopMerge:用于设置合并单元格

                

@ContentRowHeight:用于设置行高

                

@ContentStyle:设置内容格式

               

@HeadFontStyle:用于定制标题字体格式

                

@HeadRowHeight:设置标题行行高

                

@HeadStyle:设置标题样式

                

@ExcelIgnore:不将该字段转换成Excel

@ExcelIgnoreUnannotated:没有注解的字段都不转换

三、controller层接口

package ***.ruoyi.web.controller.zx.export;
import ***.alibaba.excel.EasyExcelFactory;
import ***.alibaba.excel.ExcelWriter;
import ***.alibaba.excel.write.metadata.WriteSheet;
import ***.alibaba.excel.write.metadata.style.WriteCellStyle;
import ***.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import ***.ruoyi.baseconsulttopics.service.IBaseconsultTopicsService;
import ***.ruoyi.***mittee.service.I***mitteeService;
import ***.ruoyi.***mon.domain.ResultVo;
import ***.ruoyi.document.service.IDocumentService;
import ***.ruoyi.exportData.vo.ExportListVo;
import ***.ruoyi.meet.service.IMeetService;
import ***.ruoyi.***work.service.I***workService;
import ***.ruoyi.news.service.INewsService;
import ***.ruoyi.proposal.service.IProposalService;
import ***.ruoyi.publicopinion.service.IPublicopinionService;
import ***.ruoyi.scholarly.service.IScholarlyLearningService;
import ***.ruoyi.scholarly.service.IScholarlyReadService;
import ***.ruoyi.transformdocument.service.TransformDocumentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.***.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/export")
@Slf4j
public class ExportDataIndexController {
    @Autowired
    private IProposalService proposalService;
    @Autowired
    private IPublicopinionService publicopinionService;
    @Autowired
    private I***workService ***workService;
    @Autowired
    private I***mitteeService ***mitteeService;
    @Autowired
    private IMeetService meetService;
    @Autowired
    private IDocumentService documentService;
    @Autowired
    private INewsService newsService;
    @Autowired
    private IScholarlyLearningService scholarlyLearningService;
    @Autowired
    private TransformDocumentService transformDocumentService;
    @Autowired
    private IBaseconsultTopicsService baseconsultTopicsService;



    @GetMapping("/exportDataIndex")
    public void exportExcel(HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("云南省政协数据可视化中心", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            handleExcel(out);
            out.flush();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    private void handleExcel(OutputStream out) {
        try (ExcelWriter excelWriter = EasyExcelFactory.write(out).build()) {
            //设置内容样式
            WriteCellStyle contentStyle = new WriteCellStyle();
            contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//居中
            contentStyle.setWrapped(true);//自动换行
            //设置头部样式
            WriteCellStyle headerStyle = new WriteCellStyle();
            headerStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            //设置策略
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headerStyle,contentStyle);
            WriteSheet proposalSheet = EasyExcelFactory.writerSheet(0, "委员提案").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet publicopinionSheet = EasyExcelFactory.writerSheet(1, "社情民意").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet consultationSheet = EasyExcelFactory.writerSheet(2, "协商议政").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet ***mitteeSheet = EasyExcelFactory.writerSheet(3, "委员信息").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet meetSheet = EasyExcelFactory.writerSheet(4, "会议活动").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet documentSheet = EasyExcelFactory.writerSheet(5, "公文流转").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            WriteSheet provinceSheet = EasyExcelFactory.writerSheet(6, "云南全省政协").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            excelWriter.write(getProposal(), proposalSheet);
            excelWriter.write(getPublicopinion(), publicopinionSheet);
            excelWriter.write(getConsultation(),consultationSheet);
            excelWriter.write(get***mittee(),***mitteeSheet);
            excelWriter.write(getMeet(),meetSheet);
            excelWriter.write(getDocument(),documentSheet);
            excelWriter.write(getProvinceWide(),provinceSheet);
        }
    }

    //首页-委员提案
    private List<ExportListVo> getProposal() {
        List<ExportListVo> list = new ArrayList<>();
        int proposalCount = proposalService.proposalCount(null, area);
        List<ResultVo> proposalKind = proposalService.proposalKind(null, area);
        ExportListVo vo = new ExportListVo("委员提案总数",proposalCount+" 条");
        list.add(vo);
        for (ResultVo result : proposalKind) {
            ExportListVo kind = new ExportListVo();
            kind.setName(result.getName());
            kind.setValue(result.getValue()+" 条");
            list.add(kind);
        }
        return list;
    }

    //首页-社情民意
    private List<ExportListVo> getPublicopinion() {
        List<ExportListVo> list = new ArrayList<>();
        int opinionCount = publicopinionService.opinionCount(null, area, null);
        List<ResultVo> opinionType = publicopinionService.opinionType(null, area);
        ExportListVo vo = new ExportListVo("社情民意总数",opinionCount+" 条");
        list.add(vo);
        for (ResultVo result : opinionType) {
            ExportListVo type = new ExportListVo(result.getName(),result.getValue()+" 条");
            list.add(type);
        }
        return list;
    }

    //首页-网络议政/远程协商
    public List<ExportListVo> getConsultation(){
        List<ExportListVo> list = new ArrayList<>();
        List<ResultVo> ***WorkCount = ***workService.***WorkCount(null, area);
        List<ResultVo> remoteCount = ***workService.remoteCount(null, area);
        List<ResultVo> ***workConduct = ***workService.***workConduct(area);
        List<ResultVo> ***workClosed = ***workService.***workClosed(area);
        List<ResultVo> remoteConduct = ***workService.remoteConduct(area);
        List<ResultVo> remoteClosed = ***workService.remoteClosed(area);
        ExportListVo vo = new ExportListVo("网络议政总数",***WorkCount.get(0).getValue()+" 条");
        ExportListVo vo1 = new ExportListVo("远程协商总数",remoteCount.get(0).getValue()+" 条");
        ExportListVo vo2 = new ExportListVo("网络议政-进行中",***workConduct.get(0).getValue()+" 条");
        ExportListVo vo3 = new ExportListVo("网络议政-已结束",***workClosed.get(0).getValue()+" 条");
        ExportListVo vo4 = new ExportListVo("远程协商-进行中",remoteConduct.get(0).getValue()+" 条");
        ExportListVo vo5 = new ExportListVo("远程协商-已结束",remoteClosed.get(0).getValue()+" 条");
        list.add(vo);
        list.add(vo1);
        list.add(vo2);
        list.add(vo3);
        list.add(vo4);
        list.add(vo5);
        return list;
    }

    //首页-委员信息
    public List<ExportListVo> get***mittee(){
        List<ExportListVo> list = new ArrayList<>();
        int ***mitteeCount = ***mitteeService.***mitteeCount(area,null);//委员人数
        int standing***mitteeCount = ***mitteeService.standing***mmitteeCount(area,null);//常委人员
        int officeCount = ***mitteeService.officeCount(area);//机关人
        ExportListVo vo = new ExportListVo("委员人数",***mitteeCount+" 人");
        ExportListVo vo1 = new ExportListVo("常委人数",standing***mitteeCount+" 人");
        ExportListVo vo2 = new ExportListVo("机关人数",officeCount+" 人");
        list.add(vo);
        list.add(vo1);
        list.add(vo2);
        List<ResultVo> partiesCount = ***mitteeService.partiesCount(area,null);//委员构成
        List<ResultVo> ageCount = ***mitteeService.ageCount(area,null);//年龄
        List<ResultVo> genderCount = ***mitteeService.genderCount(area,null);//性别
        List<ResultVo> nationCount = ***mitteeService.nationCount(area,null);//民族
        list.add(new ExportListVo("党派",null));
        for (ResultVo result : partiesCount) {
            ExportListVo parties = new ExportListVo(result.getName(), result.getValue() + " 人");
            list.add(parties);
        }
        list.add(new ExportListVo("年龄",null));
        for (ResultVo result : ageCount) {
            ExportListVo age = new ExportListVo(result.getName(), result.getValue() + " 人");
            list.add(age);
        }
        list.add(new ExportListVo("性别",null));
        for (ResultVo result : genderCount) {
            ExportListVo gender = new ExportListVo(result.getName(), result.getValue() + " 人");
            list.add(gender);
        }
        list.add(new ExportListVo("民族",null));
        for (ResultVo result : nationCount) {
            ExportListVo nation = new ExportListVo(result.getName(), result.getValue() + "人");
            list.add(nation);
        }
        return list;
    }

    //首页-会议活动
    public List<ExportListVo> getMeet(){
        List<ExportListVo> list = new ArrayList<>();
        List<ResultVo> meetCount = meetService.meetCount(null,area);
        list.add(new ExportListVo("会议活动总数",meetCount.get(0).getValue()+" 次"));
        List<ResultVo> yearCount = meetService.yearCount(area);
        for (ResultVo result : yearCount) {
            list.add(new ExportListVo(result.getName(),result.getValue()+" 次"));
        }
        return list;
    }

    //首页-公文流转
    public List<ExportListVo> getDocument(){
        List<ExportListVo> list = new ArrayList<>();
        int documentCount = documentService.documentCount(null, area);
        list.add(new ExportListVo("公文流转总数",documentCount+" 条"));
        List<ResultVo> yearCount = documentService.yearCount(null, area);
        for (ResultVo result : yearCount) {
            list.add(new ExportListVo(result.getName(),result.getValue()+" 条"));
        }
        return list;
    }

    //首页-云南全省政协
    public List<ExportListVo> getProvinceWide(){
        List<ExportListVo> list = new ArrayList<>();
        int ***mitteeCount = ***mitteeService.***mitteeCount(area,null);//委员人数
        int standing***mitteeCount = ***mitteeService.standing***mmitteeCount(area,null);//常委人员
        int officeCount = ***mitteeService.officeCount(area);//机关人
        int opinionCount = publicopinionService.opinionCount(null, area, null);//社情民意
        int newsCount = newsService.newsCount(null, area);//政协新闻
        int proposalCount = proposalService.proposalCount(null, area);//委员提案
        List<ResultVo> ***WorkCount = ***workService.***WorkCount(null, area);//网络议政
        List<ResultVo> remoteCount = ***workService.remoteCount(null, area);//远程协商
        Integer readCount = scholarlyLearningService.getCount("", area);
        List<ResultVo> meetCount = meetService.meetCount(null,area);//会议活动
        int documentCount = documentService.documentCount(null, area);//公文流转
        int exchangeDocumentCount = transformDocumentService.exchangeDocumentCount();//公文交换
        int baseTopi***ount = baseconsultTopicsService.baseTopi***ount(area);//协商在基层
        list.add(new ExportListVo("委员人数",***mitteeCount+" 人"));
        list.add(new ExportListVo("常委人数",standing***mitteeCount+" 人"));
        list.add(new ExportListVo("机关人数",officeCount+" 人"));
        list.add(new ExportListVo("政协新闻",newsCount+" 条"));
        list.add(new ExportListVo("社情民意",opinionCount+"条"));
        list.add(new ExportListVo("委员提案",proposalCount+" 条"));
        list.add(new ExportListVo("网络议政",***WorkCount.get(0).getValue()+" 次"));
        list.add(new ExportListVo("远程协商",remoteCount.get(0).getValue()+ " 次"));
        list.add(new ExportListVo("书香政协",readCount+" 条"));
        list.add(new ExportListVo("会议活动",meetCount.get(0).getValue()+" 次"));
        list.add(new ExportListVo("公文流转",documentCount+" 条"));
        list.add(new ExportListVo("公文交换",exchangeDocumentCount+" 条"));
        list.add(new ExportListVo("协商在基层",baseTopi***ount+" 条"));
        return list;
    }

}

四、在浏览器地址栏输入地址:http://localhost:8997/export/exportDataIndex   

进行数据导出

最后导出的文件如下所示:

至此,一个工作簿多个工作表导出的功能就完成了。

转载请说明出处内容投诉
CSS教程_站长资源网 » Springboot基于easyexcel实现一个excel文件包含多个sheet表格的数据导出

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买