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.
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);
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);
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.