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>-->