C#导出数据量大于100万csv

Posted 哈佛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#导出数据量大于100万csv相关的知识,希望对你有一定的参考价值。

    /// <summary>
    /// Export the data from datatable to CSV file
    /// </summary>
    /// <param name="grid"></param>
    public void ExportDataGridToCSV(System.Data.DataTable dt)
    {
        string strFile = "";
        string path = "";

        //File info initialization
        strFile = "test";
        strFile = strFile + DateTime.Now.ToString("yyyyMMddhhmmss");
        strFile = strFile + ".csv";
        path = Server.MapPath(strFile);



        System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
        //Tabel header
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sw.Write(dt.Columns[i].ColumnName);
            sw.Write("\t");
        }
        sw.WriteLine("");
        //Table body
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                sw.Write(DelQuota(dt.Rows[i][j].ToString()));
                sw.Write("\t");
            }
            sw.WriteLine("");
        }
        sw.Flush();
        sw.Close();

        DownLoadFile(path);
    }

    private bool DownLoadFile(string _FileName)
    {
        try
        {
            System.IO.FileStream fs = System.IO.File.OpenRead(_FileName);
            byte[] FileData = new byte[fs.Length];
            fs.Read(FileData, 0, (int)fs.Length);
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            string FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(_FileName));
            Response.AddHeader("Content-Disposition", "inline;filename=" + System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34));
            Response.AddHeader("Content-Length", fs.Length.ToString());
            Response.BinaryWrite(FileData);
            fs.Close();
            System.IO.File.Delete(_FileName);
            Response.Flush();
            Response.End();
            return true;
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
            return false;
        }
    }



    /// <summary>
    /// Delete special symbol
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string DelQuota(string str)
    {
        string result = str;
        string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "", ",", ".", "/", ":", "/,", "<", ">", "?" };
        for (int i = 0; i < strQuota.Length; i++)
        {
            if (result.IndexOf(strQuota[i]) > -1)
                result = result.Replace(strQuota[i], "");
        }
        return result;
    }

还是.csv靠谱,速度佳。.xls就是个坑货,除非有特殊要求。

//以字符流的形式下载文件
          //以字符流的形式下载文件
            string filePath = HttpContext.Current.Server.MapPath("~/Upload/temp.xls");//路径
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            //删除文件,看个人喜好
            File.Delete(filePath);
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

 

以上是关于C#导出数据量大于100万csv的主要内容,如果未能解决你的问题,请参考以下文章

plsql导出csv数据是空白

Php导出百万数据的优化

java导出大批量(百万以上)数据的excel文件

java导出大量数据,出现错误?

PHP导出大量数据到csv表

如何高效的导出 百万级别的数据量 到 Excel?