コード例 #1
0
        /// <summary>
        /// 数据抽样统计数量
        /// </summary>
        /// <param name="lngDbid">数据库ID</param>
        /// <param name="strSql">数据SQL</param>
        /// <param name="intCountNum">总计数量</param>
        /// <param name="intPumping">抽样个数</param>
        /// <returns></returns>
        private long GetPumpingSize(long?lngDbid, string strSql, int intCountNum, double WaitTime, string exportWay, int intPumping = 10)
        {
            if (intCountNum < intPumping)
            {
                intPumping = intCountNum;
            }
            int intMax = (intCountNum - 1);

            if (intMax <= 0)
            {
                intMax = intCountNum;
            }
            int[]  intRandom = Common.Fun.GetRandomUnrepeatArray(1, intMax, intPumping);//样品数组(因为数组是从0开始的,所以最大值要减1)
            string strTemp   = "";

            foreach (int item in intRandom)
            {
                DateTime datEndDate = DateTime.Now.AddMilliseconds(WaitTime);//最大等待时长

                EasyMan.Dtos.ErrorInfo err = new EasyMan.Dtos.ErrorInfo();
                DataTable dtTempDb         = new DataTable();
                if (lngDbid == null)
                {
                    string sqlOne = _reportAppService.SqlForPage(DbHelper.GetCurConnection().DbType.ToString(), strSql, item, 1, ref err);
                    dtTempDb = DbHelper.ExecuteGetTable(sqlOne);
                }
                else
                {
                    dtTempDb = _dbServerAppService.ExecuteGetTable(lngDbid.Value, strSql, item, 1);
                }

                if (DateTime.Now > datEndDate && exportWay == "online")
                {
                    return(-1);
                }
                if (dtTempDb.Rows.Count > 0)
                {
                    foreach (DataColumn comItem in dtTempDb.Columns)
                    {
                        strTemp += dtTempDb.Rows[0][comItem.ColumnName];
                    }
                }
            }
            if (strTemp.Length <= 0)
            {
                return(0);
            }
            int  intStrLength = Common.Fun.GetStringLength(strTemp);
            long lngRowAVG    = intStrLength / intPumping * intCountNum;//每行的平均大小,乘以总行数;得到大概总大小

            return(lngRowAVG);
        }
コード例 #2
0
        /// <summary>
        /// 离线导出
        /// </summary>
        /// <param name="exp">数据集</param>
        /// <param name="intCountNum">数据总量</param>
        public string OfflineExportData(ExportDataModel exp, int intCountNum)
        {
            EasyMan.Dtos.ErrorInfo err = new EasyMan.Dtos.ErrorInfo();
            err.IsError = false;
            var config        = GetExportConfig("report");
            var intTheadCount = 1000;//线程最大值

            if (ThreadList.Count >= intTheadCount)
            {
                return("导出时,由于当前运行的线程数已达到了最大值,请稍后再尝试离线下载,或请联系管理给于帮助!");
            }

            int intRowNum = (int)config.MaxRowNum;                      //得到配置的每个文档最大行数

            dtmEndTime  = DateTime.Now.AddSeconds((int)config.MaxTime); //得到配置的最大导出时长(秒)
            intPagIndex = intCountNum / intRowNum;                      //计算总页数
            string strFileName = exp.FileName;

            //lngDbServerId = (long)exp.DbServerId;
            strMapPathAll = "";
            strPathAll    = "";
            exp.UserId    = GetCurrentUserAsync().Result.Id;

            if (intCountNum % intRowNum != 0)
            {
                intPagIndex++;
            }
            #region 转换表头
            if (exp.FileFormat.ToLower() == ".xlsx")
            {
                exp.ColumnHeader = GetHeadStr(exp.TopFields);//将表头对像转成字符串
            }
            else
            {
                exp.ColumnHeader = "";
            }
            string strFilePath = exp.FilePath;//保存原始数据
            #endregion

            object obj = new object();
            if (intPagIndex > 1)                           //如果大于1页,就启用分文件再压缩的方式离线导出文件
            {
                string strTempFileFormat = exp.FileFormat; //暂存文件格式
                exp.FileFormat = ".zip";                   //压缩文件格式
                SavaDBSql(null, null, exp);                //添加初始值
                exp.FileFormat = strTempFileFormat;        //还原文件格式
                TempThreadList = new List <Thread>();      //记录线程信息
                for (int i = 1; i <= intPagIndex; i++)
                {
                    string strTempFileName = strFileName + "_" + i + exp.FileFormat;
                    exp.FileName = strTempFileName;

                    DataTable dt = new DataTable();
                    if (exp.DbServerId == null)
                    {
                        string sqlOne = _reportAppService.SqlForPage(DbHelper.GetCurConnection().DbType.ToString(), exp.Sql, i, intRowNum, ref err);
                        dt = DbHelper.ExecuteGetTable(sqlOne);
                    }
                    else
                    {
                        dt = _dbServerAppService.ExecuteGetTable((int)exp.DbServerId, exp.Sql, i, intRowNum);     //要导出的数据集
                    }
                    dt.Columns.Remove("N");                                                                       //删除分页引起的多一列数据
                    exp.ObjParam = dt;
                    string strPath    = strFilePath;                                                              //虚拟地址
                    string strMapPath = "";                                                                       //物理地址
                    Easyman.Common.Fun.GetPath(ref strTempFileName, exp.FileFormat, ref strMapPath, ref strPath); //重新整理保存路径
                    exp.FilePath   = strMapPath;
                    strPathAll    += (strPathAll == "" ? "" : "|") + strPath;
                    strMapPathAll += (strMapPathAll == "" ? "" : "|") + strMapPath;

                    Thread thread = new Thread(new ParameterizedThreadStart(OutExportFile));//启用多线程处理数据
                    thread.IsBackground = true;
                    thread.Name         = strTempFileName.Replace(exp.FileFormat, "");
                    obj = new object();
                    obj = exp;
                    thread.Start(obj);
                    ThreadList.Add(thread);     //添加到总记录里面
                    TempThreadList.Add(thread); //添加到临时记录
                }
                ExportDataModel ExportDataM = new ExportDataModel();
                ExportDataM = exp;

                t           = new System.Timers.Timer(10000); //实例化Timer类,设置间隔时间为10000毫秒;
                t.Elapsed  += new System.Timers.ElapsedEventHandler((s, e) => OutTime(s, e, strFilePath, strFileName, (ExportDataModel)obj, TempThreadList));
                t.AutoReset = true;                           //设置是执行一次(false)还是一直执行(true);
                t.Enabled   = true;                           //是否执行System.Timers.Timer.Elapsed事件;
                t.Start();
            }
            else
            {
                SavaDBSql(null, null, exp);                                                                  //添加初始值
                //非压缩导出
                TempThreadList = new List <Thread>();                                                        //记录线程信息
                string strPath = exp.FilePath;
                Easyman.Common.Fun.GetPath(ref strFileName, exp.FileFormat, ref strMapPathAll, ref strPath); //重新整理保存路径
                exp.FileName = strFileName;
                exp.FilePath = strMapPathAll;

                DataTable dt = new DataTable();
                if (exp.DbServerId == null)
                {
                    dt = DbHelper.ExecuteGetTable(exp.Sql);
                }
                else
                {
                    dt = _dbServerAppService.ExecuteGetTable((int)exp.DbServerId, exp.Sql); //要导出的数据集
                }

                //exp.ObjParam = _dbServerAppService.ExecuteGetTable((int)exp.DbServerId, exp.Sql); //要导出的数据集
                exp.ObjParam = dt;
                Thread thread = new Thread(new ParameterizedThreadStart(OutExportFile));//启用多线程处理数据
                thread.IsBackground = true;
                thread.Name         = strFileName.Replace(exp.FileFormat, "");
                obj = new object();
                obj = exp;
                thread.Start(obj);
                ThreadList.Add(thread);     //添加到总记录里面
                TempThreadList.Add(thread); //添加到临时记录

                ExportDataModel ExportDataM = new ExportDataModel();
                ExportDataM = exp;

                t           = new System.Timers.Timer(10000); //实例化Timer类,设置间隔时间为10000毫秒;
                t.Elapsed  += new System.Timers.ElapsedEventHandler((s, e) => OutTime(s, e, strPath, strFileName, ExportDataM, TempThreadList));
                t.AutoReset = true;                           //设置是执行一次(false)还是一直执行(true);
                t.Enabled   = true;                           //是否执行System.Timers.Timer.Elapsed事件;
                t.Start();
            }
            return("导出命令已发出");
        }