<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Table2Excel.aspx.cs"
Inherits="VS2013Test.AspNetTest.Table2Excel" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbl" runat="server" border="1">
<tr>
<td colspan="2"> <b>標題一</b> </td> <td colspan="2"> <b>標題二</b> </td>
</tr>
<tr style="color:red">
<td style="text-align:left"> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
<tr>
<td> 2</td> <td>3.6 </td> <td>112 </td> <td>3.05 </td>
</tr>
<tr>
<td> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
<tr>
<td> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
</table>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<p>
<asp:Button ID="Button1" runat="server" OnClientClick="return confirm('確定下載?')" Text="excel" OnClick="Button1_Click" />
</p>
<p>
<asp:Button ID="Button2" runat="server" OnClientClick="return confirm('確定下載?')" Text="TXT" OnClick="Button2_Click" />
</p>
</div>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace VS2013Test.AspNetTest
{
public partial class Table2Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string content = " <table border=\"1\" style=\"border-top:1px solid black;font-size:10px\" > ";
content += "<tr> <td colspan=\"2\"> <b>標題一</b> </td> <td colspan=\"2\"> <b>標題二</b> </td> </tr> ";
content += "<tr style=\"color:red\"> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr> ";
content += "<tr> <td> 2 </td> <td style=\"text-align:right\">" + (char)2 + "3.6100 </td> <td>112 </td> <td>3.05 </td> </tr> ";
content += " <tr> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr>";
content += " <tr> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr>";
content += "";
Literal1.Text = content;
Response.ContentType = "application/x-msexcel";
Response.AddHeader("Content-Disposition", "attachment;filename="+ DateTime.Now.ToString("yyyyMMddHHmm") +".xls");
Response.ContentEncoding = Encoding.GetEncoding("big5");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Literal1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string output = "Ac8!".PadRight(10, '-');
sb.Append(output);
sb.Append("\r\n");
output = "中文測試".PadLeft(10,'-');
sb.Append(output);
string text = sb.ToString();
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Length", text.Length.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt");
Response.Write(text);
Response.End();
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbl" runat="server" border="1">
<tr>
<td colspan="2"> <b>標題一</b> </td> <td colspan="2"> <b>標題二</b> </td>
</tr>
<tr style="color:red">
<td style="text-align:left"> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
<tr>
<td> 2</td> <td>3.6 </td> <td>112 </td> <td>3.05 </td>
</tr>
<tr>
<td> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
<tr>
<td> A</td> <td>B </td> <td>C </td> <td>D </td>
</tr>
</table>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<p>
<asp:Button ID="Button1" runat="server" OnClientClick="return confirm('確定下載?')" Text="excel" OnClick="Button1_Click" />
</p>
<p>
<asp:Button ID="Button2" runat="server" OnClientClick="return confirm('確定下載?')" Text="TXT" OnClick="Button2_Click" />
</p>
</div>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace VS2013Test.AspNetTest
{
public partial class Table2Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string content = " <table border=\"1\" style=\"border-top:1px solid black;font-size:10px\" > ";
content += "<tr> <td colspan=\"2\"> <b>標題一</b> </td> <td colspan=\"2\"> <b>標題二</b> </td> </tr> ";
content += "<tr style=\"color:red\"> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr> ";
content += "<tr> <td> 2 </td> <td style=\"text-align:right\">" + (char)2 + "3.6100 </td> <td>112 </td> <td>3.05 </td> </tr> ";
content += " <tr> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr>";
content += " <tr> <td> A</td> <td>B </td> <td>C </td> <td>D </td> </tr>";
content += "";
Literal1.Text = content;
Response.ContentType = "application/x-msexcel";
Response.AddHeader("Content-Disposition", "attachment;filename="+ DateTime.Now.ToString("yyyyMMddHHmm") +".xls");
Response.ContentEncoding = Encoding.GetEncoding("big5");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Literal1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string output = "Ac8!".PadRight(10, '-');
sb.Append(output);
sb.Append("\r\n");
output = "中文測試".PadLeft(10,'-');
sb.Append(output);
string text = sb.ToString();
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Length", text.Length.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt");
Response.Write(text);
Response.End();
}
}
}