ASP. NET export data to Excel

Source: Internet
Author: User

 

This method only saves the asp.net page as an html page. It only changes the suffix to xlc, but it can be read in excel. Next, let's take a look at other ways to export data and generate it using templates.

Below is the code

Create an asp. ne tweb application in the county and paste the code.

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"> file name: </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 on 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 of 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 the page
If (! Page. IsPostBack)
{
Data_Load (); // call method Filling Table
}

}
/// <Summary>
/// Create a data source
/// </Summary>
/// <Returns> DataView </returns>
Private void Data_Load ()
{
// The database connection string Catalog is the specified database name, And DataSource is the name of the SQL Server 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 ";
// Database connection operations
SqlConnection myConnection = new SqlConnection (myConn );
// Execute SQL statement operations
SqlDataAdapter myDataAdapter = new SqlDataAdapter (mySQLstr, myConnection );
// Open the database
MyConnection. Open ();
// Fill data in DataSet and the fy table in the test database on the database server
MyDataAdapter. Fill (myDS, "fy ");
// Fill data with DastaGrid
DataGrid1.DataSource = myDS;
DataGrid1.DataBind ();
}
/// <Summary>
/// Output to Excel
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, System. EventArgs e)
{
If (TextBox1.Text = "")
{
Response. Write ("<SCRIPT language = javascript> ");
Response. Write ("window. alert (enter the file name );");
Response. Write ("</SCRIPT> ");
}
Else
{
Response. Clear ();
Response. Buffer = true;
Response. Charset = "GB2312"; // set the type to Chinese to prevent garbled characters.
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + TextBox1.Text + ". xls"); // define the output file and file name
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312"); // set the output stream to simplified Chinese
Response. ContentType = "application/ms-excel"; // set the output file type to an 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 by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Maid + = new System. Web. UI. WebControls. Maid (this. Maid );
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void maid (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 ");
}
}
}
}

Other methods are being studied.

<% @ 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>

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.