小试阿里巴巴EasyExcel导出Excel

添加easyexcel的maven依赖

       <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>1.1.1</version>
            </dependency>

导出的模型需要继承BaseRowModel,然后可以在每个属性上设置所在的列和一些format


public class GoodsOrderExportDto extends BaseRowModel {

    @ExcelProperty(value = "订单序号",index = 0)
    private Long orderId;

    @ExcelProperty(value = "订单编号",index = 1)
    private String orderCode;

    @ExcelProperty(value = "商品序号",index = 2)
    private Long goodsId;

    @ExcelProperty(value = "商品名称",index = 3)
    private String goodsTitle;

    @ExcelProperty(value = "商品链接",index = 4)
    private String goodsDetailsUrl;

    @ExcelProperty(value = "商品图片",index = 5)
    private String goodsPhoto;

    @ExcelProperty(value = "商品数量",index = 6)
    private Long goodsNum;

    @ExcelProperty(value = "商品价格",index = 7)
    private BigDecimal goodsAmt;

    @ExcelProperty(value = "区域公司",index = 8)
    private Long areaComp;
   
    @ExcelProperty(value = "付款时间",index = 12,format = "yy-MM-dd hh:mm:ss")
    private Date payTime;

}

最后在控制器输出


 @RequestMapping(value = "export",method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<GoodsOrderDto> orderDtoList = goodsOrderServiceRemote.querGoodsOrderList();
        List<GoodsOrderExportDto> orderExportDtoList = Collections.emptyList();
        if (CollectionUtils.isNotEmpty(orderDtoList)){
            orderExportDtoList = new ArrayList<>(orderDtoList.size());
            for (GoodsOrderDto goodsOrderDto : orderDtoList) {
                GoodsOrderExportDto goodsOrderExportDto = new GoodsOrderExportDto();
                BeanUtils.copyProperties(goodsOrderDto,goodsOrderExportDto);

                orderExportDtoList.add(goodsOrderExportDto);
            }
            orderDtoList.clear();
        }

        // 设定输出文件头
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("订单导出.xls", "UTF-8"));
        // 定义输出类型
        response.setContentType("application/msexcel");

        OutputStream outputStream = response.getOutputStream();

        ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLS);

        Sheet sheet = new Sheet(1, 0, GoodsOrderExportDto.class);

        excelWriter.write(orderExportDtoList,sheet);

        outputStream.flush();
        excelWriter.finish();
        outputStream.close();
    }

相关文章

此处评论已关闭