How to bind data to the gridview and convert it to excel

Source: Internet
Author: User

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data. SqlClient;
Using System. Data;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String sqlconn = "Server =.; DataBase = db; Integrated Security = SSPI ";
String SQL = "select top 10 * from table ";

SqlConnection myConnection = new SqlConnection (sqlconn); // create a database connection instance

MyConnection. Open (); // Open the database

SqlCommand myCommand = new SqlCommand (SQL, myConnection); // create an SQL instance and execute an SQL


SqlDataAdapter Adapter = new SqlDataAdapter (); // create an SQL data Adapter
Adapter. SelectCommand = myCommand; // The property is set to retrieve records from the data source.


DataSet myDs = new DataSet (); // create a DataSet instance
Adapter. Fill (myDs); // Fill the dataset

GridView1.DataSource = myDs. Tables [0]. DefaultView ;//
GridView1.DataBind ();

// DataToExcel ("tested cxcel", GridView1 );
MyConnection. Close (); // Close the database connection
}
Public void DataToExcel (string fileName, GridView myGridView)
{
// Define the document type and character encoding
Response. Clear ();
Response. Buffer = false;
// Response. Charset = "UTF-8 ";
Response. Charset = "GB2312 ";
// The following line is very important. The attachment parameter indicates downloading as an attachment. You can change it to online.
// Filename=FileFlow.xls specifies the name of the output file. Note that the extension is consistent with the specified file type. It can be:. doc |. xls |. txt |. htm
Response. appendHeader ("Content-Disposition", "attachment; filename =" + System. web. httpUtility. urlEncode (fileName, System. text. encoding. UTF8) + ". xls ");
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8 ");
// Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
// Response. contentType: the specified file type can be application/ms-excel | application/ms-word | application/ms-txt | application/ms-html | or can be directly supported by other browsers document
Response. ContentType = "application/ms-excel ";
This. EnableViewState = false;
// System. Globalization. Fig ("ZH-CN", true );
// Define an input stream
System. IO. StringWriter oStringWriter = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );
// Bind the target data to the input stream output
MyGridView. RenderControl (oHtmlTextWriter );
Response. Write (oStringWriter. ToString ());
Response. End ();
}

// The following empty statement must be added; otherwise, "must be placed in the form tag with runat = server" appears ." Error
Public override void VerifyRenderingInServerForm (Control control Control)
{
}

// Click an event to generate an excel file
Protected void button#click (object sender, EventArgs e)
{
DataToExcel ("tested cxcel", GridView1 );
}
}

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.