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")%>&nbsp;(惠陽)電子有限公司</td>
                    </tr>
                    <tr>
                        <td colspan="7" align="center">
                            次廢物品外賣單</td>
                    </tr>
                    <tr>
                        <td align="left" colspan="2">
                            部門:<%# Eval("ApplyerDeptName")%>&nbsp;</td>
                        <td align="left" colspan="2">
                            廠商:<%# Eval("Company2")%>&nbsp;</td>
                        <td>
                            日期:<%# Eval("Date")%>&nbsp;</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") %>&nbsp;</td>
                <td><%# Eval("Specification")%>&nbsp;</td>
                <td><%# Eval("Unit")%>&nbsp;</td>
                <td><%# Eval("Quantity")%>&nbsp;</td>
                <td><%# Eval("UnitPrice") %>&nbsp;</td>
                <td><%# Eval("Amount") %>&nbsp;</td>
                    <td>
                        <%# Eval("Name1") %>&nbsp;</td>
                    <td>
                        <%# Eval("Specification")%>&nbsp;</td>
                    <td>
                        <%# Eval("Unit")%>&nbsp;</td>
                    <td>
                        <%# Eval("Quantity")%>&nbsp;</td>
                    <td>
                        <%# Eval("UnitPrice") %>&nbsp;</td>
                    <td>
                        <%# Eval("Amount") %>&nbsp;</td>
                <td rowspan="7"><%# Eval("Remark")%>&nbsp;</td>
                </tr>
                <tr>
                <td><%# Eval("Name2") %>&nbsp;</td>
                <td><%# Eval("Specification2")%>&nbsp;</td>
                <td><%# Eval("Unit2") %>&nbsp;</td>
                <td><%# Eval("Quantity2")%>&nbsp;</td>
                <td><%# Eval("UnitPrice2") %>&nbsp;</td>
                <td><%# Eval("Amount2") %>&nbsp;</td>
                </tr>
                <tr>
                <td><%# Eval("Name3")%>&nbsp;</td>
                <td><%# Eval("Specification3")%>&nbsp;</td>
                <td><%# Eval("Unit3") %>&nbsp;</td>
                <td><%# Eval("Quantity3")%>&nbsp;</td>
                <td><%# Eval("UnitPrice3")%>&nbsp;</td>
                <td><%# Eval("Amount3")%>&nbsp;</td>
                </tr>
                <tr>
                <td><%# Eval("Name4")%>&nbsp;</td>
                <td><%# Eval("Specification4") %>&nbsp;</td>
                <td><%# Eval("Unit4") %>&nbsp;</td>
                <td><%# Eval("Quantity4") %>&nbsp;</td>
                <td><%# Eval("UnitPrice4")%>&nbsp;</td>
                <td><%# Eval("Amount4") %>&nbsp;</td>
                </tr>
                <tr>
                <td><%# Eval("Name5")%>&nbsp;</td>
                <td><%# Eval("Specification5") %>&nbsp;</td>
                <td><%# Eval("Unit5") %>&nbsp;</td>
                <td><%# Eval("Quantity5")%>&nbsp;</td>
                <td><%# Eval("UnitPrice5") %>&nbsp;</td>
                <td><%# Eval("Amount5")%>&nbsp;</td>
                </tr>
                <tr>
                <td><%# Eval("Name7")%>&nbsp;</td>
                <td><%# Eval("Specification7") %>&nbsp;</td>
                <td><%# Eval("Unit7") %>&nbsp;</td>
                <td><%# Eval("Quantity7") %>&nbsp;</td>
                <td><%# Eval("UnitPrice7")%>&nbsp;</td>
                <td><%# Eval("Amount7")%>&nbsp;</td>
                </tr>
                <tr>
                <td rowspan="2" align="center">總金額</td>
                <td colspan="6" align="left">小寫:<%# Eval("Money8")%>&nbsp;</td>
                </tr>
                <tr>
                <td colspan="6" align="left">大寫:<%# Eval("Money")%>&nbsp;萬 <%# Eval("Money2")%>&nbsp;仟<%# Eval("Money3")%>&nbsp; 佰<%# Eval ("Money4") %>&nbsp;
                    拾<%# Eval("Money5")%>&nbsp; 元<%# Eval("Money6")%>&nbsp; 角<%# Eval("Money7")%>&nbsp;&nbsp; 分</td>
                </tr>
                <tr>
                <td colspan="7">附件:<%# Eval("FileName")%>&nbsp;</td>
                </tr>
                <tr>
                <td align="center">承辦:</td>
                <td><%# Eval("Undertake")%>&nbsp;</td>
                <td align="center">主管:</td>
                <td><%# Eval("Charge")%>&nbsp;</td>
                <td align="center">總務主管:</td>
                <td colspan="2"><%# Eval("GeneralDirector")%>&nbsp;</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;
    }
 }

posted @ 2010-07-31 15:25  Aliceblogs  阅读(393)  评论(0编辑  收藏  举报