easyexcel 使用table写入

布满荆棘的人生 2023-03-01 12:49 3阅读 0赞

easyexcel 使用table写入

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

示例

Test

  1. @Data
  2. class Person{
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. }
  7. @Data
  8. class Fruit{
  9. private Integer id;
  10. private String name;
  11. private Double price;
  12. }
  13. public class Test {
  14. private static final String write_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write2.xlsx";
  15. private static List<Person> data(){
  16. List<Person> list=new ArrayList<>();
  17. for (int i=0;i<5;i++){
  18. Person person=new Person();
  19. person.setId(i);
  20. person.setName("瓜田李下"+i);
  21. person.setAge(20+i);
  22. list.add(person);
  23. }
  24. return list;
  25. }
  26. private static List<Fruit> data2(){
  27. List<Fruit> list=new ArrayList<>();
  28. for (int i=0;i<5;i++){
  29. Fruit fruit=new Fruit();
  30. fruit.setId(i);
  31. fruit.setName("apple"+i);
  32. fruit.setPrice((double)(4+i));
  33. list.add(fruit);
  34. }
  35. return list;
  36. }
  37. public static void write(){
  38. ExcelWriter excelWriter=null;
  39. try{
  40. excelWriter= EasyExcel.write(write_path).build();
  41. WriteSheet writeSheet=EasyExcel.writerSheet().build();
  42. WriteTable writeTable=EasyExcel.writerTable(0).head(Person.class).needHead(true).build();
  43. WriteTable writeTable2=EasyExcel.writerTable(1).head(Fruit.class).needHead(true).build();
  44. excelWriter.write(data(),writeSheet,writeTable);
  45. excelWriter.write(data2(),writeSheet,writeTable2);
  46. }finally {
  47. if (excelWriter!=null){
  48. excelWriter.finish();
  49. }
  50. }
  51. }
  52. public static void main(String[] args){
  53. write();
  54. }
  55. }

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

使用测试

  1. ![20200726104735929.png][]

发表评论

表情:
评论列表 (有 0 条评论,3人围观)

还没有评论,来说两句吧...

相关阅读

    相关 EasyExcel如何使用

    EasyExcel是一个Java库,用于读写Excel文件。要使用EasyExcel,您需要在项目中添加依赖并导入必要的类。 依赖信息: <dependency>

    相关 EasyExcel 使用记录

    使用的是阿里的 EasyExcel ,在读的时候出现NPE错误,追究一下 使用 EasyExcel 写excel的时候 api 非常简单好用,而在读的时候却发现有一些bu