正文

将ASP.NET页面内的数据导出到Excel 或 Word中2006-08-03 11:21:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/17150.html

分享到:

 

在以下按钮单击事件中实现:
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有分页,而又要打印所有数据的话就应先取消分页.

阅读(2135) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册