欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

easyexcel 使用table写入

程序员文章站 2022-07-05 21:12:51
easyexcel使用table写入***********************示例...

easyexcel 使用table写入

 

 

***********************

示例

 

Test

@Data
class Person{

    private Integer id;
    private String name;
    private Integer age;
}

@Data
class Fruit{

    private Integer id;
    private String name;
    private Double price;
}

public class Test {

    private static final String write_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write2.xlsx";

    private static List<Person> data(){
        List<Person> list=new ArrayList<>();

        for (int i=0;i<5;i++){
            Person person=new Person();
            person.setId(i);
            person.setName("瓜田李下"+i);
            person.setAge(20+i);

            list.add(person);
        }

        return list;
    }

    private static List<Fruit> data2(){
        List<Fruit> list=new ArrayList<>();

        for (int i=0;i<5;i++){
            Fruit fruit=new Fruit();
            fruit.setId(i);
            fruit.setName("apple"+i);
            fruit.setPrice((double)(4+i));

            list.add(fruit);
        }

        return list;
    }

    public static void write(){
        ExcelWriter excelWriter=null;

        try{
            excelWriter= EasyExcel.write(write_path).build();

            WriteSheet writeSheet=EasyExcel.writerSheet().build();

            WriteTable writeTable=EasyExcel.writerTable(0).head(Person.class).needHead(true).build();
            WriteTable writeTable2=EasyExcel.writerTable(1).head(Fruit.class).needHead(true).build();

            excelWriter.write(data(),writeSheet,writeTable);
            excelWriter.write(data2(),writeSheet,writeTable2);
        }finally {
            if (excelWriter!=null){
                excelWriter.finish();
            }
        }
    }

    public static void main(String[] args){
        write();
    }
}

 

************

使用测试

 

                    easyexcel 使用table写入

 

 

本文地址:https://blog.csdn.net/weixin_43931625/article/details/107589581

相关标签: easyexcel