zoukankan      html  css  js  c++  java
  • easyexcel(一)写

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>1.1.2-beat1</version>
            </dependency>

    代码

    public class WriteExcel {
    
        //无映射,有表头
        @Test
        public void noModel() throws FileNotFoundException {
            OutputStream out=new FileOutputStream("test2.xls");
            ExcelWriter writer=new ExcelWriter(out, ExcelTypeEnum.XLS);
            //创建sheet页
            Sheet sheet=new Sheet(1,0);
            sheet.setSheetName("sheet1");
            //写入的表数据
            List<List<String>> data=new ArrayList<>();
            for(int i=0;i<5;i++){
                List<String> item=new ArrayList<>();
                item.add("tom"+i);
                item.add(String.valueOf(10+i));
                item.add("cn");
                data.add(item);
            }
            //表头(列名)数据
            List<List<String>> head=new ArrayList<>();
            List<String> c1=new ArrayList<>();
            c1.add("姓名");
            List<String> c2=new ArrayList<>();
            c2.add("年龄");
            List<String> c3=new ArrayList<>();
            c3.add("地址");
            head.add(c1);
            head.add(c2);
            head.add(c3);
    
            Table table=new Table(1);
            table.setHead(head);
            //写入表
            writer.write0(data,sheet,table);
            writer.finish();
        }
    
    
        //有映射,有表头
        @Test
        public void withModel() throws FileNotFoundException {
            FileOutputStream out=new FileOutputStream("test2.xls");
            ExcelWriter writer=new ExcelWriter(out,ExcelTypeEnum.XLS);
            //创建sheet,传入StudentModel.class(继承自BaseRowModel)参数设置表头
            Sheet sheet=new Sheet(1,0,StudentModel.class);
            sheet.setSheetName("sheet1");
            //设置写入的数据
            List<StudentModel> data=new ArrayList<>();
            for(int i=0;i<10;i++){
                StudentModel student=new StudentModel();
                student.setName("tom"+i);
                student.setAge("1"+i);
                student.setScore("6"+i);
                data.add(student);
            }
            //写入表
            writer.write(data,sheet);
            writer.finish();
        }
      //映射模型类
    static class StudentModel extends BaseRowModel{ @ExcelProperty(value = "姓名",index = 0) private String name; @ExcelProperty(value = "年龄",index = 1) private String age; @ExcelProperty(value = "分数",index = 2) private String score;      //Getter and Setter } }

     @ExcelProperty :映射模型类中使用,用来设置表头索引和名称

  • 相关阅读:
    个人工作总结07
    uboot是用来干什么的,有什么作用?
    交叉编译器的命名规则及详细解释(arm/gnu/none/linux/eabi/eabihf/gcc/g++)
    C++中explicit关键字的使用
    ubuntu 虚拟机下使用摄像头
    用CMAKE编译OpenCV 3.4.2+Opencv Contrib 3.4生成可执行包
    人脸识别:objectDetection
    opencv 3 -- waitKey()函数
    CreateThread与_beginthreadex本质区别
    lib文件和dll文件
  • 原文地址:https://www.cnblogs.com/yjh1995/p/13553764.html
Copyright © 2011-2022 走看看