首先添加函数:
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
protected void btnExcel_Click(object sender, EventArgs e)
{
BllPaper bllpaper = new BllPaper();
GridView allmessage = new GridView();
this.pager.Controls.Add(allmessage);
allmessage.ID = "detailsmessage";
allmessage.AutoGenerateColumns = false;
BoundField boundField2 = new BoundField();
allmessage.Columns.Add(boundField2);
boundField2.HeaderText = "学生学号 ";
boundField2.DataField = "StudentID";
BoundField boundField1 = new BoundField();
allmessage.Columns.Add(boundField1);
boundField1.HeaderText = "学生姓名 ";
boundField1.DataField = "StudentName";
BoundField boundField3 = new BoundField();
allmessage.Columns.Add(boundField3);
boundField3.HeaderText = "所在学院 ";
boundField3.DataField = "College";
BoundField boundField4 = new BoundField();
allmessage.Columns.Add(boundField4);
boundField4.HeaderText = "学位 ";
boundField4.DataField = "Degree";
BoundField boundField5 = new BoundField();
allmessage.Columns.Add(boundField5);
boundField5.HeaderText = "专业";
boundField5.DataField = "Degree";
BoundField boundField6 = new BoundField();
allmessage.Columns.Add(boundField6);
boundField6.HeaderText = "论文题目";
boundField6.DataField = "PaperTitle";
BoundField boundField7 = new BoundField();
allmessage.Columns.Add(boundField7);
boundField7.HeaderText = "答辩委员会秘书";
boundField7.DataField = "Secretary";
BoundField boundField8 = new BoundField();
allmessage.Columns.Add(boundField8);
boundField8.HeaderText = "答辩时间 ";
boundField8.DataField = "AnswerTime";
BoundField boundField9 = new BoundField();
allmessage.Columns.Add(boundField9);
boundField9.HeaderText = "答辩地点 ";
boundField9.DataField = "AnswerPlace";
allmessage.DataSource = bllpaper.GetAllCheckedPaperList();
allmessage.DataBind();
ToExcel(allmessage, "已审核答辩论文列表.xls");
}
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + HttpUtility.UrlEncode(FileName).ToString());
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}