.net中将DataTable导出到word、Excel、txt、htm的方法

作者:金牛区福生殡葬服务部 来源:www.cdfsbz.com 发布时间:2017-09-09 10:24:08
.net中将DataTable导出到word、Excel、txt、htm的方法

dt:DataTable

strFile:fileName

strExt:type


private void GridExport(DataTable dt, string strFile, string strExt)
{
string strAppType = "";
switch (strExt)
{
case "xls":
strAppType = "application/ms-excel";
break;
case "doc":
strAppType = "application/ms-word";
break;
case "txt":
strAppType = "application/ms-txt";
break;
case "html":
case "htm":
strAppType = "application/ms-html";
break;
default: return;
}
GridView MyGridView = new GridView();
MyGridView.DataSource = dt;
MyGridView.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("Content-Type", "text/html; charset=GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.{1}", HttpUtility.UrlEncode(strFile,Encoding.GetEncoding("GB2312")), strExt));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = strAppType;
//MyGridView.Page.EnableViewState = false;
//二、定义一个输入流
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//三、将目标数据绑定到输入流输出
MyGridView.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End();
}

,专题系统

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:鄂州网站建设 http://ezhou.45qun.com

  • 上一篇:Python之print
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 金牛区福生殡葬服务部 ALL RIGHTS RESERVED.
    购买cdfsbz.com友情链接、项目合作请联系客服QQ:2500-38-100 邮箱:2500-38-100#QQ.com(#换@)
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100