通过Controller导出数据库表到Excel --改进版2

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

初步进行筛选需要显示后的信息源代码如下:

@RequestMapping(value = "exportAll",method = {RequestMethod.GET})
    public void exportAll(Map query, HttpServletResponse response) {
        String[] title = new String[]{"类型","订单状态","创建时间","买家编号", "产品名称","发货人",
                "总数量", "总金额","产品类型","商品类型", "是否为礼品券","是否为自提订单","产品单价",
                "supplier","orderno", "creator","appid","seller","代理级别名称"};
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("订单表");
        sheet.setDefaultColumnWidth(15);
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFCell cell = null;
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        try {
            List list = orderService.queryListByMap(query);
            if (list != null && list.size() > 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                for (int i = 0, len = list.size(); i < len; i++) {
                    Order o = list.get(i);
                    Product product = productService.selectById(o.getProid());
                    row = sheet.createRow(i + 1);
                    //0-setCellValue不能为空,进行判断
                    //类型--OrderConstants
                    //零售和发货需要单独建表
                    if (o.getType()==null){
                        row.createCell(0).setCellValue("未定义");
                    }else if (o.getType()==1){
                        row.createCell(0).setCellValue("采购");
                    }else if (o.getType()==2){
                        row.createCell(0).setCellValue("发货");
                    }else if (o.getType()==3){
                        row.createCell(0).setCellValue("代理");
                    }else if (o.getType()==4){
                        row.createCell(0).setCellValue("购买代理资格");
                    }else if (o.getType()==5){
                        row.createCell(0).setCellValue("资格升级");
                    }else if (o.getType()==6){
                        row.createCell(0).setCellValue("零售(直接购买)");
                    }else if (o.getType()==7){
                        row.createCell(0).setCellValue("零售(购物车购买)");
                    }else if (o.getType()==8){
                        row.createCell(0).setCellValue("零售订单拆分后的销售订单");
                    }else if (o.getType()==9){
                        row.createCell(0).setCellValue("零售订单拆分后的发货订单");
                    }
                    //1-订单状态-state-OrderConstants
switch (o.getStatus()){
            case 0:
                row.createCell(1).setCellValue("未定义");
                break;
            case 1:
                row.createCell(1).setCellValue("未付款");
                break;
            case 2:
                row.createCell(1).setCellValue("未确认");
                break;
            case 3:
                row.createCell(1).setCellValue("未发货");
                break;
            case 4:
                row.createCell(1).setCellValue("发货");
                break;
            case 5:
                row.createCell(1).setCellValue("买家拒绝");
                break;
            case 6:
                row.createCell(1).setCellValue("卖家拒绝");
                break;
            case 7:
                row.createCell(1).setCellValue("平台赞同");
                break;
            case 8:
                row.createCell(1).setCellValue("平台拒绝");
                break;
            case 9:
                row.createCell(1).setCellValue("完成");
                break;
            case 10:
                row.createCell(1).setCellValue("已发货未确认");
                break;
            case 11:
                row.createCell(1).setCellValue("已确认未发货");
                break;
            default:/*
                        * 不显示取消的订单
                        * */
                row.createCell(1).setCellValue("订单取消");
                break;
        }
                    
                    //2-创建时间
                    if (o.getCreatetime()==null){
                        row.createCell(2).setCellValue(0);
                    }else {
                        row.createCell(2).setCellValue(sdf.format(o.getCreatetime()));
                    }
                    //3-买家编号
                    if (o.getBuyer()==null){
                        row.createCell(3).setCellValue((long)0);
                    }else {
                        row.createCell(3).setCellValue(o.getBuyer());
                    }
                    //4-产品名称--与产品编号相同--只需要一个
                    if (o.getProname()==null){
                        row.createCell(4).setCellValue("(null)");
                    }else {
                        row.createCell(4).setCellValue(o.getProname());
                    }
                    //5-发货人
                    if (o.getDeliver()==null){
                        row.createCell(5).setCellValue((long)0);
                    }else {
                        row.createCell(5).setCellValue(o.getDeliver());
                    }
                    //6-总数量
                    if (o.getTotal_count()==null){
                        row.createCell(6).setCellValue((long)0);
                    }else {
                        row.createCell(6).setCellValue(o.getTotal_count());
                    }
                    //7-总金额
                    if (o.getTotal_money()==null){
                        row.createCell(7).setCellValue("未定义");
                    }else {
                        row.createCell(7).setCellValue(o.getTotal_money());
                    }
                    //8-产品类型 0-单品 1-系列
                    if (o.getProtype()==null){
                        row.createCell(8).setCellValue("未定义");
                    }else if (o.getProtype() == 0){
                        row.createCell(8).setCellValue("单品");
                    }else if (o.getProtype() == 1){
                        row.createCell(8).setCellValue("系列");
                    }
                    //9-商品类型(0:代表虚拟商品订单,1:代表实物商品订单)
                    if (o.getGoodstype()==0){
                        row.createCell(9).setCellValue("虚拟商品订单");
                    }else if (o.getGoodstype()==1){
                        row.createCell(9).setCellValue("实物商品订单");
                    }else if (o.getGoodstype()==null){
                        row.createCell(9).setCellValue("其他");
                    }
                    //10-是否为礼品券(1代表是礼品券 0代表不是礼品券)
                    if (o.getTicketgiftnum()==null){
                        row.createCell(10).setCellValue("未定义");
                    }else if (o.getTicketgiftnum()==1){
                        row.createCell(10).setCellValue("礼品券");
                    }else if (o.getTicketgiftnum()==0){
                        row.createCell(10).setCellValue("不是礼品券");
                    }
                    //11-是否为自提订单(1-是,0-否)
                    row.createCell(11).setCellValue("未定义");
                    //12-产品单价
                    if (o.getPrice()==null){
                        row.createCell(12).setCellValue("未定义");
                    }else {
                        row.createCell(12).setCellValue(o.getPrice());
                    }
                    //13-supplier
                    if (o.getSupplier()==null){
                        row.createCell(13).setCellValue((long)0);
                    }else {
                        row.createCell(13).setCellValue(o.getSupplier());
                    }
                    //14-orderno
                    if (o.getOrderno()==null){
                        row.createCell(14).setCellValue("未定义");
                    }else {
                        row.createCell(14).setCellValue(o.getOrderno());
                    }
                    //15-creator
                    if (o.getCreator()==null){
                        row.createCell(15).setCellValue((long)0);
                    }else {
                        row.createCell(15).setCellValue(o.getCreator());
                    }
                    //16-appid
                    if (o.getAppid()==null){
                        row.createCell(16).setCellValue("未定义");
                    }else {
                        row.createCell(16).setCellValue(o.getAppid());
                    }
                    //17-seller
                    if (o.getSeller()==null){
                        row.createCell(17).setCellValue((long)0);
                    }else {
                        row.createCell(17).setCellValue(o.getSeller());
                    }
                    //18-代理级别名称
                    if (o.getAgentname()==null){
                        row.createCell(18).setCellValue("未定义");
                    }else {
                        row.createCell(18).setCellValue(o.getAgentname());
                    }
                }
            }
            OutputStream out = response.getOutputStream();
            response.setHeader("content-disposition", "attachment; filename=exportAll.xls");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            wb.write(out);
            out.close();
        } catch (Exception e) {
            log.error("导出失败,{}", e);
        }
    }

导出效果大致如下:

通过Controller导出数据库表到Excel --改进版2_第1张图片

以下为改进要求:

  1. 类型:进行判断,是否为零售或者发货。零售和发货需要单独建表。 2.订单状态:判断订单状态是否为取消,不显示取消订单。(continue跳出) 3.买家编号和creator数据一样,可以考虑只需要一个。 4.是否为自提订单:数据为空,先保留。建议放在最后,以便删除。 5.产品单价需要通过OP和ROP表查询,price为单价。 6.supplier。。。忘了在哪张表,以后再补。 7.creator和seller表有的数据相似,貌似在person表中查询,但person表有数据为空,待研究。 8.seller表如果为空或0,则显示supplier的name。 9.需要显示订单的总价,通过OP和ROP表查询。如果有成交价格,则总价=成交价格X数量;如没有成交价格,则总价=TM。 10.appid:通过WA表查询,显示name。 11.OP,ROP中的PN要单独将其中的每一个都显示出来。 12.零售和发货需要单独建表要求: {
    1. 需要显示快递信息,通过DO表查询。 2.快递信息包含:快递名E,快递价格EM(为空时,默认为0)。 3.地址PCD+A。 4.通过OI查询O表,OI=I-->显示其他信息。 5.显示客户信息。 }

分割

代码还未修改完成,持续更新中。

@RequestMapping(value = "exportAll",method = {RequestMethod.GET})
    public void exportAll(Map query, HttpServletResponse response) {
        String[] title = new String[]{"类型","订单状态","创建时间","Creator", "产品名称","发货人",
                "总数量", "总金额","产品类型","商品类型", "是否为礼品券","产品单价", "supplier",
                "orderno","公众号相关","seller","代理级别名称","是否为自提订单","总价"};
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("订单表");
        sheet.setDefaultColumnWidth(15);
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFCell cell = null;
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        try {
            List list = orderService.queryListByMap(query);
            if (list != null && list.size() > 0) {
                int i=0;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                for (int i = 0, len = list.size(); i < len; i++) {
//                    Order o = list.get(i);
                for (Order o:list){
//                    row = sheet.createRow(i + 1);
                    //0-setCellValue不能为空,进行判断
                    //类型--OrderConstants
                    //判断订单状态是否为取消,如果为取消则不显示
                    if (o.getStatus() == null){
                        continue;
                    }else if (o.getStatus() == -1){
                        continue;
                    }else if (o.getStatus() != -1) {
                        Personal personal = personalService.selectById(o.getCreator());
                        List retailOrderProduct = orderService.selectRetailOrderProductByOrderId(o.getAppid(),o.getId());
                        RetailOrderProduct rop = retailOrderProduct.get(i);
                        List orderProducts =  orderService.getOrderProducts(o.getAppid(),o);
                        OrderProduct op = orderProducts.get(i);
                        row = sheet.createRow(i + 1);
                        i++;
                        //0-零售和发货需要单独建表
                        switch (o.getType()){
            case 1:
                row.createCell(0).setCellValue("采购");
                break;
            case 2:
                row.createCell(0).setCellValue("发货");
                break;
            case 3:
                row.createCell(0).setCellValue("代理");
                break;
            case 4:
                row.createCell(0).setCellValue("购买代理资格");
                break;
            case 5:
                row.createCell(0).setCellValue("资格升级");
                break;
            case 6:
                row.createCell(0).setCellValue("零售(直接购买)");
                break;
            case 7:
                row.createCell(0).setCellValue("零售(购物车购买)");
                break;
            case 8:
                row.createCell(0).setCellValue("零售订单拆分后的销售订单");
                break;
            case 9:
                row.createCell(0).setCellValue("零售订单拆分后的发货订单");
                break;
            default:
                row.createCell(0).setCellValue("未定义");
                break;
        }
                        //1-订单状态-state-OrderConstants
                       switch (o.getStatus()){
            case 0:
                row.createCell(1).setCellValue("未定义");
                break;
            case 1:
                row.createCell(1).setCellValue("未付款");
                break;
            case 2:
                row.createCell(1).setCellValue("未确认");
                break;
            case 3:
                row.createCell(1).setCellValue("未发货");
                break;
            case 4:
                row.createCell(1).setCellValue("发货");
                break;
            case 5:
                row.createCell(1).setCellValue("买家拒绝");
                break;
            case 6:
                row.createCell(1).setCellValue("卖家拒绝");
                break;
            case 7:
                row.createCell(1).setCellValue("平台赞同");
                break;
            case 8:
                row.createCell(1).setCellValue("平台拒绝");
                break;
            case 9:
                row.createCell(1).setCellValue("完成");
                break;
            case 10:
                row.createCell(1).setCellValue("已发货未确认");
                break;
            case 11:
                row.createCell(1).setCellValue("已确认未发货");
                break;
            default:/*
                        * 不显示取消的订单
                        * */
                row.createCell(1).setCellValue("订单取消");
                break;
        }
                        //2-创建时间
                        if (o.getCreatetime() == null) {
                            row.createCell(2).setCellValue(0);
                        } else {
                            row.createCell(2).setCellValue(sdf.format(o.getCreatetime()));
                        }
                        //3-Creator
                        if (o.getCreator() == null) {
                            row.createCell(3).setCellValue("未定义");
                        } else {
                            if (personal != null) {
                                row.createCell(3).setCellValue(personal.getName());
//                                row.createCell(3).setCellValue(o.getCreator());
                            }else {
                                row.createCell(3).setCellValue("未定义");
                            }
                        }
                        //4-产品名称--与产品编号相同--只需要一个
                        if (o.getProname() == null) {
                            row.createCell(4).setCellValue("(null)");
                        } else {
                            row.createCell(4).setCellValue(o.getProname());
                        }
                        //5-发货人
                        if (o.getDeliver() == null) {
                            row.createCell(5).setCellValue((long) 0);
                        } else {
                            row.createCell(5).setCellValue(o.getDeliver());
                        }
                        //6-总数量
                        if (o.getTotal_count() == null) {
                            row.createCell(6).setCellValue((long) 0);
                        } else {
                            row.createCell(6).setCellValue(o.getTotal_count());
                        }
                        //7-总金额
                        if (o.getTotal_money() == null) {
                            row.createCell(7).setCellValue("未定义");
                        } else {
                            row.createCell(7).setCellValue(o.getTotal_money());
                        }
                        //8-产品类型 0-单品 1-系列
                        if (o.getProtype() == null) {
                            row.createCell(8).setCellValue("未定义");
                        } else if (o.getProtype() == 0) {
                            row.createCell(8).setCellValue("单品");
                        } else if (o.getProtype() == 1) {
                            row.createCell(8).setCellValue("系列");
                        }
                        //9-商品类型(0:代表虚拟商品订单,1:代表实物商品订单)
                        if (o.getGoodstype() == 0) {
                            row.createCell(9).setCellValue("虚拟商品订单");
                        } else if (o.getGoodstype() == 1) {
                            row.createCell(9).setCellValue("实物商品订单");
                        } else if (o.getGoodstype() == null) {
                            row.createCell(9).setCellValue("其他");
                        }
                        //10-是否为礼品券(1代表是礼品券 0代表不是礼品券)
                        if (o.getTicketgiftnum() == null) {
                            row.createCell(10).setCellValue("未定义");
                        } else if (o.getTicketgiftnum() == 1) {
                            row.createCell(10).setCellValue("礼品券");
                        } else if (o.getTicketgiftnum() == 0) {
                            row.createCell(10).setCellValue("不是礼品券");
                        }
                        //11-产品单价
                        if (o.getPrice() == null) {
                            row.createCell(11).setCellValue("未定义");
                        } else {
                            row.createCell(11).setCellValue(o.getPrice());
                        }
                        //12-supplier
                        if (o.getSupplier() == null) {
                            row.createCell(12).setCellValue((long) 0);
                        } else {
                            row.createCell(12).setCellValue(o.getSupplier());
                        }
                        //13-orderno
                        if (o.getOrderno() == null) {
                            row.createCell(13).setCellValue("未定义");
                        } else {
                            row.createCell(13).setCellValue(o.getOrderno());
                        }
                        //14-公众号相关
                        if (o.getAppid() == null) {
                            row.createCell(14).setCellValue("未定义");
                        } else {
                            Wbapp wbapp = wbAppService.findByAppid(o.getAppid());
                            row.createCell(14).setCellValue(wbapp.getAppname());
                        }
                        //15-seller
                        if (o.getSeller() == null) {
                            if (o.getSupplier() == null) {
                                row.createCell(15).setCellValue("未定义");
                            } else {
                                row.createCell(15).setCellValue(o.getSupplier());
                            }
                        } else {
//                            Personal personal = personalService.selectById(o.getCreator());
                            row.createCell(15).setCellValue(personal.getName());
                        }
                        //16-代理级别名称
                        if (o.getAgentname() == null) {
                            row.createCell(16).setCellValue("未定义");
                        } else {
                            row.createCell(16).setCellValue(o.getAgentname());
                        }
                        //17-是否为自提订单(1-是,0-否)
                        row.createCell(17).setCellValue("未定义");
                        //18-总价
                        if (o.getType()==6 || o.getType()==7 || o.getType()==8 || o.getType()==9){
                            if (rop.getDealprice() == null) {
                                row.createCell(18).setCellValue(rop.getTotalmoney());
                            }else if (rop.getDealprice() != null){
                                BigDecimal m = new BigDecimal(Float.parseFloat(rop.getDealprice()));
                                BigDecimal n = new BigDecimal(rop.getNumber());
                                row.createCell(18).setCellValue(String.valueOf(m.multiply(n)));
                            }
                        }else{
//                            if (op.getPrice() == null){
                                row.createCell(18).setCellValue(op.getTotalmoney());
//                            }else if (op.getPrice() != null){
//                                BigDecimal m = new BigDecimal(Float.parseFloat(op.getPrice()));
//                                BigDecimal n = new BigDecimal();
//                                row.createCell(18).setCellValue(op.getTotalmoney());
//                            }
                        }
                    }
                }
            }
            OutputStream out = response.getOutputStream();
            response.setHeader("content-disposition", "attachment; filename=exportAll.xls");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            wb.write(out);
            out.close();
        } catch (Exception e) {
            log.error("导出失败,{}", e);
        }
    }

转载于:https://my.oschina.net/LYQ19941105/blog/749193

你可能感兴趣的:(通过Controller导出数据库表到Excel --改进版2)