动态从数据库获取数据,省市县三级联动,有校验…

2018-08-02 05:53:45来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

话不多说,看效果图,直接上代码。

sheet  商户表

 

 hideSheet ,功能完成后隐藏的Sheet,用于储存下拉框中的信息,(以一定的规则将所需数据存储在表格中)。

 

下面是代码

部分数据需要在导出时使用,如通过省名去找它的编码,这时就没有必要再去数据库查询,把此数据储存成全局参数,使用时只需要简单的判断即可。

四大直辖市在这里被忽略,如有需要根据实际情况去处理。

数组可使用动态数组。

 @RequestMapping(params = "action=toExcel")
    public void toExcel(HttpServletRequest request,HttpServletResponse response) {
        try {
            String name = "商户表";

            String valStr = "attachment;filename="
                    + new String(name.getBytes("gb2312"), "ISO8859-1") + ".xls";
            OutputStream os = response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            // 设定输出文件头
            response.setHeader("Content-Disposition", valStr);
            response.setContentType("application/msexcel");// 定义输出类型
            Workbook workbook = new HSSFWorkbook();
            
            String[] fieldLabelArray = { "商户简称", "商户全称", "商户类型", "父级商户", "省",
                    "市", "县", "电话", "联系人", "邮箱", "邮编", "地址", "经度",
                    "纬度" };
            
            //得到省名称
            Map<String,String> params = new HashMap<String,String>();
            //所有的省
            if(faMerchantList.size()==0){
                faMerchantList = service.findList("UU_BANK_MERCHANT.queryName", paramData);
            }
            if(merchantList.size()==0){
                merchantList = bankMerchantTypeService.findList("UU_BANK_MERCHANTTYPE.query", paramData);
            }
            if(proviceLists.size()==0){
                proviceLists = serviceOut.findList("UU_BANK_BANKOUTLET.selectProvices", paramData);
            }
            String[] provinceArr=new String[proviceLists.size()];    
            //将有子区域的父区域放到一个数组中
            ArrayList areaFatherNameArr=new ArrayList();
            Map<String,String[]> areaMap =new HashMap<String, String[]>();
            int index=0;
            int cityIndex=0;
            for(Object object:proviceLists){
                Map entry=(Map) object;
                provinceArr[index]=(String) entry.get("NAME");
                String provinceCode = (String) entry.get("CITYCODE");
                areaFatherNameArr.add(cityIndex,(String) entry.get("NAME"));
                cityIndex+=1;
                if(!provinceCode.isEmpty()){
                    String city_Code = provinceCode.substring( 0, 2);
                    params.put("CITY_CODE", city_Code);            
                    params.put("CITYCODE", provinceCode);
                }        
                List cityLists = serviceOut.findList("UU_BANK_BANKOUTLET.selectCitys", params);
                String[] areaArr= new String[cityLists.size()];
                int indexs=0;
                for(Object city:cityLists){
                    Map entryCity=(Map) city;
                    areaFatherNameArr.add(cityIndex, (String) entryCity.get("NAME"));
                    areaArr[indexs]=(String) entryCity.get("NAME");
                    String cityCode = (String) entryCity.get("CITYCODE");
                    if(!cityCode.isEmpty()){
                        String city_Code = cityCode.substring( 0, 4);
                        params.put("CITY_CODE", city_Code);            
                        params.put("CITYCODE", cityCode);
                    }            
                    List countyLists = serviceOut.findList("UU_BANK_BANKOUTLET.selectCountys", params);
                    String[] countyArr= new String[countyLists.size()];
                    int countyindex=0;
                    for(Object county:countyLists){
                        Map entryCounty=(Map) county;
                        countyArr[countyindex]=(String) entryCounty.get("NAME");
                        countyindex++;
                    }
                    areaMap.put((String) entryCity.get("NAME"), countyArr);
                    indexs++;
                    cityIndex++;
                }
                areaMap.put((String) entry.get("NAME"),areaArr);
                index++;
            };
            //查询商户类型
            int merchantArrIndex=0;
            String[] merchantArr=new String[merchantList.size()];
            for (Object merchantType : merchantList) {
                Map merchant=(Map) merchantType;
                merchantArr[merchantArrIndex]=(String) merchant.get("NAME");
                merchantArrIndex++;
            }
            //查询父级商户
            int faMerchantIndex=0;
            String[] faMerchantArr=new String[faMerchantList.size()];
            for (Object fmerchantI : faMerchantList) {
                Map merchantf=(Map) fmerchantI;
                faMerchantArr[faMerchantIndex]=(String) merchantf.get("NAME");
                faMerchantIndex++;
            }
            // 创建Sheet
            String sheetName = name;
            Sheet sheet = workbook.createSheet(name);
            //创建sheet1 
            Sheet hideSheet =workbook.createSheet("area");
            //是否隐藏sheet1
            workbook.setSheetHidden(workbook.getSheetIndex(hideSheet), true);  
            //生成数据
            int rowId=0;
            Row provinceRow = hideSheet.createRow(rowId++);
            provinceRow.createCell(0).setCellValue("省列表");  
            for(int i = 0; i < provinceArr.length; i ++){  
                Cell provinceCell = provinceRow.createCell(i + 1);  
                provinceCell.setCellValue(provinceArr[i]);  
             }  
            //将具体数据写入每一行中
            for(int i = 0;i < areaFatherNameArr.size();i++){  
                String key = (String) areaFatherNameArr.get(i);  
                String[] son = areaMap.get(key); 
                Row row = hideSheet.createRow(rowId++);  
                row.createCell(0).setCellValue(key);  
                for(int j = 0; j < son.length; j ++){  
                    Cell cell = row.createCell(j + 1);  
                    cell.setCellValue(son[j]);  
                }  

                //添加名称管理
                String range = getRange(1,rowId,son.length);
                Name names =workbook.createName();
                names.setNameName(key);
                String formula ="area!"+range;
                names.setRefersToFormula(formula);
            }

            // 商户类型
            DVConstraint typeConstraint = DVConstraint.createExplicitListConstraint(merchantArr);
            CellRangeAddressList typeRangeAddressList = new CellRangeAddressList(1, 1000, 2, 2);
            DataValidation typeDataValidation = new HSSFDataValidation(typeRangeAddressList, typeConstraint);
            typeDataValidation.createErrorBox("error", "请选择正确的商户类型");
            sheet.addValidationData(typeDataValidation);
            // 父级商户
            DVConstraint faConstraint = DVConstraint.createExplicitListConstraint(faMerchantArr);
            CellRangeAddressList faRangeAddressList = new CellRangeAddressList(1, 1000, 3, 3);
            DataValidation faDataValidation = new HSSFDataValidation(faRangeAddressList, faConstraint);
            typeDataValidation.createErrorBox("error", "请选择正确的商户类型");
            sheet.addValidationData(faDataValidation);

            // 省规则
            DVConstraint provConstraint = DVConstraint.createExplicitListConstraint(provinceArr);
            CellRangeAddressList provRangeAddressList = new CellRangeAddressList(1, 1000, 4, 4);
            DataValidation provinceDataValidation = new HSSFDataValidation(provRangeAddressList, provConstraint);
            provinceDataValidation.createErrorBox("error", "请选择正确的省份");
            sheet.addValidationData(provinceDataValidation);

            // 市以规则,此处仅作一个示例
            // "INDIRECT($A$" + 2 + ")" 表示规则数据会从名称管理器中获取key与单元格 A2 值相同的数据,如果A2是浙江省,那么此处就是
            // 浙江省下的区域信息。
            DVConstraint formula = DVConstraint.createFormulaListConstraint("INDIRECT($E:$E)");
             CellRangeAddressList rangeAddressList = new CellRangeAddressList(1, 1000, 5, 5);
             DataValidation cacse = new HSSFDataValidation(rangeAddressList, formula);
             cacse.createErrorBox("error", "请选择正确的市");
             sheet.addValidationData(cacse);

             // 区规则
             formula = DVConstraint.createFormulaListConstraint("INDIRECT($F:$F)");
             rangeAddressList = new CellRangeAddressList(1, 1000, 6, 6);
             cacse = new HSSFDataValidation(rangeAddressList, formula);
             cacse.createErrorBox("error", "请选择正确的区");
             sheet.addValidationData(cacse);
             //设置格式
            CellStyle cellStyle = workbook.createCellStyle();      
       //设置边框:    
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框    
            //设置居中:   
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中    
            //设置字体:      
            Font font2 = workbook.createFont();    
            font2.setFontName("仿宋_GB2312");    
            font2.setFontHeightInPoints((short) 11);        
            cellStyle.setFont(font2);//选择需要用到的字体格式
            //设置标题行格式
            CellStyle cellStyleHeader = workbook.createCellStyle(); 
            //设置背景色
            cellStyleHeader.setFillForegroundColor(HSSFColor.LIME.index);
            cellStyleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            //设置边框:    
            cellStyleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
            cellStyleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
            cellStyleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
            cellStyleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框    
            //设置居中:   
            cellStyleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中    
            //设置字体:      
            Font font = workbook.createFont();    
            font.setFontName("黑体");    
            font.setFontHeightInPoints((short) 12);        
            cellStyleHeader.setFont(font);//选择需要用到的字体格式
            WritableCell cell;
            // 生成表头
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < fieldLabelArray.length; i++) {
                sheet.setDefaultColumnStyle(i, cellStyle);
                sheet.setColumnWidth(i, 4000);
                headerRow.createCell(i).setCellValue(fieldLabelArray[i]); 
                headerRow.getCell(i).setCellStyle(cellStyleHeader);
            }
            // 输出文件
            workbook.write(os);
            os.close();
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
   }

 

 添加名称管理方法

private String getRange(int offset, int rowId, int colCount) {
        char start =(char) ('A'+offset);
        if(colCount<=25){
            char end =(char) (start+colCount-1);
            return "$"+start+"$"+rowId+":$"+end+"$"+rowId;
        }else{
            char endPrefix ='A';
            char endSuffix ='A';
            if((colCount-25)/26==0 || colCount==51){//26-51之间,包括边界(仅两次字母表计算)
                if((colCount-25)%26==0){//边界值
                    endSuffix=(char)('A'+25);
                }else{
                    endSuffix=(char)('A'+(colCount-25)%26-1);
                }
            }else{//51以上
                if((colCount-25)%26==0){
                    endSuffix=(char)('A'+25);
                    endPrefix=(char)(endPrefix+(colCount-25)/26-1);
                }else{
                    endSuffix=(char)('A'+(colCount-25)%26-1);
                    endPrefix=(char)(endPrefix+(colCount-25)/26);
                }
            }
            return "$"+start+"$"+rowId+":$"+endPrefix+endSuffix+"$"+rowId;
        }
    }

有更好的希望能给给我邮箱发一封,大家共同学习,共同进步,有部分内容网上找的,如有冒犯,请谅解,本人,菜鸟。

导入有时间再整理。

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring Boot 集成 Hystrix

下一篇:Java网络编程