当前位置:开发者网络 >> 技术教程 >> .NET教程 >> Asp.Net开发 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
将ASP.NET页面内的数据导出到Excel 或 Word中
作者:未知
日期:2006-10-16
人气:
投稿:(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
 

在以下按钮单击事件中实现:
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

相关文章: