Example #1
0
        /// <summary>
        /// 用于Web导出
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <param name="strFileName">文件名</param>
        /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>
        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {
            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType     = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset         = "";
            curContext.Response.AppendHeader("Content-Disposition",
                                             "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            if (dtSource.Rows.Count > 50000)
            {
                DeleteCsv();
                string          downPath = @"Uploads/" + strFileName + ".csv";
                CsvStreamWriter csw      = new CsvStreamWriter(curContext.Server.MapPath(downPath), System.Text.Encoding.Default);
                csw.AddData(dtSource, 1);
                csw.Save();

                curContext.Response.Redirect("/" + downPath, true);
            }
            else
            {
                curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
                curContext.Response.End();
            }
        }
Example #2
0
        /// <summary>   
        /// 用于Web导出   
        /// </summary>   
        /// <param name="dtSource">源DataTable</param>   
        /// <param name="strHeaderText">表头文本</param>   
        /// <param name="strFileName">文件名</param>   
        /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>   
        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {
            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            if (dtSource.Rows.Count > 50000)
            {
                DeleteCsv();
                string downPath = @"Uploads/" + strFileName + ".csv";
                CsvStreamWriter csw = new CsvStreamWriter(curContext.Server.MapPath(downPath),System.Text.Encoding.Default);
                csw.AddData(dtSource, 1);
                csw.Save();

                curContext.Response.Redirect("/" + downPath, true);
            }
            else
            {
                curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
                curContext.Response.End();
            }
        }