Exemplo n.º 1
0
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportData()
        {
            string sql        = "";
            long   tbReportId = 0;  //初始化tbReportId
            string strResult  = ""; //返回参数
            //获得参数
            string code = Request["code"].Trim();

            if (!string.IsNullOrEmpty(Request["tbReportId"].Trim()))
            {
                tbReportId = Convert.ToInt64(Request["tbReportId"].Trim());
            }
            string queryParams = Request["queryParams"]; //查询条件
            string url         = Request["url"];         //页面url

            url = System.Web.HttpUtility.UrlDecode(url);
            string bootUrl    = Request["bootUrl"];                                                                                    //网站根目录(含虚拟层级)
            string strHost    = bootUrl.Substring(0, bootUrl.IndexOf(Request.Url.Authority.ToLower()) + Request.Url.Authority.Length); //http头
            string exportWay  = Request["exportWay"];                                                                                  //导出方式
            string fileFormat = Request["fileFormat"];                                                                                 //文件格式

            EasyMan.Dtos.ErrorInfo err = new EasyMan.Dtos.ErrorInfo();
            err.IsError = false;

            #region 逻辑部分
            try
            {
                var report = _reportAppService.GetReportBase(code);
                var expCfg = _exportAppService.GetExportConfig("report");//得到当前功能的配置信息
                #region 默认值
                int    intWaitTime  = 60000;
                int    intMaxRowNum = 1800000;
                int    intDataSize  = 51200;
                int    intValidDay  = 15;
                string strPath      = "upfiles";
                if (expCfg.WaitTime == null || expCfg.WaitTime <= 0)
                {
                    expCfg.WaitTime = intWaitTime;
                }
                if (expCfg.MaxRowNum == null || expCfg.MaxRowNum <= 0)
                {
                    expCfg.MaxRowNum = intMaxRowNum;
                }
                if (expCfg.DataSize == null || expCfg.DataSize <= 0)
                {
                    expCfg.DataSize = intDataSize;
                }
                if (expCfg.ValidDay == null || expCfg.ValidDay <= 0)
                {
                    expCfg.ValidDay = intValidDay;
                }
                if (expCfg.Path == null || expCfg.Path.Trim() == "")
                {
                    expCfg.Path = (bootUrl.Replace(strHost, "") + strPath).Replace("\\", "/").Replace("//", "/");
                }
                else
                {
                    expCfg.Path = (bootUrl.Replace(strHost, "") + expCfg.Path).Replace("\\", "/").Replace("//", "/");
                }
                #endregion
                sql = _reportAppService.GetSqlForField(code, queryParams, tbReportId, ref err);

                if (err.IsError)
                {
                    throw new Exception(err.Message);
                }
                int intCountSize = IntDataSize(sql, (report == null || report.DbServerId == null ? null : report.DbServerId));//返回当前集合条数
                if (intCountSize <= 0)
                {
                    return(Content("暂无无数据导出!"));
                }
                #region 抽样数据
                double   WaitTime   = (double)expCfg.WaitTime;
                DateTime datEndDate = DateTime.Now.AddMilliseconds(WaitTime);//最大等待时长
                if (DateTime.Now > datEndDate && exportWay == "online")
                {
                    return(Content("online导出时,由于数据量过大,在统计数据时超出online最大等待时长,请转为offline导出。是否转为offline导出?"));
                }
                long intPumping = GetPumpingSize(report.DbServerId, sql, intCountSize, WaitTime, exportWay);//通过样品数据预估数据集大小,如果小于等于0,表示超时
                if (intPumping <= 0)
                {
                    return(Content("online导出时,由于数据量过大,在数据抽样时超出online最大等待时长,请转为offline导出。是否转为offline导出?"));
                }
                #endregion

                //判断是否为offline
                if ((intCountSize > expCfg.MaxRowNum || intPumping > expCfg.DataSize) && exportWay == "online")
                {
                    return(Content("online导出最大支持" + expCfg.MaxRowNum + "条数据及" + expCfg.DataSize + "KB字节,是否转为offline导出?"));
                }

                object objTopFields = "";
                if (tbReportId != 0)
                {
                    var topFieldArr = _tbReportAppService.GetFildTopList(tbReportId); //获取多表头字段集合
                    var fieldArr    = _tbReportAppService.GetFildList(tbReportId);    //获取字段集合

                    if (topFieldArr != null && topFieldArr.Count > 0)
                    {
                        objTopFields = GetTopFieldForExcel(topFieldArr.ToArray(), fieldArr.ToArray()); //多表头信息
                    }
                }
                //根据url得到module
                var             module = _moduleAppService.GetModuleByUrl(url);
                string          strExt = GetExtend(fileFormat.ToLower());
                ExportDataModel exp    = new ExportDataModel
                {
                    ReportCode   = code,
                    DisplayName  = (module.Name == null || module.Name.Trim() == "" ? "" : module.Name + "_") + DateTime.Now.Ticks,
                    ExportWay    = exportWay,
                    FromUrl      = url,
                    FileFormat   = strExt,
                    FilePath     = expCfg.Path,
                    ValidDay     = expCfg.ValidDay,
                    TopFields    = objTopFields,//多表头信息
                    ColumnHeader = "",
                    Sql          = sql,
                    DbServerId   = report == null || report.DbServerId == null ? null : report.DbServerId,
                    FileName     = (module == null ? "无名称" : module.Name) + "_" + DateTime.Now.Ticks,
                    Status       = "生成中",
                    ObjParam     = "",
                    IsClose      = false
                };
                if (module.Id > 0)
                {
                    exp.ModuleId = module.Id;
                }

                //针对两种形式的导出处理,待完善
                switch (exportWay)
                {
                case "offline":
                    strResult = _exportAppService.OfflineExportData(exp, intCountSize);
                    break;

                case "online":
                    strResult = strHost + _exportAppService.OnlineExportData(exp);
                    break;

                default:
                    strResult = strHost + _exportAppService.OnlineExportData(exp);
                    break;
                }
            }
            catch (Exception ex)
            {
                err.IsError = false;
                string strHtml = "<script src=\"../Scripts/jquery-2.2.4.min.js\"></script>";
                strHtml += "<script src=\"../Common/rootUrl.js\"></script>";
                strHtml += "<script src=\"../Common/Scripts/errorPage/error.js\"></script>";
                strHtml += "<script>$(function () {SendErrorInfo('导出提示','导出时,程序异常。代码:" + ex.Message + ",请联系管理员')})</script>";
                return(Content(strHtml));
            }
            #endregion
            return(Content(strResult));
        }