Asp. NET Export data to Excel

Source: Internet
Author: User
Tags html page tostring
asp.net|excel| Export Data The method simply saves the ASP.net page to an HTML page and simply changes the suffix to XLC but Excel can read it, and then I'll see if there is another way to export the data and use the template to generate it.

Here is the code

County New Asp.ne Tweb application just paste the code in.

HTML page code


<%@ Page language= "C #" codebehind= "OutExcel.aspx.cs" autoeventwireup= "false" inherits= "EMeng.Exam.OutPutExcel"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server" >
<Columns>
<asp:BoundColumn></asp:BoundColumn>
</Columns>
</asp:datagrid>
<P>
<asp:label id= "Label1" runat= "server" > FileName:</asp:label>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "server" text= "output to Excel" ></asp:button></P>
</form>
</body>
</HTML>

Next is the code in the CS page

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;

Namespace Emeng.exam
{
<summary>
Summary description of the outputexcel.
</summary>
public class OutPutExcel:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
Private DataSet myDS =new DataSet ();

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! Page.IsPostBack)
{
Data_load ()//Call Method Fill table
}

}
<summary>
Create a data source
</summary>
<returns>DataView</returns>
private void Data_load ()
{
Database connection string catalog to the specified database name, datasource to the SQL Server name to be connected
String myconn = "User id=sa; Password=sa;initial Catalog=test;data SOURCE=ZXB; Connect timeout=20 ";
Query string
String Mysqlstr= "SELECT * from FY";
Connecting to database operations
SqlConnection myconnection = new SqlConnection (myconn);
Executing SQL statement Operations
SqlDataAdapter mydataadapter = new SqlDataAdapter (mysqlstr,myconnection);
Open Database
Myconnection.open ();
Populating the DataSet with data, populating the FY table in the test library in the database server
Mydataadapter.fill (myds, "FY");
Populating the Dastagrid with data
datagrid1.datasource=myds;
Datagrid1.databind ();
}
<summary>
Output to Excel
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, System.EventArgs e)
{
if (textbox1.text== "")
{
Response.Write ("<script language=javascript>");
Response.Write ("Window.alert (' please enter a filename ');";
Response.Write ("</SCRIPT>");
}
Else
{
Response.Clear ();
Response.buffer= true;
Response.charset= "GB2312"; Set the type to prevent the appearance of garbled characters
Response.appendheader ("Content-disposition", "Attachment;filename=" +textbox1.text+ ". xls"); Defining output files and file names
Response.contentencoding=system.text.encoding.getencoding ("GB2312");/set output stream to Simplified Chinese
Response.ContentType = "Application/ms-excel";//Set Output file type to excel file.
This. EnableViewState = false;
System.Globalization.CultureInfo Mycitrad = new System.Globalization.CultureInfo ("ZH-CN", true);
System.IO.StringWriter ostringwriter = new System.IO.StringWriter (Mycitrad);
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);
This. Datagrid1.rendercontrol (Ohtmltextwriter);
Response.Write (Ostringwriter.tostring ());
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.itemdatabound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this. Datagrid1_itemdatabound);
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Datagrid1_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)
{
E.item.cells[0]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:@");
E.ITEM.CELLS[3]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}

are continuing to study other ways



<%@ Page language= "C #" codebehind= "OutExcel.aspx.cs" autoeventwireup= "false" inherits= "EMeng.Exam.OutPutExcel"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server" >
<Columns>
<asp:BoundColumn></asp:BoundColumn>
</Columns>
</asp:datagrid>
<P>
<asp:label id= "Label1" runat= "server" > FileName:</asp:label>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "server" text= "output to Excel" ></asp:button></P>
</form>
</body>
</HTML>

Next is the code in the CS page

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;

Namespace Emeng.exam
{
<summary>
Summary description of the outputexcel.
</summary>
public class OutPutExcel:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
Private DataSet myDS =new DataSet ();

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! Page.IsPostBack)
{
Data_load ()//Call Method Fill table
}

}
<summary>
Create a data source
</summary>
<returns>DataView</returns>
private void Data_load ()
{
Database connection string catalog to the specified database name, datasource to the SQL Server name to be connected
String myconn = "User id=sa; Password=sa;initial Catalog=test;data SOURCE=ZXB; Connect timeout=20 ";
Query string
String Mysqlstr= "SELECT * from FY";
Connecting to database operations
SqlConnection myconnection = new SqlConnection (myconn);
Executing SQL statement Operations
SqlDataAdapter mydataadapter = new SqlDataAdapter (mysqlstr,myconnection);
Open Database
Myconnection.open ();
Populating the DataSet with data, populating the FY table in the test library in the database server
Mydataadapter.fill (myds, "FY");
Populating the Dastagrid with data
datagrid1.datasource=myds;
Datagrid1.databind ();
}
<summary>
Output to Excel
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, System.EventArgs e)
{
if (textbox1.text== "")
{
Response.Write ("<script language=javascript>");
Response.Write ("Window.alert (' please enter a filename ');";
Response.Write ("</SCRIPT>");
}
Else
{
Response.Clear ();
Response.buffer= true;
Response.charset= "GB2312"; Set the type to prevent the appearance of garbled characters
Response.appendheader ("Content-disposition", "Attachment;filename=" +textbox1.text+ ". xls"); Defining output files and file names
Response.contentencoding=system.text.encoding.getencoding ("GB2312");/set output stream to Simplified Chinese
Response.ContentType = "Application/ms-excel";//Set Output file type to excel file.
This. EnableViewState = false;
System.Globalization.CultureInfo Mycitrad = new System.Globalization.CultureInfo ("ZH-CN", true);
System.IO.StringWriter ostringwriter = new System.IO.StringWriter (Mycitrad);
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);
This. Datagrid1.rendercontrol (Ohtmltextwriter);
Response.Write (Ostringwriter.tostring ());
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.itemdatabound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this. Datagrid1_itemdatabound);
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Datagrid1_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)
{
E.item.cells[0]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:@");
E.ITEM.CELLS[3]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}

are continuing to study other ways







Related Article

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.