百萬數據多線程分頁查詢 EasyExcel導出Excel

1.組裝數據:CountDownLatch多線程分頁查詢組裝數據

@Override
    public Map<String, Object> exportBmInformationMainModelList(Map<String, Object> queryParams) {
         List<List<Object>> data = new ArrayList<List<Object>>(); // 需要導出的數據
         
        BmInformationMainQueryCondition condition = new BmInformationMainQueryCondition();
        if (null == queryParams.get("businessOrderTypeNo")) {
            return new HashMap<String, Object>();
        }
        if(queryParams.get("infoMainNo")==null ||queryParams.get("infoMainNo").toString().equals("null")){
            queryParams.remove("infoMainNo");
        }
        BeanToMapUtils.mapToBean(queryParams, condition);
        
        BmBusinessDetailQueryCondition detailCondition=new BmBusinessDetailQueryCondition();
        detailCondition.setOrgId(condition.getOrgId());
        detailCondition.setOrderTypeNo(condition.getBusinessOrderTypeNo());
        List<BmBusinessDetailQueryItem> businessDetailQueryItems=bmBusinessDetailManager.getBusinessDetail(detailCondition);

        ArrayList title=new ArrayList<List<String>>();
        List<String> headCell1 = new ArrayList<String>();
        headCell1.add("單據編號");
        title.add(headCell1); //表頭
        List<String> headCell2 = new ArrayList<String>();
        headCell2.add("經營組織");
        title.add(headCell2);
        List<String> headCell3 = new ArrayList<String>();
        headCell3.add("客戶名稱");
        title.add(headCell3); //表頭
        List<String> headCell4 = new ArrayList<String>();
        headCell4.add("供應商名稱");
        title.add(headCell4); //表頭
        List<String> headCell5 = new ArrayList<String>();
        headCell5.add("品牌");
        title.add(headCell5); //表頭
        List<String> headCell6 = new ArrayList<String>();
        headCell6.add("運單號");
        title.add(headCell6); //表頭
        List<String> headCell7 = new ArrayList<String>();
        headCell7.add("VIN碼");
        title.add(headCell7); //表頭
        List<String> headCell8 = new ArrayList<String>();
        headCell8.add("起始地");
        title.add(headCell8); //表頭
        List<String> headCell9 = new ArrayList<String>();
        headCell9.add("目的地");
        title.add(headCell9); //表頭
        List<String> headCell10 = new ArrayList<String>();
        headCell10.add("業務類型");
        title.add(headCell10); //表頭
        List<String> headCell11 = new ArrayList<String>();
        headCell11.add("狀態");
        title.add(headCell11); //表頭
        List<String> headCell12 = new ArrayList<String>();
        headCell12.add("是否計費");
        title.add(headCell12); //表頭
        List<String> headCell13 = new ArrayList<String>();
        headCell13.add("運抵時間");
        title.add(headCell13); //表頭
        List<String> headCell14 = new ArrayList<String>();
        headCell14.add("業務日期");
        title.add(headCell14); //表頭

        ArrayList<String> otherAttributeList=new ArrayList<String>();
        if(businessDetailQueryItems.size()>0){
            for(BmBusinessDetailQueryItem item:businessDetailQueryItems){
                if(!title.contains(item.getAttNameCn())){
                    List<String> headCell = new ArrayList<String>();
                    headCell.add(item.getAttNameCn());
                    title.add(headCell); // 表頭
                    otherAttributeList.add(item.getAtt());
                }
            }
        }
        List<String> headCell15 = new ArrayList<String>();
        headCell15.add("來源系統業務單號");
        title.add(headCell15); //表頭
        
        List<String> headCell16 = new ArrayList<String>();
        headCell16.add("客戶訂單號");
        title.add(headCell16); //表頭
        
        List<String> headCell17 = new ArrayList<String>();
        headCell17.add("會計主體");
        title.add(headCell17); //表頭
        
        List<String> headCell18 = new ArrayList<String>();
        headCell18.add("創建時間");
        title.add(headCell18); //表頭
        
        BigInteger rowNum=new BigInteger("0");
        int times=0;
        List<Map<String, Object>> bmInformationMainRumNum=calculateCommonService.nativeSqlFind("QueryBmInformationMainCount",null,queryParams);
        if(bmInformationMainRumNum.size()>0){
            rowNum=(BigInteger) bmInformationMainRumNum.get(0).get("rowNum");
        }
        if(rowNum.longValue()>0){
            times=(int) Math.ceil((double)rowNum.doubleValue()/(double)10000);
            final CountDownLatch barrier=new CountDownLatch(times);
            for(int i=0;i<times;i++){
                Map map=new HashMap();
                map.putAll(queryParams);
                map.put("startNum", i*10000);
                new QueryBmInformationMainThread(barrier,otherAttributeList, data, map).start();
            }
            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("headList", title);
        result.put("dataList", data);
        return result;
    }

2.分頁查詢線程

    

class QueryBmInformationMainThread extends Thread{
        private CountDownLatch  countDownLatch ;
        private ArrayList<String> otherAttributeList;
        private List<List<Object>> data;
        private Map<String, Object> queryParams;
        
        public QueryBmInformationMainThread(CountDownLatch countDownLatch, ArrayList<String> otherAttributeList,List<List<Object>> data,Map<String, Object> queryParams){
            this.otherAttributeList=otherAttributeList;
            this.data=data;
            this.queryParams=queryParams;
            this.countDownLatch=countDownLatch;
        }
        
        @Override
        public void run() {
            List<Map<String, Object>> thisTimesList=calculateCommonService.nativeSqlFind("QueryBmInformationMainList",null,queryParams);
            List<List<Object>> thisResult=new ArrayList<List<Object>>();
            if (thisTimesList.size() > 0) {
                for (Map<String,Object> item : thisTimesList) {
                     ArrayList<Object> info=new ArrayList<Object>();
                     info.add(item.get("INFO_MAIN_NO"));
                     info.add(item.get("COMPANY_NAME"));
                     info.add(item.get("CUST_NAME"));
                     info.add(item.get("SUP_NAME"));
                     info.add(item.get("ATTRIBUTE_018"));
                     info.add(item.get("ATTRIBUTE_002"));
                     info.add(item.get("ATTRIBUTE_003"));
                     info.add(item.get("ATTRIBUTE_010"));
                     info.add(item.get("ATTRIBUTE_012"));
                     if(item.get("BUSINESS_TYPE")!=null && item.get("BUSINESS_TYPE").equals("TRANSPORT")){
                         info.add("運輸");
                     }
                     else if(item.get("BUSINESS_TYPE")!=null && item.get("BUSINESS_TYPE").equals("STORAGE")){
                         info.add("倉儲");
                     }
                     if(item.get("SUBMIT_STATUS")!=null && item.get("SUBMIT_STATUS").equals("submit")){
                         info.add("審覈");
                     }
                     else if(item.get("SUBMIT_STATUS")!=null && item.get("SUBMIT_STATUS").equals("cancel")){
                         info.add("未審覈");
                     }
                     if(item.get("IS_COMPLETE_CALC")!=null && item.get("IS_COMPLETE_CALC").equals("Y")){
                         info.add("是");
                     }
                     else if(item.get("IS_COMPLETE_CALC")!=null && item.get("IS_COMPLETE_CALC").equals("N")){
                         info.add("否");
                     }
                     info.add(item.get("ATTRIBUTE_006"));
                     //業務日期格式化年月日
                     SimpleDateFormat ymd =new SimpleDateFormat("yyyy-MM-dd" );
                     info.add(ymd.format(item.get("BUSINESS_TIME")));
                     for(String attr:otherAttributeList){
                         info.add(item.get(humpToUnderline(attr)));
                     }
                     
                     info.add(item.get("BUSINESS_SOURCE_NO"));
                     info.add(item.get("CUST_ORDER_NO"));
                     info.add(item.get("LEGAL_ENTITY_NAME"));
                     //業務日期格式化年月日
                     SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
                     info.add(sdf.format(item.get("CREATE_TIME")));
                     thisResult.add(info);
                }
            }
            synchronized(data){
                data.addAll(thisResult);
            }
            countDownLatch.countDown();
        }
        
    }

3.EasyExcel導出

@ResponseBody
    @RequestMapping(value = "/exportBusinessOrderInfoExcel")
    public void exportBusinessOrderInfoExcel(@RequestParam Map<String, Object> params) throws Exception{
        if(null == params.get("businessOrderTypeNo")||"".equals(params.get("businessOrderTypeNo"))){
            return;
        }
        params.put("orgId", UserContextHolder.getCurrentUser().getOrgId());
        
        Map<String, Object> result = bmBusinessOrderInfoService.exportBmInformationMainModelList(params);
        if (EmptyUtils.isEmpty(result)) {
            return;
        }
        List<List<String>> headerList = (List<List<String>>) result.get("headList"); // 需要導出的數據
        List<List<Object>> dataList = (List<List<Object>>) result.get("dataList"); // 需要導出的數據
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = df.format(new Date());
        String excelName = "業務單據" + date + ".xlsx";
        String sheetName = "業務單據";
        excelName = URLEncoder.encode(excelName, "UTF-8");
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        ServletOutputStream out = response.getOutputStream();
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + excelName);
        response.setContentType("application/vnd.ms-excel");
        
        
        ExcelWriter writer = EasyExcelFactory.getWriter(out);
        // 寫在sheet1
        Sheet sheet1 = new Sheet(1, 3);
        sheet1.setSheetName(sheetName);
        sheet1.setHead(headerList);
        // or 設置自適應寬度
        sheet1.setAutoWidth(Boolean.TRUE);
        writer.write1(dataList, sheet1);
        writer.finish();
        out.flush();
        out.close();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章