java实现数据库数据导出到Excel并下载查看

1、首先下载工具类:http://download.csdn.net/download/ledzcl/10234291

2、直接调用工具类的中的export这个方法,放上源码。

@ResponseBody
@RequestMapping(value = "/export", method = RequestMethod.GET)
public void export(HttpServletResponse response, OrderDto dto) {
    RestResponse<List<OrderDto>> restResponse = orderRemoteService.exportOrder(getAccessToken(), dto);
    if (!restResponse.success()) {
        throw new BusinessException(restResponse.getErrorMsg());
    }
    try {
        OutputStream out = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("商品订单", "UTF-8") + ".xls");
        Collection<Object[]> collection = new ArrayList<>();
        for (OrderDto orderDto : restResponse.getData()) {
            Object[] objects = new Object[11];
            objects[0] = orderDto.getReceiver();
            objects[1] = orderDto.getReceiverPhone();
            objects[2] = orderDto.getMailingMethod();
            objects[3] = orderDto.getMailingAddress();
            objects[4] = orderDto.getReleaseName();
            objects[5] = orderDto.getPayment();
            objects[6] = orderDto.getQuantity();
            objects[7] = orderDto.getPaymentStatus();
            objects[8] = orderDto.getStatus();
            objects[9] = orderDto.getRefundState();
            objects[10] = orderDto.getCreateTime();
            collection.add(objects);
        }
        ExcelUtils.export(collection, "商品订单", new String[]{"收件人", "收件人电话", "发货类型", "发货地址",
                        "产品名称", "总金额", "商品数量", "支付状态", "订单状态", "退款状态", "订单日期"},
                new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, restResponse.getData().size(), out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
前端点击下载即可:
<button οnclick="location.href='export';" class="layui-btn layui-btn-normal">导出订单</button>
我是用maven搭建的项目所以要加入依赖:
<!-- poi解析Excel表格依赖. -->
<!--<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>-->

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值