战狂粗人张

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

例子代码:

第一步:

        <!--Excel包-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beta5</version>
        </dependency>

 

第二步:

    @Test
    public void  ExportTest() throws IOException {
        //指定文件输出位置
        OutputStream outputStream =new FileOutputStream("H:/excel/file/myexcel3.xlsx");
        ExcelWriter excelWriter =EasyExcelFactory.getWriter(outputStream);
        //将要输出的内容填充到Sheet里
        Sheet sheet =new Sheet(1,0,ExcelModel.class );
        //设置sheet表名
        sheet.setSheetName("my_three_excel");
        /**
         * 写数据到Write上下文中
         * 第一个参数:要写入的内容
         * 第二个参数:要写入的sheet目标
         */
        excelWriter.write(createModelList2(),sheet);
        excelWriter.finish();
        outputStream.close();
    }

 

第三步:

@Data
public class ExcelMode extends BaseRowModel {
    @ExcelProperty(value = "姓名" ,index = 0)
    private  String  userName;
    @ExcelProperty(value = "年龄" ,index = 1)
    private String age;
    @ExcelProperty(value = "住址" ,index = 2)
    private String address;
}

private List<ExcelMode> createModelList (){
        List<ExcelMode> list = new ArrayList<>();
        for(int i=0; i<20;i++){
            ExcelMode excelMode = new ExcelMode();
            excelMode.setUserName("哒哒"+i);
            excelMode.setAge("22");
            excelMode.setAddress("广西");
            list.add(excelMode);
        }
        return list;
    }

 

第四步:

@Data
public class ExcelModel2 extends BaseRowModel {
    @ExcelProperty(value = {"name","name"},index = 0)
    private  String name;
    @ExcelProperty(value ={"age","age"},index = 1)
    private  String age;
    @ExcelProperty(value={"cash_value","高"},index = 2)
    private String cashvalue_high ;
    @ExcelProperty(value={"cash_value","中"},index = 3)
    private String cashvalue_during ;
    @ExcelProperty(value={"cash_value","低"},index = 4)
    private String cashvalue_low ;

}

 

posted on 2020-01-12 03:20  战狂粗人张  阅读(2502)  评论(0编辑  收藏  举报