• 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(" 时间" + "	" + "名称" + "	" + "名称" + "	" + "名称" + "	" + "单号" );
                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()) + "	" + DelQuota(dt.Rows[i][1].ToString()) + "	" + DelQuota(dt.Rows[i][2].ToString()) + "	" + DelQuota(dt.Rows[i][3].ToString()) + "	" + 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;
            }
  • 相关阅读:
    在 ubuntu20 上安装 docker
    在 ubuntu20 上替换原有的源,解决下载软件慢的问题
    thymeleaf 模板语法
    对 spring 中默认的 DataSource 创建进行覆盖
    Spring Security OAuth2 笔记(一)
    对 ThreadLocal 的了解(一)
    解决 docker.io 上拉取 images Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
    Spring Security Oauth2 使用 token 访问资源服务器出现异常:Invalid token does not contain resource id (oauth2)
    windows 查看被占用的端口信息
    不写注释的程序员-Models
  • 原文地址:https://www.cnblogs.com/luckys/p/13431403.html
Copyright © 2020-2023  润新知