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

Admin | 2007-12-23 12:13:26 | TrackRecord: 2202 Times | Tag标签:asp.net 打印本页

您当前所处的位置是:〖首页〗→【文章页】 本站共有16个图文教程栏目,请用心拜读!

本站提供经典的Excel公式函数实例,Word排版技巧,PPT教程;同时更兼有Flash,PowerPoint,数据库等技术文章。

在以下按钮单击事件中实现:
private void btnMIME_Click(object sender, System.EventArgs e)
{
dgShow.AllowPaging = false;
 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();
dgShow.AllowPaging = true;
BindData(); 

}
注:1.若DataGrid中有按钮列,则在导出前应先将其隐藏.

2.若DataGrid有分页,而又要打印所有数据的话就应先取消分页.


若为VS.NET2005中的数据绑定新控件GridView,以上代码会提示异常错误,经网上搜索,解决方案如下:

   1、在导出界面中重载
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
   2
web.config中修改<pages enableEventValidation ="false" ></pages>

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

 CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>



上一篇:asp.net图形显示函数    下一篇:asp.net中DataGrid中实现CheckBox列

会员评论列表:
针对本篇文章或本站,请您发表个人的建议或批评!
FreeBoxPc

谷歌搜索 百度搜索 本站仅与内容具备一定的实用价值的原创网站交换友情链接,力争为大众做出更优质的服务!
All Rights Reserved版权所有 本站备案信息:滇ICP备11001339号-2 站长联系方式 Email:dzwebs@126.com