easyexcel 输出指定列

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

easyexcel 输出指定列

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

示例


header 类

Product

@Data
public class Product {

    private Integer id;

    private String productName;

    private Double price;

    @ExcelIgnore
    private String desc;
}

Product2

@Data
public class Product2 {

    private Integer id;
    private String productName;
    private Double price;
    private String desc;
}

测试类

Test

public class Test4 {

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

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

        for (int i=0;i<5;i++){
            Product product=new Product();
            product.setId(i);
            product.setProductName("海贼王"+i);
            product.setPrice((double)(i+10));
            product.setDesc("动漫");

            list.add(product);
        }

        return list;
    }

    public static void main(String[] args){
        ExcelWriter excelWriter=null;

        Set<String> excludeColumns=new HashSet<>();
        excludeColumns.add("id");

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

            WriteSheet writeSheet=EasyExcel.writerSheet(0,"sheet").head(Product.class).build();
            WriteSheet writeSheet2=EasyExcel.writerSheet(1,"sheet2").head(Product2.class)
                    .excludeColumnFiledNames(excludeColumns).build();

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

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

使用测试

                 

                 

相关文章