You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.6 KiB
C#

2 months ago
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("<table cellpadding=\"0\" cellspacing=\"0\" style=\"border:1px solid black;text-align:center;\" width=\"700\">\n");
html.Append("<tr>\n<td style=\"border-bottom: 1px solid black;text-align:center;\" colspan=\"6\">XXXX年XX月课程表<img style='border: 0px currentColor; border-image: none; width: 393px; height: 38px;' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAAmCAYAAAAx4GcyAAAA80lEQVR42u3VOwrAIBBAQe9/acXCRlhcRRuZaRSS4AfCKwUAAAAAAAAAAABeqMOY9zGaR++tns9jdp3ou5397pzj5Dyv7yB7llvr3LiD7DlWe/N7gkiIhEiIBIiESIiESIBIiIRIiASIhEiIhEiASIiESIgEiIRIiIRIACIhEiIBiIRIiAQgEiIhEoBIiIRIACIhEiIhEiASIiESIgEiIRIiIRIgEiIhEiIBIiESIiESIBIiIRIiAYiESIgEIBIiIRKASIiESAAiIRIiAYiESIiESIBIiIRIiASIhEiIhEiASIiESIgEAAAAAAAAAAAAAPCBBlvO0kRfPFVTAAAAAElFTkSuQmCC'></img></td>\n</tr>\n");
/*********************************************/
html.Append("<tr>\n");
for (int i = 0; i <= 5; i++)
{
if (i == 5)
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align: center;\">" + (string.IsNullOrEmpty(toCNNum(i)) ? "&nbsp;" : toCNNum(i)) + "</td>\n");
}
else
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;border-right: 1px solid black;\">" + (string.IsNullOrEmpty(toCNNum(i)) ? "&nbsp;" : toCNNum(i)) + "</td>\n");
}
}
html.Append("</tr>\n");
/*********************************************/
html.Append("<tr>\n");
for (int i = 0; i <= 5; i++)
{
if (i == 0)
{
html.Append("<td rowspan=\"5\" style=\"border-bottom: 1px solid black;text-align:center;border-right: 1px solid black;\">上午</td>\n");
}
else
{
if (i == 5)
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;\">课程</td>\n");
}
else
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;border-right: 1px solid black;\">课程</td>\n");
}
}
}
html.Append("</tr>");
/*********************************************/
for (int i = 0; i <= 3; i++)
{
html.Append("<tr>\n");
for (int j = 0; j <= 4; j++)
{
if (j == 4)
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;\">课程</td>\n");
}
else
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;border-right: 1px solid black;\">课程</td>\n");
}
}
html.Append("</tr>\n");
}
/*********************************************/
html.Append("<tr>\n");
html.Append("<td colspan=\"6\" style=\"border-bottom: 1px solid black;text-align:center;\">&nbsp;</td>\n");
html.Append("</tr>\n");
/*********************************************/
html.Append("<tr>\n");
for (int i = 0; i <= 5; i++)
{
if (i == 0)
{
html.Append("<td rowspan=\"2\" style=\"border-right : 1px solid black;text-align: center;\">下午</td>\n");
}
else
{
if (i == 5)
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;\">课程</td>\n");
}
else
{
html.Append("<td style=\"border-bottom: 1px solid black;text-align:center;border-right: 1px solid black;\">课程</td>\n");
}
}
}
html.Append("</tr>");
/*********************************************/
html.Append("<tr>");
for (int i = 0; i <= 4; i++)
{
if (i == 4)
{
html.Append("<td style=\"text-align: center;\">课程</td>\n");
}
else
{
html.Append("<td style=\"border-right : 1px solid black;text-align: center;\">课程</td>\n");
}
}
html.Append("</tr>");
/*********************************************/
html.Append("</table>");
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();
}
}
}