根据查询条件导出Excel文件

一、GET方式请求

前端页面:页面要点击按钮导出函数;

    //导出excel
    function exportExcel() {
        window.location.href = "CenterChainTaskController/exportExcel?CENTERTASKCODE=" + $("#searchCENTERTASKCODE").val().trim() + "" +
            "&TASK_SOURCEID=" + $("#TASK_SOURCEID_SEARCH").val() + "&TASK_TYPEID=" + $("#TASK_TYPEID_SEARCH").val()+ "&TASK_CLASSIFYID=" + $("#TASK_CLASSIFYID_SEARCH").val()
            + "&OBJECT_NAME=" + $("#OBJECT_NAME_SEARCH").val()+ "&checkPoint=" + $("#checkPoint").val()
            + "&TASK_BEGINDATE=" + $("#TASK_BEGINDATE_SEARCH").val()+ "&TASK_ENDDATE=" + $("#TASK_ENDDATE_SEARCH").val()
            + "&STATUS=";
    }

后台代码:

 @RequestMapping(value = "exportExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, CenterChainTask centerChainTask){
        String json = "";
        try {
            List attributeList = new ArrayList();
            StringBuffer listSQL = new StringBuffer(QL.SQL.CenterChainTask.COMMON.TASKLISTEXCEL);
            //动态拼接sql 条件查询
            if(StringUtils.isNotBlank(centerChainTask.getCENTERTASKCODE())){
                listSQL.append(" and a.CENTERTASKCODE like ?");
                attributeList.add("%"+centerChainTask.getCENTERTASKCODE()+"%");
            }
            if (StringUtils.isNotBlank(centerChainTask.getTASK_SOURCEID())){
                listSQL.append(" and a.TASK_SOURCEID = ?");
                attributeList.add(centerChainTask.getTASK_SOURCEID());
            }
            if (StringUtils.isNotBlank(centerChainTask.getTASK_TYPEID())){
                listSQL.append(" and a.TASK_TYPEID = ?");
                attributeList.add(centerChainTask.getTASK_TYPEID());
            }
            if (StringUtils.isNotBlank(centerChainTask.getTASK_CLASSIFYID())){
                listSQL.append(" and a.TASK_CLASSIFYID = ?");
                attributeList.add(centerChainTask.getTASK_CLASSIFYID());
            }
            if (centerChainTask.getTASK_BEGINDATE()!=null && centerChainTask.getTASK_ENDDATE()!=null){
                listSQL.append(" and a.TASK_BEGINDATE = ?");
                attributeList.add(centerChainTask.getTASK_BEGINDATE());
            }
            if (centerChainTask.getTASK_ENDDATE()!=null){
                listSQL.append(" and a.TASK_ENDDATE = ?");
                attributeList.add(centerChainTask.getTASK_BEGINDATE());
            }
            if (StringUtils.isNotBlank(centerChainTask.getOBJECT_NAME())){
                listSQL.append(" and instr(a.OBJECT_NAME,?) > 0");
                attributeList.add(centerChainTask.getOBJECT_NAME());
            }
            if (StringUtils.isNotBlank(centerChainTask.getSTATUS())){
                listSQL.append(" and a.STATUS = ?");
                attributeList.add(centerChainTask.getSTATUS());
            }
            if (StringUtils.isBlank(centerChainTask.getSTATUS())){
                listSQL.append(" and a.STATUS in ('0','1','2','3','4','5','6')");
            }
            listSQL.append(" order by a.create_date desc ");
            List list = iCenterChainTaskServices.getListBySql(listSQL.toString(), attributeList,CenterChainTask.class);
            LinkedHashMap fieldMap = new LinkedHashMap();
            fieldMap.put("CENTERTASKCODE","任务单号");
            fieldMap.put("TASK_SOURCENAME","任务来源");
            fieldMap.put("TASK_TYPENAME","任务类型");
            fieldMap.put("TASK_CLASSIFYNAME","任务类别");
            fieldMap.put("OBJECT_NAME","交办对象");
            fieldMap.put("TASK_BEGINDATE","任务开始时间");
            fieldMap.put("TASK_ENDDATE","任务结束时间");
            fieldMap.put("STATUS","状态");
            ExcelUtils.listToExcel(list,fieldMap,"联动中心任务统计",list.size()+2,response);
        } catch (ExcelException e) {
            this.print(e.getMessage(),response);
            e.printStackTrace();
        }
    } 
  

sql语句中转换状态为中文,不然导出的文件状态值是数字:

public static final String TASKLISTEXCEL = "select GUID,CENTERTASKCODE,TASK_SOURCEID,TASK_SOURCENAME,TASK_TYPEID,TASK_TYPENAME,TASK_CLASSIFYID,TASK_CLASSIFYNAME,TASK_BEGINDATE," +
                        "TASK_ENDDATE,OBJECTTYPE,OBJECT_ID,OBJECT_NAME,TASK_CONTENT,ASSIGN_OPINION,ATTACHMENT,CASE STATUS WHEN '0' THEN '待派遣' WHEN '1' THEN '待签收' WHEN '2' THEN '处理中' WHEN '3' THEN '已处置'" +
                        "WHEN '4' THEN '已回复' WHEN '5' THEN '已办结' WHEN '6' THEN '重新派遣' ELSE '已作废' END AS STATUS, AFFIRM_USERID,AFFIRM_USERNAME,AFFIRM_DATE,CREATE_DATE,CREATE_USERID,CREATE_USERNAME,UPDATE_DATE,UPDATE_USERID,UPDATE_USERNAME from T_TASK_CENTER a where 1=1 ";

二、post方式请求

如果后台要用到当前用户的数据,要用post方式,get请求无法携带token之类的信息;

前段页面:

 //导出excel
    function exportExcel() {
        var $eleForm = $("
"); $eleForm.attr("action","/SubChainCenterTaskController/exportExcel"); $(document.body).append($eleForm); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); $eleForm.append(''); //提交表单,实现下载 $eleForm.submit(); }

后台:

@RequestMapping(value = "exportExcel", method = RequestMethod.POST)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, SubChainCenterTask subChainCenterTask){
        String json = "";
        //获取当前用户信息
        Claims b = JWTUtil.parseJWT(request.getParameter("token"), ConfigLoader.baseSecurity);
        String accountName = (String) b.get("accountName");
        Accounts account = iSubChainCenterTaskServicesImpl.getEntityBySql("select * from accounts where account_name = '" + accountName + "' and del_flag = 0", null, Accounts.class);
        User user = accountServicesImpl.getUserByAccount(account.getId());
        try {
            List attributeList = new ArrayList();
            StringBuffer listSQL = new StringBuffer(cn.o.comprehensivework.tasksubjectcenter.ql.QL.SQL.SubChainCenterTask.COMMON.LISTExcel);
            //动态拼接sql 条件查询
            if(StringUtils.isNotBlank(subChainCenterTask.getCENTERTASKCODE())){
                listSQL.append(" and a.CENTERTASKCODE like ?");
                attributeList.add("%"+subChainCenterTask.getCENTERTASKCODE()+"%");
            }
            if (StringUtils.isNotBlank(subChainCenterTask.getTASK_SOURCEID())){
                listSQL.append(" and a.TASK_SOURCEID = ?");
                attributeList.add(subChainCenterTask.getTASK_SOURCEID());
            }
            if (StringUtils.isNotBlank(subChainCenterTask.getTASK_TYPEID())){
                listSQL.append(" and a.TASK_TYPEID = ?");
                attributeList.add(subChainCenterTask.getTASK_TYPEID());
            }
            if (StringUtils.isNotBlank(subChainCenterTask.getTASK_CLASSIFYID())){
                listSQL.append(" and a.TASK_CLASSIFYID = ?");
                attributeList.add(subChainCenterTask.getTASK_CLASSIFYID());
            }
            if (subChainCenterTask.getTASK_BEGINDATE()!=null && subChainCenterTask.getTASK_ENDDATE()!=null){
                listSQL.append(" and a.TASK_BEGINDATE = ?");
                attributeList.add(subChainCenterTask.getTASK_BEGINDATE());
            }
            if (subChainCenterTask.getTASK_ENDDATE()!=null){
                listSQL.append(" and a.TASK_ENDDATE = ?");
                attributeList.add(subChainCenterTask.getTASK_BEGINDATE());
            }
            if (StringUtils.isNotBlank(subChainCenterTask.getOBJECT_NAME())){
                listSQL.append(" and instr(a.OBJECT_NAME,?) > 0");
                attributeList.add(subChainCenterTask.getOBJECT_NAME());
            }
            if (StringUtils.isNotBlank(subChainCenterTask.getSTATUS())){
                listSQL.append(" and a.STATUS = ?");
                attributeList.add(subChainCenterTask.getSTATUS());
            }
            if (StringUtils.isBlank(subChainCenterTask.getSTATUS())){
                listSQL.append(" and a.STATUS in ('0','1','2','3','4','5','6')");
            }
            listSQL.append(" and a.SUBCENTERCODE = ?");
            attributeList.add(account.getDataAreaCode());
            listSQL.append(" order by a.create_date desc ");
            List list = iSubChainCenterTaskServicesImpl.getListBySql(listSQL.toString(), attributeList,SubChainCenterTask.class);
            LinkedHashMap fieldMap = new LinkedHashMap();
            fieldMap.put("TASK_NATURE","任务分类");
            fieldMap.put("CENTERTASKCODE","任务单号");
            fieldMap.put("TASK_SOURCENAME","任务来源");
            fieldMap.put("TASK_TYPENAME","任务类型");
            fieldMap.put("TASK_CLASSIFYNAME","任务类别");
            fieldMap.put("OBJECT_NAME","交办对象");
            fieldMap.put("TASK_BEGINDATE","任务开始时间");
            fieldMap.put("TASK_ENDDATE","任务结束时间");
            fieldMap.put("STATUS","状态");
            ExcelUtils.listToExcel(list,fieldMap,"联动分中心任务统计",list.size()+2,response);
        }catch (Exception e){
            this.print(e.getMessage(),response);
            e.printStackTrace();
        }

    } 
  

注:ExcelUtils工具类可到本人的上传资源内下载

你可能感兴趣的:(Excel导出)