SoFunction
Updated on 2025-04-11

Sample code for implementing multi-Sheet page export using EasyPoi

Preface

Because I encountered the need to export multiple Sheet pages many times, I recorded them for subsequent reference

1. Pom dependency

<!-- integratedeasypoiComponents .Exportexcel / -->
            <dependency>
                <groupId></groupId>
                <artifactId>easypoi-base</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId></groupId>
                <artifactId>easypoi-web</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId></groupId>
                <artifactId>easypoi-annotation</artifactId>
                <version>3.2.0</version>
            </dependency>

2. Main method

SXSSFWorkbook workbook = new SXSSFWorkbook();
try {
                workbook = (notQualifiedSumDeptCountVos, locale);
                return workbook;
            } catch (Exception e) {
                ("mistake",e);
                return null;
            }

3. Splicing multiple Sheet pages

private SXSSFWorkbook getSheetsList(List<Vo> notQualifiedSumDeptCountVos, Locale locale){
        // Export multiple sheets for inspection items ranking        List<Map<String, Object>> sheetsList = new ArrayList<>();
        // Create data overview 1-the sheet of the number of unqualified times        (notQualifiedSumDeptCountVos, sheetsList, locale);
        SXSSFWorkbook workbook = (sheetsList);
        return workbook;
    }

4. Get a single Sheet page

private void getNotQualifiedSumExportSheet(List<NotQualifiedSumDeptCountVo> notQualifiedSumDeptCountVos, List<Map<String, Object>> sheetsList, Locale locale){
        if ((notQualifiedSumDeptCountVos)) {
            // Create data overview 1-map used for sheets with unqualified times            Map<String, Object> notQualifiedSumExportMap = new HashMap<>(16);
            String notQualifiedSumTitle = (".", null, locale);
            String notQualifiedSumSheetName = ("", null, locale);
            ExportParams notQualifiedSumExportParams = new ExportParams(notQualifiedSumTitle, notQualifiedSumSheetName, );
            List<ExcelExportEntity> notQualifiedSumColList = new ArrayList<>();
            List<Map<String,Object>> notQualifiedSumResList = new ArrayList<>();
            try {
                (notQualifiedSumColList, notQualifiedSumResList, notQualifiedSumDeptCountVos, , locale);
            } catch (Exception e) {
                ("getNotQualifiedSumExportSheet", e);
            }
            ("title", notQualifiedSumExportParams);
            ("entityList", notQualifiedSumColList);
            ("data", notQualifiedSumResList);
            (notQualifiedSumExportMap);
        }
    }

V. ExcelUtils

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * @author: chenheng
 * @create: 2022-05-25 09:20
 * @description:
 **/
public class ExcelUtils {
  /**
    * For internationalization
    */
  private static MessageSource messageSource = ();
  /**
    * Create multiple sheets in one excel
    * @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();
      (workbook, (ExportParams) ("title"), ,
          (List<ExcelExportEntity>) ("entityList"), (Collection<?>) ("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 = (pojoClass);
    //Properties list that need to be exported    List<Field> newFields = new ArrayList<>();
    for (Field field : classFields) {
      Excel excel = ();
      if (excel != null) {
        ExcelExportEntity entity = new ExcelExportEntity();
        (((), null, locale));
        (());
        ((()==null?"0":()));
        (entity);
        (field);
      }
    }
    //Data body    for (Object obj : list) {
      Map<String, Object> map = new HashMap<>();
      for (Field field : newFields) {
        // Use only when obtaining the attributes modified with private        (true);
        ((), (obj)!=null?(obj):"-");
      }
      (map);
    }
  }

}

6. MyExcelExportService

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .slf4j.Slf4j;
import ;

/**
 * @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 (()) {
      ("Excel export start ,class is {}", pojoClass);
      ("Excel version is {}",
          ().equals() ? "03" : "07");
    }
    if (workbook == null || entity == null || pojoClass == null || dataSet == null) {
      throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
    }
    try {
      List<ExcelExportEntity> excelParams = entityList;
      // Get all fields      Field[] fileds = (pojoClass);
      ExcelTarget etarget = ();
      String targetId = etarget == null ? null : ();
      getAllExcelField((), targetId, fileds, excelParams, pojoClass,
          null, null);
      //After obtaining all parameters, the subsequent logical judgment will be consistent      createSheetForMap(workbook, entity, excelParams, dataSet);
    } catch (Exception e) {
      ((), e);
      throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, ());
    }
  }
}

refer to:

Easypoi official website

EasyPOI realizes dynamic generation of multiple sheets and column counts

This is the article about the example code for using EasyPoi to implement multi-Sheet page export. For more related content related to EasyPoi multi-Sheet page export, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!