设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

将ASP.NET页面内的数据导出到Excel 或 Word中

作者:佚名 出处:网络转载 时间:08-24 点击:

内容载入中...
华腾联合陪你一路学习 华腾联合.中国

在以下按钮单击事件中实现:
private void btnMIME_Click(object sender, System.EventArgs e)
{
 BindData();
 Response.ContentType = "application/vnd.ms-Excel";
 Response.AddHeader("Content-Disposition", "inline;filename="
   +   HttpUtility.UrlEncode("下载文件.xls",Encoding.UTF8   )   );  
 

 //如果输出为word,修改为以下代码
 //Response.ContentType = "application/ms-word"
 //Response.AddHeader("Content-Disposition", "inline;filename=test.doc")
 StringBuilder sb=new StringBuilder();
 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
 System.Web.UI.HTMLTextWriter hw = new System.Web.UI.HTMLTextWriter(sw);
 sb.Append("<HTML><body>");
 dgShow.RenderControl(hw);
 sb.Append("</body></HTML>");
 Response.Write(sb.ToString());
 Response.End();
}
注:1.若DataGrid中有按钮列,则在导出前应先将其隐藏.
    2.若DataGrid有分页,而又要打印所有数据的话就应先取消分页.

http://pyt5208.cnblogs.com/archive/2006/07/10/447048.HTML


我要纠错】【进入论坛交流】【关闭此页】【进入博客】
收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息