Asp.net導出Excel:服務器控件DataList
前臺:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server" style="text-align:center;">
<div style="width: 750px; " >
<div style="text-align:left;"><asp:Button ID="Button1" runat="server"
Text="導出Excel" BorderColor="#B2B2B2" BorderStyle="Solid" BorderWidth="1px"
BackColor="#EFEFEF" onclick="Button1_Click" /></div><br />
<br />
<asp:DataList ID="DataList1" runat="server" style="width:730px;">
<ItemTemplate>
<table class="style1" width="730" border="1" cellpadding="0" cellspacing="0" >
<tr>
<td colspan="7" align="center">
<%# Eval("Company")%> (惠陽)電子有限公司</td>
</tr>
<tr>
<td colspan="7" align="center">
次廢物品外賣單</td>
</tr>
<tr>
<td align="left" colspan="2">
部門:<%# Eval("ApplyerDeptName")%> </td>
<td align="left" colspan="2">
廠商:<%# Eval("Company2")%> </td>
<td>
日期:<%# Eval("Date")%> </td>
</tr>
<tr>
<td align="center">品名</td>
<td align="center">規格</td>
<td align="center">單位</td>
<td align="center">數量</td>
<td align="center">單價</td>
<td align="center">金額</td>
<td align="center">備注</td>
</tr>
<tr>
</tr>
<tr>
<td><%# Eval("Name1") %> </td>
<td><%# Eval("Specification")%> </td>
<td><%# Eval("Unit")%> </td>
<td><%# Eval("Quantity")%> </td>
<td><%# Eval("UnitPrice") %> </td>
<td><%# Eval("Amount") %> </td>
<td>
<%# Eval("Name1") %> </td>
<td>
<%# Eval("Specification")%> </td>
<td>
<%# Eval("Unit")%> </td>
<td>
<%# Eval("Quantity")%> </td>
<td>
<%# Eval("UnitPrice") %> </td>
<td>
<%# Eval("Amount") %> </td>
<td rowspan="7"><%# Eval("Remark")%> </td>
</tr>
<tr>
<td><%# Eval("Name2") %> </td>
<td><%# Eval("Specification2")%> </td>
<td><%# Eval("Unit2") %> </td>
<td><%# Eval("Quantity2")%> </td>
<td><%# Eval("UnitPrice2") %> </td>
<td><%# Eval("Amount2") %> </td>
</tr>
<tr>
<td><%# Eval("Name3")%> </td>
<td><%# Eval("Specification3")%> </td>
<td><%# Eval("Unit3") %> </td>
<td><%# Eval("Quantity3")%> </td>
<td><%# Eval("UnitPrice3")%> </td>
<td><%# Eval("Amount3")%> </td>
</tr>
<tr>
<td><%# Eval("Name4")%> </td>
<td><%# Eval("Specification4") %> </td>
<td><%# Eval("Unit4") %> </td>
<td><%# Eval("Quantity4") %> </td>
<td><%# Eval("UnitPrice4")%> </td>
<td><%# Eval("Amount4") %> </td>
</tr>
<tr>
<td><%# Eval("Name5")%> </td>
<td><%# Eval("Specification5") %> </td>
<td><%# Eval("Unit5") %> </td>
<td><%# Eval("Quantity5")%> </td>
<td><%# Eval("UnitPrice5") %> </td>
<td><%# Eval("Amount5")%> </td>
</tr>
<tr>
<td><%# Eval("Name7")%> </td>
<td><%# Eval("Specification7") %> </td>
<td><%# Eval("Unit7") %> </td>
<td><%# Eval("Quantity7") %> </td>
<td><%# Eval("UnitPrice7")%> </td>
<td><%# Eval("Amount7")%> </td>
</tr>
<tr>
<td rowspan="2" align="center">總金額</td>
<td colspan="6" align="left">小寫:<%# Eval("Money8")%> </td>
</tr>
<tr>
<td colspan="6" align="left">大寫:<%# Eval("Money")%> 萬 <%# Eval("Money2")%> 仟<%# Eval("Money3")%> 佰<%# Eval ("Money4") %>
拾<%# Eval("Money5")%> 元<%# Eval("Money6")%> 角<%# Eval("Money7")%> 分</td>
</tr>
<tr>
<td colspan="7">附件:<%# Eval("FileName")%> </td>
</tr>
<tr>
<td align="center">承辦:</td>
<td><%# Eval("Undertake")%> </td>
<td align="center">主管:</td>
<td><%# Eval("Charge")%> </td>
<td align="center">總務主管:</td>
<td colspan="2"><%# Eval("GeneralDirector")%> </td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
后臺:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class ExportExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "server=192.168.3.127;database=FlowMaster;UId=sa;password=bruce_zhao888418";
SqlConnection sqlcon = new SqlConnection(str);
sqlcon.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from afu_form_CFWP where 1=1 and SerialID='CFWP2010072816340628055'", sqlcon);
DataSet ds = new DataSet();
sqlda.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
//Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "text/HTML";
this.EnableViewState = false;
}
}