实现GridView中的数据的打印和导出到Excel

MSDN上对 VerifyRenderingInServerForm 方法的解释是:
  必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法。呈现服务器窗体元素的方式不同的页可以重写此方法以在不同的条件下引发异常。
  如果回发或使用客户端脚本的服务器控件没有包含在 HtmlForm 服务器控件 (<form runat="server">) 标记中,它们将无法正常工作。这些控件可以在呈现时调用该方法,以在它们没有包含在 HtmlForm 控件中时提供明确的错误信息。
  开发自定义服务器控件时,通常在为任何类型的输入标记重写 Render 方法时调用该方法。这在输入控件调用 GetPostBackEventReference 或发出客户端脚本时尤其重要。复合服务器控件不需要作出此调用,没有这个方法,程序将报错。
前台代码:
 1 <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " TeacherReport.aspx.cs "  Inherits = " TeacherCenter_TeacherReport "   %>
 2
 3 <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
 4
 5 < html xmlns = " http://www.w3.org/1999/xhtml "   >
 6 < head runat = " server " >
 7      < title > 教师花名册 </ title >
 8      < link type = " text/css "  rel = " stylesheet "  href = " ../App_themes/css/global.css "   />
 9 </ head >
10 < body >
11      < object  id = " WebBrowser "  classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 "  height = " 0 "
12         width = " 0 " >
13      </ object >
14      < form id = " form1 "  runat = " server " >
15      < div style = " text-align: center " >
16          < table border = " 0 "  cellpadding = " 0 "  style = " text-align: left "  width = " 600 " >
17              < tr height = " 23 " >
18                  < td align = " center "  style = " font-size: 25px; height: 23px " >
19                     临沂市兰山区委党校 </ td >
20              </ tr >
21              < tr height = " 23 " >
22                  < td align = " center "  colspan = " 2 "  style = " font-size: 25px; height: 23px " >
23                     教师花名册 </ td >
24              </ tr >
25              < tr >
26                  < td align = " right "  colspan = " 2 " >
27                      < asp:Label ID = " lblDateTime "  runat = " server "  Text = " lblDateTime "  Width = " 110px " ></ asp:Label ></ td >
28              </ tr >
29              < tr >
30                  < td colspan = " 2 "  style = " height: 21px "  valign = " top " >
31                      & nbsp; < asp:GridView ID = " gvTeacherInfoList "  runat = " server "  AllowPaging = " True "
32                         AutoGenerateColumns = " False "
33                         PageSize = " 40 "  Width = " 600px "  OnPageIndexChanging = " gvTeacherInfoList_PageIndexChanging " >
34                          < FooterStyle HorizontalAlign = " Right "   />
35                          < Columns >
36                              < asp:TemplateField HeaderText = " 序号 " >
37                                  < HeaderTemplate >
38                                     序号
39                                  </ HeaderTemplate >
40                                  < ControlStyle Width = " 10% "   />
41                                  < ItemTemplate >
42                                      <% #Container.DataItemIndex + 1 %>
43                                  </ ItemTemplate >
44                              </ asp:TemplateField >
45                              < asp:BoundField DataField = " Teacher_Name "  HeaderText = " 姓名 "  SortExpression = " Teacher_Name "   />
46                              < asp:BoundField DataField = " TeacherSex "  HeaderText = " 性别 "  SortExpression = " TeacherSex "   />
47                              < asp:BoundField DataField = " Teacher_Age "  HeaderText = " 年龄 "  SortExpression = " Teacher_Age "   />
48                              < asp:BoundField DataField = " TeacherDuty "  HeaderText = " 职务 "  SortExpression = " TeacherDuty "   />
49                              < asp:BoundField DataField = " Teacher_Speciality "  HeaderText = " 专业 "  SortExpression = " Teacher_Speciality "   />
50                              < asp:BoundField DataField = " Teacher_RelationMode "  HeaderText = " 联系方式 "   />
51                          </ Columns >
52                      </ asp:GridView >
53                  </ td >
54              </ tr >
55              < tr >
56                  < td align = " right "  colspan = " 2 "  style = " height: 21px "  valign = " top " >
57                      < asp:Button ID = " btnPrint "  runat = " server "  Text = " 打 印 "  Width = " 53px "  OnClick = " btnPrint_Click "   />
58                      < asp:Button ID = " btnExcel "  runat = " server "  Text = " 导出到Excel "  OnClick = " btnExcel_Click "   /></ td >
59              </ tr >
60          </ table >
61     
62      </ div >
63      </ form >
64 </ body >
65 </ html >
66

后台代码:

 1 using  System;
 2 using  System.Data;
 3 using  System.Configuration;
 4 using  System.Collections;
 5 using  System.Web;
 6 using  System.Web.Security;
 7 using  System.Web.UI;
 8 using  System.Web.UI.WebControls;
 9 using  System.Web.UI.WebControls.WebParts;
10 using  System.Web.UI.HtmlControls;
11 using  System.Data.SqlClient;
12 using  Lymit.DistanceEDU.Model;
13 using  Lymit.Distanct.Model;
14 using  BLL;
15
16 public  partial  class  TeacherCenter_TeacherReport : System.Web.UI.Page
17 {
18    protected void Page_Load(object sender, EventArgs e)
19    {
20        if (!Page.IsPostBack)
21        {
22            BindTeacherInfo();
23        }

24        this.lblDateTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
25    }

26
27    private void BindTeacherInfo()
28    {
29        TeacherController tc = TeacherController.GetInstance();
30        string sex = "全部";
31        DataTable dt = tc.GetTeacherRoster(sex);
32        this.gvTeacherInfoList.DataSource = dt.DefaultView;
33        this.gvTeacherInfoList.DataBind();
34    }

35
36    protected void gvTeacherInfoList_PageIndexChanging(object sender, GridViewPageEventArgs e)
37    {
38        this.gvTeacherInfoList.PageIndex = e.NewPageIndex;
39        BindTeacherInfo();
40    }

41
42    protected void btnExcel_Click(object sender, EventArgs e)
43    {
44        Response.Clear();
45        Response.Buffer = true;
46        Response.Charset = "GB2312";
47        Response.AppendHeader("Content-Disposition""attachment;filename=FileName.xls");
48        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
49        Response.ContentEncoding = System.Text.Encoding.UTF7;
50        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
51        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
52        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
53        this.gvTeacherInfoList.RenderControl(oHtmlTextWriter);
54        Response.Output.Write(oStringWriter.ToString());
55        Response.Flush();
56        Response.End();
57    }

58
59    public override void VerifyRenderingInServerForm( Control control )
60    {
61
62    }

63
64    protected override void Render(HtmlTextWriter writer)
65    {
66        if (Page != null)
67        {
68            Page.VerifyRenderingInServerForm(this);
69        }

70        base.Render(writer);
71    }

72    protected void btnPrint_Click(object sender, EventArgs e)
73    {
74        Response.Write("<script> window.print()</script>");
75    }

76}

77
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值