it编程 > 编程语言 > Java

使用EasyPoi实现多Sheet页导出的示例代码

5人参与 2025-03-08 Java

前言

因多次遇到导出多sheet页的需求,故记录下来,以备后续参考使用

一、pom依赖

<!-- 集成easypoi组件 .导出excel http://easypoi.mydoc.io/ -->
            <dependency>
                <groupid>cn.afterturn</groupid>
                <artifactid>easypoi-base</artifactid>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupid>cn.afterturn</groupid>
                <artifactid>easypoi-web</artifactid>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupid>cn.afterturn</groupid>
                <artifactid>easypoi-annotation</artifactid>
                <version>3.2.0</version>
            </dependency>

二、主方法

sxssfworkbook workbook = new sxssfworkbook();
try {
                workbook = this.getsheetslist(notqualifiedsumdeptcountvos, locale);
                return workbook;
            } catch (exception e) {
                log.error("错误",e);
                return null;
            }

三、拼接多sheet页

private sxssfworkbook getsheetslist(list<vo> notqualifiedsumdeptcountvos, locale locale){
        // 点检项排名导出多sheet页
        list<map<string, object>> sheetslist = new arraylist<>();
        // 创建数据概览1-不合格次数的sheet
        this.getnotqualifiedsumexportsheet(notqualifiedsumdeptcountvos, sheetslist, locale);
        sxssfworkbook workbook = excelutils.exportexcel(sheetslist);
        return workbook;
    }

四、获取单个sheet页

private void getnotqualifiedsumexportsheet(list<notqualifiedsumdeptcountvo> notqualifiedsumdeptcountvos, list<map<string, object>> sheetslist, locale locale){
        if (collectionutil.isnotempty(notqualifiedsumdeptcountvos)) {
            // 创建数据概览1-不合格次数的sheet使用的map
            map<string, object> notqualifiedsumexportmap = new hashmap<>(16);
            string notqualifiedsumtitle = messagesource.getmessage("export.check.item.rank0.not.qualified.sum.title", null, locale);
            string notqualifiedsumsheetname = messagesource.getmessage("export.check.item.rank.not.qualified.sheet.name", null, locale);
            exportparams notqualifiedsumexportparams = new exportparams(notqualifiedsumtitle, notqualifiedsumsheetname, exceltype.xssf);
            list<excelexportentity> notqualifiedsumcollist = new arraylist<>();
            list<map<string,object>> notqualifiedsumreslist = new arraylist<>();
            try {
                excelutils.getexcelexportmap(notqualifiedsumcollist, notqualifiedsumreslist, notqualifiedsumdeptcountvos, notqualifiedsumdeptcountvo.class, locale);
            } catch (exception e) {
                log.error("getnotqualifiedsumexportsheet", e);
            }
            notqualifiedsumexportmap.put("title", notqualifiedsumexportparams);
            notqualifiedsumexportmap.put("entitylist", notqualifiedsumcollist);
            notqualifiedsumexportmap.put("data", notqualifiedsumreslist);
            sheetslist.add(notqualifiedsumexportmap);
        }
    }

五、excelutils

package com.ovopark.check.util;

import cn.afterturn.easypoi.excel.annotation.excel;
import cn.afterturn.easypoi.excel.entity.exportparams;
import cn.afterturn.easypoi.excel.entity.enmus.exceltype;
import cn.afterturn.easypoi.excel.entity.params.excelexportentity;
import cn.hutool.core.util.reflectutil;
import com.ovopark.check.service.impl.myexcelexportservice;
import java.lang.reflect.field;
import java.util.arraylist;
import java.util.collection;
import java.util.hashmap;
import java.util.list;
import java.util.locale;
import java.util.map;
import javax.servlet.http.httpservletresponse;
import org.apache.poi.xssf.streaming.sxssfworkbook;
import org.springframework.context.messagesource;

/**
 * @author: chenheng
 * @create: 2022-05-25 09:20
 * @description:
 **/
public class excelutils {
  /**
   * 用于国际化
   */
  private static messagesource messagesource = springcontextutils.getbean(messagesource.class);
  /**
   * 一个excel 创建多个sheet
   * @param list
   * @return
   */
  public static sxssfworkbook exportexcel(list<map<string, object>> list) {
    sxssfworkbook workbook = new sxssfworkbook();
    for (map<string, object> map : list) {
      myexcelexportservice service = new myexcelexportservice();
      service.createsheetwithlist(workbook, (exportparams) map.get("title"), exportparams.class,
          (list<excelexportentity>) map.get("entitylist"), (collection<?>) map.get("data"));
    }
    return workbook;
  }

  public static void getexcelexportmap(list<excelexportentity> collist, list<map<string,object>> reslist,
      list list, class<?> pojoclass, locale locale) throws illegalaccessexception {
    field[] classfields = reflectutil.getfields(pojoclass);
    //需要导出的属性list
    list<field> newfields = new arraylist<>();
    for (field field : classfields) {
      excel excel = field.getannotation(excel.class);
      if (excel != null) {
        excelexportentity entity = new excelexportentity();
        entity.setname(messagesource.getmessage(excel.name(), null, locale));
        entity.setkey(field.getname());
        entity.setordernum(integer.parseint(excel.ordernum()==null?"0":excel.ordernum()));
        collist.add(entity);
        newfields.add(field);
      }
    }
    //数据体
    for (object obj : list) {
      map<string, object> map = new hashmap<>();
      for (field field : newfields) {
        // 仅在获取用private修饰属性使用
        field.setaccessible(true);
        map.put(field.getname(), field.get(obj)!=null?field.get(obj):"-");
      }
      reslist.add(map);
    }
  }

}

六、myexcelexportservice

package com.ovopark.check.service.impl;

import cn.afterturn.easypoi.excel.annotation.exceltarget;
import cn.afterturn.easypoi.excel.entity.exportparams;
import cn.afterturn.easypoi.excel.entity.enmus.exceltype;
import cn.afterturn.easypoi.excel.entity.params.excelexportentity;
import cn.afterturn.easypoi.excel.export.excelexportservice;
import cn.afterturn.easypoi.exception.excel.excelexportexception;
import cn.afterturn.easypoi.exception.excel.enums.excelexportenum;
import cn.afterturn.easypoi.util.poipublicutil;
import java.lang.reflect.field;
import java.util.collection;
import java.util.list;
import lombok.extern.slf4j.slf4j;
import org.apache.poi.ss.usermodel.workbook;

/**
 * @author: chenheng
 * @create: 2022-05-25 09:26
 * @description:
 **/
@slf4j
public class myexcelexportservice extends excelexportservice {

  public void createsheetwithlist(workbook workbook, exportparams entity, class<?> pojoclass, list<excelexportentity> entitylist, collection<?> dataset) {
    if (logger.isdebugenabled()) {
      logger.debug("excel export start ,class is {}", pojoclass);
      logger.debug("excel version is {}",
          entity.gettype().equals(exceltype.hssf) ? "03" : "07");
    }
    if (workbook == null || entity == null || pojoclass == null || dataset == null) {
      throw new excelexportexception(excelexportenum.parameter_error);
    }
    try {
      list<excelexportentity> excelparams = entitylist;
      // 得到所有字段
      field[] fileds = poipublicutil.getclassfields(pojoclass);
      exceltarget etarget = pojoclass.getannotation(exceltarget.class);
      string targetid = etarget == null ? null : etarget.value();
      getallexcelfield(entity.getexclusions(), targetid, fileds, excelparams, pojoclass,
          null, null);
      //获取所有参数后,后面的逻辑判断就一致了
      createsheetformap(workbook, entity, excelparams, dataset);
    } catch (exception e) {
      logger.error(e.getmessage(), e);
      throw new excelexportexception(excelexportenum.export_error, e.getcause());
    }
  }
}

参考:

easypoi官网

easypoi实现多sheet和列数的动态生成

到此这篇关于使用easypoi实现多sheet页导出的示例代码的文章就介绍到这了,更多相关easypoi多sheet页导出内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

idea导入若依项目教程

03-08

Java获取Object中Value的实现方法

03-08

Java将文件按照指定格式切分成多个文件

03-08

利用EasyPOI实现多sheet和列数的动态生成

03-08

Java实现markdown格式内容转换为word

03-08

SpringBoot如何实现调用controller和Service层方法

03-08

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论