//excel文件名
string filename = "考勤汇总";
StringBuilder ExcelHtml = new StringBuilder();
ExcelHtml.Append("<table border='1' style='vnd.ms-excel.numberformat:@'>");
//拼接<tr><th></th></tr>第一行标题
//循环拼接<tr><td></td></tr>每一行的内容
ExcelHtml.Append("</table>");
DownloadExcelByTtml(ExcelHtml.ToString(), filename);
/// <summary>
/// 下载 Excel
/// </summary>
/// <param name="ExcelHtml"></param>
/// <param name="filename"></param>
/// <returns></returns>
public void DownloadExcelByTtml(string ExcelHtml, string filename)
{
Page p = HttpContext.Current.Handler as Page;
p.Response.Clear();
p.Response.Buffer = true;
p.Response.Charset = "UTF-8";
p.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");
p.Response.ContentEncoding = Encoding.UTF8;
p.Response.ContentType = "application/vnd.ms-excel";
p.EnableViewState = false;
p.Response.Write("<html><head><meta http-equiv='Content-Type' content='application/vnd.ms-excel; charset=utf-8' /></head>" + ExcelHtml + "</html>");
p.Response.End();
}