Excel文件下载功能

HtmlTextWriter:能够帮助我们快速生成html语句。

下载功能实现代码

 1    protected void Button1_Click(object sender, EventArgs e)
 2         {
 3             int currentPage = AspNetPager1.CurrentPageIndex;
 4             int pageSize = AspNetPager1.PageSize;
 5             int recordCount;
 6 
 7             StudentDAL dal = new StudentDAL();
 8             DataSet ds = dal.GetPage(currentPage, pageSize, out recordCount);
 9             Repeater1.DataSource = ds;
10             Repeater1.DataBind();
11             ToExcel(Repeater1, "FinanceInContractor.xls");
12         }
13 
14         #region 导出为Excel
15         private void ToExcel(Control ctl, string FileName)
16         {
17             HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
18             ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter();
19             HtmlTextWriter hw = new HtmlTextWriter(tw);
20             ctl.RenderControl(hw);
21             HttpContext.Current.Response.Write(tw.ToString());
22             HttpContext.Current.Response.End();
23         }
24         #endregion
View Code
原文地址:https://www.cnblogs.com/move-up/p/5939209.html