Export to Excel from Repeater:
private void ExportExcel()
{
{
// Excel的文件名
string strFileName = System.Configuration.ConfigurationManager.AppSettings["ExcelFileName"];
string strFileName = System.Configuration.ConfigurationManager.AppSettings["ExcelFileName"];
if (strFileName.Substring(strFileName.Length - 4, 4).ToUpper() != ".XLS")
{
strFileName += ".XLS";
}
{
strFileName += ".XLS";
}
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlTextWriter hw = new HtmlTextWriter(sw);
// RepeaterUserList是Repeater控件的ID
this.RepeaterUserList.RenderControl(hw);
this.RepeaterUserList.RenderControl(hw);
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
Response.Write("<html><head><meta http-equiv=Content-Type content=/"text/html; charset=GB2312/"><title> Copyright by SDU</title></head><body><center>");
Response.Write("<table>");
Response.Write("<tr>");
Response.Write("<th>ID</th>");
Response.Write("<th>姓名</th>");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
Response.Write("<html><head><meta http-equiv=Content-Type content=/"text/html; charset=GB2312/"><title> Copyright by SDU</title></head><body><center>");
Response.Write("<table>");
Response.Write("<tr>");
Response.Write("<th>ID</th>");
Response.Write("<th>姓名</th>");
Response.Write("<th>性别</th>");
Response.Write("</tr>");
Response.Write(sw.ToString());
Response.Write("</table>");
Response.Write("</center></body></html>");
Response.End();
}
Response.Write("</tr>");
Response.Write(sw.ToString());
Response.Write("</table>");
Response.Write("</center></body></html>");
Response.End();
}