c#大数据导出表格溢出问题的解决方案

//数据量过多查询数据库中的数据执行缓慢,造成在查询语句时就溢出
//将数据拆分,利用分页的形式进行查询
//查询条数
            string sqlcount = "exec  [Pg_Paging]  '表名','ID','0',null,'ID','ID', '" + wherestr.Replace("'", "''") + "' ,null,1";
            DataTable count = SqlHlper.ExecuteDt(sqlcount);
            //总条数
            int num = int.Parse(count.Rows[0]["TotalRecord"].ToString());
            //因数据条数太多,以分页的形式进行查询,十万一次查询,多少个十万,分页数
            int row = num / 100000;
            if ((num % 100000) > 0)
            {
                row = row + 1;
            }
            //导出是数据保存的位置
            string path = "";
            path = Server.MapPath("~/Download/导出.csv");
            System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
            //表头,固定的无需进行循环
            sw.Write(" 时间" + "\t" + "名称" + "\t" + "名称" + "\t" + "名称" + "\t" + "单号" );
            sw.WriteLine("");
//给每一行赋值
            //十万十万的数据进行查询
            for (int p = 1; p <= row; p++)
            {
                //查询数据
                string sql = "exec [dbo].[Pg_Paging] '表名','ID','" + p + "',100000,'UpdateTime desc','" + 查询的字段,*为所有字段+ "', '" + wherestr.Replace("'", "''") + "' ,'',0";
                DataTable dtBody = SqlHlper.ExecuteDt(sql);
                for (int i = 0; i < dtBody.Rows.Count; i++)
                {
 //导出的数据列固定,无需循环,减少循环的次数提高效率
                sw.Write(DelQuota(dt.Rows[i][0].ToString()) + "\t" + DelQuota(dt.Rows[i][1].ToString()) + "\t" + DelQuota(dt.Rows[i][2].ToString()) + "\t" + DelQuota(dt.Rows[i][3].ToString()) + "\t" + DelQuota(dt.Rows[i][4].ToString()) );
                sw.WriteLine("");
                }
            }
            sw.Flush();
            sw.Close();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=下载的表名.csv");
            Response.WriteFile(path);
           // System.IO.File.Delete(path);
            Response.Flush();
            Response.End();




//导出的数据中存在字符会出现换行的情况,处理特殊字符
        public string DelQuota(string str)
        {
            string result = "\"" + str + "\"";
            return result;
        }

 

posted @ 2020-08-04 09:37  天空之云  阅读(586)  评论(0编辑  收藏  举报