easyExcel实现多个sheet页导出,main方法测试。

之前小编写过一篇有关于easyExcel实现多个sheet页导出的文章,但有人看不懂,所以小编写了一个main方法,以帮助让你更通俗易懂,先上结果:

main方法代码如下:

package easyExcel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class test {
    public static void main(String[] args) {
        //FileUtil.mkdir("D:\\test");创建测试文件夹,第一次运行之后注释掉,
        File file = new File("D:\\test\\test.xlsx");
        //准备第一个sheet页的数据
        list1 list1 = new list1();
        list1.setName("张三");
        list1.setAge(20);

        list1Data list1Data = new list1Data();
        List<list1Data> list2Data1 = new ArrayList<>();
        BeanUtil.copyProperties(list1, list1Data);
        list2Data1.add(list1Data);


        //准备第二个sheet页的数据
        list1 list2 = new list1();
        list2.setName("李四");
        list2.setAge(22);

        list2Data list2Data = new list2Data();
        BeanUtil.copyProperties(list2, list2Data);
        List<list2Data> list2Data2 = new ArrayList<>();
        list2Data2.add(list2Data);

        try (ExcelWriter excelWriter = EasyExcel.write(file).build();) {//file文件;
            WriteSheet sheet = EasyExcel.writerSheet("张三信息").head(list1Data.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            WriteSheet build = EasyExcel.writerSheet("李四信息").head(list2Data.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            excelWriter.write(list2Data1, sheet);
            excelWriter.write(list2Data2, build);
            System.out.println("运行成功");

        }


    }
}

如下是sheet页的对象:

package easyExcel;

import lombok.Data;


@Data
public class list1 {
    private String name;
    private int age;

}

package easyExcel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import lombok.Data;
import lombok.EqualsAndHashCode;


@Data
@EqualsAndHashCode
//头字体大小
@HeadFontStyle(fontHeightInPoints = 12)
//内容字体大小
@ContentFontStyle(fontHeightInPoints = 10)

public class list1Data {
    @ExcelProperty("姓名")
    private String name;

    @ExcelProperty("年龄")
    private int age;
}

package easyExcel;


import lombok.Data;


@Data

public class list2 {
    private String name;
    private int age;
}


 

package easyExcel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import lombok.Data;
import lombok.EqualsAndHashCode;


@Data
@EqualsAndHashCode
//头字体大小
@HeadFontStyle(fontHeightInPoints = 12)
//内容字体大小
@ContentFontStyle(fontHeightInPoints = 10)

public class list2Data {
    @ExcelProperty("姓名")
    private String name;

    @ExcelProperty("年龄")
    private int age;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值