1. 程式人生 > >根據查詢條件匯出資料

根據查詢條件匯出資料

/**
 * 根據查詢條件查詢匯出資料
 */
public void exportTable(){
    Page pageInfo = this.getPageInfo();
    String taskBookId = request.getParameter("taskBookId");
    String type = request.getParameter("type");
    pageInfo = researchManageService.listData(pageInfo, searchVO,taskBookId);
    List<Map> list = pageInfo.getResult();
    this.exportTable(list,"專案課題匯出","課題列表",taskBookId);
}

/**
 * 匯出
 * @param cusMapList
 * @param fileName
 * @param dataType
 */
public void exportTable(List<Map> cusMapList, String fileName, String dataType,String taskBookId) {
    try {
        // 定義輸出流
        HttpServletResponse response = ServletActionContext.getResponse();
        OutputStream os = response.getOutputStream();
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        fileName = fileName + date;
        fileName = new String(fileName.getBytes(), "iso-8859-1");
        response.setCharacterEncoding("UTF-8");
        response.reset();
        response.setHeader("Content-disposition", "attachment;filename = " + new String(fileName + ".xls"));
        response.setContentType("application/msexcel");
        // 設定單元格字型
        WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
        WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
        // 用於標題居中
        WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
        wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條
        wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊
        wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對齊
        wcf_center.setWrap(false); // 文字是否換行
        // 用於正文居中
        WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
        wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條
        wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊
        wcf_left.setAlignment(Alignment.CENTRE); // 文字水平對齊
        wcf_left.setWrap(true); // 文字是否換行
        // 建立工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        WritableSheet sheet = workbook.createSheet(dataType, 0);
        List listK = new ArrayList();
        List listV = new ArrayList();
        Map<String, Object> map = new HashMap();
        List list = new ArrayList();

        listK.add("num"); listV.add("序號");
        listK.add("WBS"); listV.add("課題編碼");
        listK.add("name"); listV.add("課題名稱");
        listK.add("mnemonicCode"); listV.add("助記碼");
        if(!StringUtil.validateString(taskBookId)){
            listK.add("projectCode"); listV.add("專案編號");
            listK.add("productName"); listV.add("專案名稱");
        }
        listK.add("departmentName"); listV.add("責任部門");
        listK.add("createUserName"); listV.add("建立人");
        listK.add("createDate"); listV.add("建立時間");
        listK.add("modifyUserName"); listV.add("更新人");
        listK.add("modifyDate"); listV.add("更新時間");
        listK.add("status"); listV.add("狀態");
        listK.add("indexNo"); listV.add("排序號");


        //格式化標題欄
        int r = 0;//單元格橫座標增量
        int e = 1;//標題深度
        int l;//列寬

        for (int i = 0; i < listV.size(); i++) {
            Object obj = listV.get(i);
            if (obj instanceof Map) {
                Map mapObj = (Map) obj;
                List resultList = (List) mapObj.get("result");
                int size = resultList.size();
                sheet.mergeCells(i + r, 0, i + r + size - 1, 0);
                r = r + size - 1;
            } else if(e==2) {
                sheet.mergeCells(i + r, 0, i + r, 1);
            }
        }

        //設定列標題和根據標題初始化列寬
        String t;
        r = 0;
        for (int i = 0; i < listV.size(); i++) {
            Object obj = listV.get(i);
            if (obj instanceof Map) {
                Map mapObj = (Map) obj;
                List resultList = (List) mapObj.get("result");
                int size = resultList.size();
                if(size>0){
                    for (int j = 0; j < resultList.size(); j++) {
                        t = resultList.get(j).toString();
                        sheet.addCell(new Label(i + r + j, 1, t, wcf_center));
                        sheet.addCell(new Label(i + r, 0, mapObj.get("name").toString(), wcf_center));
                        l = t.length();
                        sheet.setColumnView(i + r + j, l * 9 / 4);
                    }
                    r = r + size - 1;
                }else{
                    sheet.addCell(new Label(i + r, 0, mapObj.get("name").toString(), wcf_center));
                    l = (mapObj.get("name").toString()).length();
                    sheet.setColumnView(i + r, l * 9 / 4);
                }
            } else {
                t = listV.get(i).toString();
                sheet.addCell(new Label(i + r, 0, t, wcf_center));
                l = (listV.get(i).toString()).length();
                sheet.setColumnView(i + r, l * 9 / 4);
            }
        }
        //繪製內容
        String k="";
        String s;
        for (int i = 0; i < cusMapList.size(); i++) {
            sheet.addCell(new Label(0, i + e, i + 1 + "", wcf_center));//序號
            Map temp = cusMapList.get(i);
            for (int j = 1; j < listK.size(); j++) {
                if (temp.get(listK.get(j)) != null) {
                    l = sheet.getColumnWidth(j);
                    k = temp.get(listK.get(j)).toString();
                    if(k.equals("underway")){
                        k = "進行中";
                    }else if(k.equals("close")){
                        k = "業務關閉";
                    }else if(k.equals("delete")){
                        k = "刪除";
                    }
                    if (k.length() > l / (9 / 3) && k.length() <= 20) {//再次根據內容設定列寬(只增不減)
                        sheet.setColumnView(j, k.length() * 9 / 3);
                    } else if (k.length() > 20) {
                        sheet.setColumnView(j, 40);
                    }
                    s = (listK.get(j).toString()).toLowerCase();
                    if (s.indexOf("date") != -1 || s.indexOf("time") != -1) {
                        if (k.length() > 10) {
                            k = k.substring(0, 10);
                        }
                        if (listV.get(j) instanceof Map) {
                            Map tempMap = (Map)listV.get(j);
                            if(tempMap.get("name")!=null){
                                t = tempMap.get("name").toString();
                            }else{
                                t="";
                            }
                        }else{
                            t = listV.get(j).toString();
                        }
                        if ((t.length() * 9 / 3) > (k.length() * 3 / 2)) {
                            sheet.setColumnView(j, t.length() * 9 / 3);
                        } else {
                            sheet.setColumnView(j, k.length() * 3 / 2);
                        }
                    }
                    sheet.addCell(new Label(j, i + e, k, wcf_left));
                } else {
                    sheet.addCell(new Label(j, i + e, "", wcf_left));
                }
            }
        }
        workbook.write();
        workbook.close();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}