easyexcel 列表填充

x33g5p2x  于2021-12-28 转载在 其他  
字(3.5k)|赞(0)|评价(0)|浏览(1411)

easyexcel 列表填充

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

示例

Test

@Data
class Student{

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

public class Test {

    private static final String template_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"template2.xlsx";
    private static final String fill_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill3.xlsx";
    private static final String fill2_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill4.xlsx";
    private static final String fill3_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill5.xlsx";
    private static final String fill4_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill6.xlsx";
    private static final String fill5_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill7.xlsx";

    public static void fill(){
        EasyExcel.write(fill_path).withTemplate(template_path).sheet().doFill(data());
    }

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

        try{
            excelWriter=EasyExcel.write(fill2_path).withTemplate(template_path).build();
            WriteSheet writeSheet=EasyExcel.writerSheet("Sheet2").build();

            Map<String,Object> map=new HashMap<>();
            map.put("date",new Date());
            map.put("total",5);

            FillConfig fillConfig= FillConfig.builder().forceNewRow(true).build();
            excelWriter.fill(data(),fillConfig,writeSheet);
            excelWriter.fill(map,writeSheet);
        }finally {
            if (excelWriter!=null){
                excelWriter.finish();
            }
        }
    }

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

        try {
            excelWriter=EasyExcel.write(fill3_path).withTemplate(template_path).registerConverter(new CustomConverter()).build();
            WriteSheet writeSheet=EasyExcel.writerSheet("Sheet3").build();

            Map<String,Object> map=new HashMap<>();
            map.put("date", LocalDateTime.now());

            excelWriter.fill(data(),writeSheet);
            excelWriter.fill(map,writeSheet);

            List<List<String>> lists=new ArrayList<>();

            List<String> list=new ArrayList<>();
            list.add(null);
            list.add(null);
            list.add("total:"+5);
            lists.add(list);

            excelWriter.write(lists,writeSheet);
        }finally {
            if (excelWriter!=null){
                excelWriter.finish();
            }
        }
    }

    public static void fill4(){
        FillConfig fillConfig= FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        EasyExcel.write(fill4_path).withTemplate(template_path).sheet("Sheet4").doFill(data(),fillConfig);
    }

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

        try{
            excelWriter=EasyExcel.write(fill5_path).withTemplate(template_path).build();
            WriteSheet writeSheet=EasyExcel.writerSheet("Sheet5").build();
            FillConfig fillConfig= FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();

            excelWriter.fill(new FillWrapper("data1",data()),writeSheet);
            excelWriter.fill(new FillWrapper("data2",data()),fillConfig,writeSheet);
        }finally {
            if (excelWriter!=null){
                excelWriter.finish();
            }
        }
    }

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

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

            list.add(student);
        }

        return list;
    }

    public static void main(String[] args){
        fill();
        fill2();
        fill3();
        fill4();
        fill5();
    }
}

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

使用测试


竖向填充

                          

                          


竖向复杂填充

                          

                          


竖向复杂大数据量填充

                          

                          


横向填充

                          

                          


带前缀填充

                          

                          

相关文章