using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace TradeManage.TongJi { public partial class Excel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { htmlTag.InnerHtml = createTable(); } } protected string createTable() { StringBuilder html = new StringBuilder(); html.Append("\n"); html.Append("\n\n\n"); /*********************************************/ html.Append("\n"); for (int i = 0; i <= 5; i++) { if (i == 5) { html.Append("\n"); } else { html.Append("\n"); } } html.Append("\n"); /*********************************************/ html.Append("\n"); for (int i = 0; i <= 5; i++) { if (i == 0) { html.Append("\n"); } else { if (i == 5) { html.Append("\n"); } else { html.Append("\n"); } } } html.Append(""); /*********************************************/ for (int i = 0; i <= 3; i++) { html.Append("\n"); for (int j = 0; j <= 4; j++) { if (j == 4) { html.Append("\n"); } else { html.Append("\n"); } } html.Append("\n"); } /*********************************************/ html.Append("\n"); html.Append("\n"); html.Append("\n"); /*********************************************/ html.Append("\n"); for (int i = 0; i <= 5; i++) { if (i == 0) { html.Append("\n"); } else { if (i == 5) { html.Append("\n"); } else { html.Append("\n"); } } } html.Append(""); /*********************************************/ html.Append(""); for (int i = 0; i <= 4; i++) { if (i == 4) { html.Append("\n"); } else { html.Append("\n"); } } html.Append(""); /*********************************************/ html.Append("
XXXX年XX月课程表
" + (string.IsNullOrEmpty(toCNNum(i)) ? " " : toCNNum(i)) + "" + (string.IsNullOrEmpty(toCNNum(i)) ? " " : toCNNum(i)) + "
上午课程课程
课程课程
 
下午课程课程
课程课程
"); return html.ToString(); } protected string toCNNum(int num) { string str = string.Empty; switch (num) { case 1: str = "一"; break; case 2: str = "二"; break; case 3: str = "三"; break; case 4: str = "四"; break; case 5: str = "五"; break; } return str; } protected void Export_Click(object sender, EventArgs e) { //输出的应用类型 Response.ContentType = "application/vnd.ms-excel"; //设定编码方式,若输出的excel有乱码,可优先从编码方面解决 // Response.Charset = "gb2312"; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭ViewState,此属性在Page中 EnableViewState = false; //filenames是自定义的文件名 Response.AppendHeader("Content-Disposition", "attachment;filename=data.xls"); //content是步骤1的html,注意是string类型 Response.Write(createTable()); Response.End(); } } }