当前位置: 首页 > 工具软件 > EasyPoi > 使用案例 >

Easypoi的简单教程

习宸
2023-12-01

公司项目中要使用easypoi,所以来做个记录
本次仅仅使用到导出execl,其他用到的时候再做更新

首先,easypoi是基于poi的,源码里也带着poi的东西
在使用easypoi的时候,使用的注解 比较多,省去了大量的格式配置等等

easypoi的整体思路是:建立实体类作为映射基础,再调用ExcelExportUtil中的各种方法满足各种操作

Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("数据", "dIntentH", ExcelType.XSSF), 
				DIntentH.class, 
				dIntentHList);
Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams("采购订单", "dIntentH", ExcelType.XSSF),
                DIntentH.class,
                excelExportServer,
                pramMap);

如上述代码,有两种导出execl的方法,一种是普通导出exportExcel,一种是大数据导出exportBigExcel,

普通导出比较简单,ExportParams,class,数据集合三个参数就够了.

大数据导出需要自己写实现类,这个实现类是做分页(分批写入execl的),不至于耗费大量的CPU和内存去处理

简单的贴上实现类的代码

package com.rc.openapi.model.EasyPoi;

import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import com.alibaba.fastjson.JSONObject;
import com.rc.openapi.domain.mysql.entity.DIntentH;
import com.rc.openapi.service.stock.DIntentHService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: 111yao_springcloud
 * @description: 大数据导出实现类
 * @author: liuqiangqiang
 * @create: 2021-03-22 16:50
 **/
@Service
public class DIntentHIExcelExportServerImpl implements IExcelExportServer {

    @Resource
    private DIntentHService dIntentHService;


    @Override
    public List<Object> selectListForExcelExport(Object queryParams, int page) {

        @SuppressWarnings("unchecked")
        Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(queryParams), Map.class);
        Integer offset = (page - 1) * 100;
        map.put("offset", offset);
        map.put("limit", 100);
        List<DIntentH> dIntentHList = dIntentHService.queryByCondition(map);
        List<Object> objects = new ArrayList<>(dIntentHList);
        if (dIntentHList.size() == 0) {
            return null;
        }
        return objects;
    }
}

这个page参数会一直做循环,必须有返回null的截止点

在controller层中要先注入IExcelExportServer

	@Autowired
    private IExcelExportServer excelExportServer;

贴上controller层的代码,没有做本地存根,直接是

/**
     * execl的导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String sourceBillNo,
                       String sourceBillType,
                       String intentType,
                       String intentNo,
                       Integer compid,
                       String busno,
                       String supplierNo,
                       String ownerNo,
                       String createTimeStart,
                       String createTimeEnd,
                       String validTimeStart,
                       String validTimeEnd,
                       Integer status,
                       Integer transferStatus) {
        Map<String, Object> pramMap = new HashMap<>(16);
        if (!StringUtil.isNull(sourceBillNo)) {
            pramMap.put("sourceBillNo", sourceBillNo);
        }
        if (!StringUtil.isNull(sourceBillType)) {
            pramMap.put("sourceBillType", sourceBillType);
        }
        /**
         * 业务来源,区分门店请货还是仓间调拨
         */
        pramMap.put("businessType", INTENT_BUSINESSTYPE);
        if (!StringUtil.isNull(intentType)) {
            pramMap.put("intentType", intentType);
        }
        if (!StringUtil.isNull(intentNo)) {
            pramMap.put("intentNo", intentNo);
        }
        if (compid != null) {
            pramMap.put("compid", compid);
        }
        if (!StringUtil.isNull(busno)) {
            pramMap.put("busno", busno);
        }
        if (!StringUtil.isNull(supplierNo)) {
            pramMap.put("supplierNo", supplierNo);
        }
        if (!StringUtil.isNull(ownerNo)) {
            pramMap.put("ownerNo", ownerNo);
        }
        if (!StringUtil.isNull(createTimeStart)) {
            pramMap.put("createTimeStart", createTimeStart);
        }
        if (!StringUtil.isNull(createTimeEnd)) {
            pramMap.put("createTimeEnd", createTimeEnd);
        }
        if (!StringUtil.isNull(validTimeStart)) {
            pramMap.put("validTimeStart", validTimeStart);
        }
        if (!StringUtil.isNull(validTimeEnd)) {
            pramMap.put("validTimeEnd", validTimeEnd);
        }
        if (status != null) {
            pramMap.put("status", status);
        }
        if (transferStatus != null) {
            pramMap.put("transferStatus", transferStatus);
        }

        String fileName = "采购订单.xlsx";
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("数据", "dIntentH", ExcelType.XSSF), DIntentH.class, dIntentHList);
        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams("采购订单", "dIntentH", ExcelType.XSSF),
                DIntentH.class,
                excelExportServer,
                pramMap);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 类似资料: