csharp: DataTable export to excel,word,csv etc_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > csharp: DataTable export to excel,word,csv etc

csharp: DataTable export to excel,word,csv etc

 2014/4/16 16:28:43  ?Geovin Du Dream Park?  博客园  我要评论(0)
  • 摘要:http://code.msdn.microsoft.com/office/Export-GridView-to-07c9f836https://exporter.codeplex.com////<summary>///塗聚文修改///</summary>///<paramname="dt"></param>///<paramname="Response"></param>///<
  • 标签:excel ARP

http://code.msdn.microsoft.com/office/Export-GridView-to-07c9f836

https://exporter.codeplex.com/

class="brush:csharp;gutter:true;"> /// <summary>
        /// 塗聚文修改
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="Response"></param>
        /// <param name="filename"></param>
        public static void Convertword(DataTable dt, HttpResponse Response, string filename)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".doc");
            Response.Charset = "utf-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="Response"></param>
        /// <param name="filename"></param>
        public static void Convertexcel(DataTable dt, HttpResponse Response, string filename)
        {

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");//
            Response.Charset = "utf-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="Response"></param>
        /// <param name="filename"></param>
        public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename)
        {

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".csv");
            Response.Charset = "utf-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "Application/x-msexcel";
            StringBuilder sb = new StringBuilder();
            if (dataTable.Columns.Count != 0)
            {
                foreach (DataColumn column in dataTable.Columns)
                {
                    sb.Append(column.ColumnName + ',');
                }
                sb.Append("\r\n");
                foreach (DataRow row in dataTable.Rows)
                {
                    foreach (DataColumn column in dataTable.Columns)
                    {
                        if (row[column].ToString().Contains(",") == true)
                        {
                            row[column] = row[column].ToString().Replace(",", "");
                        }
                        sb.Append(row[column].ToString() + ',');
                    }
                    sb.Append("\r\n");
                }
            }
            Response.Write(sb.ToString());
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();

        }

 

发表评论
用户名: 匿名