EasyPOI一次导出多个sheet单元格

EasyPOI一次导出多个sheet单元格

使用ExcelExportUtil的以下方法:

    /**
     * 一个excel 创建多个sheet
     * 
     * @param list
     *            多个Map key title 对应表格Title key entity 对应表格对应实体 key data
     *            Collection 数据
     * @return
     */
    public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {
        Workbook workbook = getWorkbook(type,0);
        for (Map<String, Object> map : list) {
            ExcelExportService service = new ExcelExportService();
            service.createSheet(workbook, (ExportParams) map.get("title"),
                (Class<?>) map.get("entity"), (Collection<?>) map.get("data"));
        }
        return workbook;
    }

1.添加EasyPOI的支持

  <!-- easypoi的支持 -->
        <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>

2.创建实体类

重要的事情说三遍,实体类一定要有Get方法,实体类一定要有Get方法,实体类一定要有Get方法,一般Get和Set都要加进去

此次测试我直接把get和Set方法都显示出来了

2.1 Dog类

package com.Day3;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
/**
 * @author yfq
 * @create 2020-07-07 14:24
 */

public class Dog
{

    @Excel(name = "狗狗的名字")
   public String name;

    @Excel(name = "狗狗的年龄")
   public int age;

    public Dog()
    {
    }

    public Dog(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}

2.2 Cat类

package com.Day3;

import cn.afterturn.easypoi.excel.annotation.Excel;

/**
 * @author yfq
 * @create 2020-07-07 14:26
 */

public class Cat
{
    @Excel(name = "猫猫的名字")
    public String name;

    @Excel(name = "猫猫的年龄")
    public int age;

    public Cat()
    {
    }

    public Cat(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}

3.测试

package com.Day3;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 
 * @create 2020-07-07 14:27
 */
public class MoreSheetExportTest
{

    public static void main(String[] args) throws IOException
    {

        List<Map<String, Object>> list = new ArrayList<>();


        //狗狗
        HashMap<String, Object> dogMap = new HashMap<>();

            //ExportParams
            ExportParams exportParams = new ExportParams("清单一","狗狗清单");
            dogMap.put("title", exportParams);

            //Class
            dogMap.put("entity", Dog.class);

            //Collection<?> dataSet
            ArrayList<Dog> dogs = new ArrayList<>();
            dogs.add(new Dog("泰迪",2));
            dogs.add(new Dog("哈士奇",1));

            //加入到Map
            dogMap.put("data", dogs);


        list.add(dogMap);

        //猫猫
        HashMap<String, Object> CatMap = new HashMap<>();

            //ExportParams
            ExportParams exportParams1 = new ExportParams("清单二", "猫猫清单");
            CatMap.put("title", exportParams1);

            //Class
            CatMap.put("entity", Cat.class);

            //Collection<?> dataSet
            ArrayList<Cat> cats = new ArrayList<>();
            cats.add(new Cat("小花",1));
            cats.add(new Cat("小红",2));

             CatMap.put("data", cats);

        list.add(CatMap);

        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);

        workbook.write(new FileOutputStream(new File("G:\\animal.xls")));

        workbook.close();

    }
}

导出的结果:
在这里插入图片描述

在这里插入图片描述

4. 总结:

其实创建多个sheet非常简单,就是用一个大的Map存放ExportParams entity、Class<?> pojoClass、Collection<?> dataSet
注意这个Map中的key ,
title 对应着ExportParams entity
entity 对应着Class<?> pojoClass
data 对应着Collection<?> dataSet

然后将这个Map放在list中去做为 exportExcel(List<Map<String, Object>> list, ExcelType type)第一个参数,至于第二个参数ExcelType.HSSF 或ExcelType.XSSF

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值