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