Asp.net export Excel: Server Control DataList

Source: Internet
Author: User

Pre-release:

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </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 = "Export Excel" BorderColor = "# B2B2B2" BorderStyle = "Solid" BorderWidth = "1px"
BackColor = "# EFEFEF" onclick = "button?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; (huizhi) E-commerce subsidiary limited </td>
</Tr>
<Tr>
<Td colspan = "7" align = "center">
Sub-renewal item external orders </td>
</Tr>
<Tr>
<Td align = "left" colspan = "2">
Bu quota: <% # eval ("applyerdeptname") %> & nbsp; </TD>
<TD align = "Left" colspan = "2">
Distributors: <% # eval ("company2") %> & nbsp; </TD>
<TD>
Date: <% # eval ("date") %> & nbsp; </TD>
</Tr>


<Tr>
<TD align = "center"> product name </TD>
<TD align = "center"> specifications </TD>
<Td align = "center"> single bit </td>
<Td align = "center"> data volume </td>
<Td align = "center"> single region </td>
<Td align = "center"> Golden Week </td>
<Td align = "center"> notice note </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"> General Fund </td>
<TD colspan = "6" align = "Left"> Small Partition: <% # eval ("money8") %> & nbsp; </TD>
</Tr>
<Tr>
<TD colspan = "6" align = "Left"> large queue: <% # eval ("money") %> & nbsp; queue <% # eval ("money2 ") %> & nbsp; percent <% # eval ("money3") %> & nbsp; percent <% # eval ("money4") %> & nbsp;
Pickup <% # eval ("money5") %> & nbsp; RMB <% # eval ("money6") %> & nbsp; angle <% # eval ("money7 ") %> & nbsp; Points </TD>
</Tr>
<Tr>
<TD colspan = "7"> attachment: <% # eval ("FILENAME") %> & nbsp; </TD>
</Tr>
<Tr>
<TD align = "center"> Commitment: </TD>
<TD> <% # eval ("undertake") %> & nbsp; </TD>
<TD align = "center"> Supervisor: </TD>
<TD> <% # eval ("charge") %> & nbsp; </TD>
<TD align = "center"> chief executive: </TD>
<TD colspan = "2"> <% # eval ("generaldirector") %> & nbsp; </TD>
</Tr>
</Table>

</ItemTemplate>
</Asp: DataList>
</Div>
</Form>
</Body>
</Html>
Post-renewal:

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 button#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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.