Step 1: First set up a basic aspx page default. aspx. We'll just start and end with the key code:
The code is as follows: |
Copy code |
<Form id = "form1" runat = "server"> <Div> <Asp: GridView ID = "GridView1" runat = "server"> </Asp: GridView> </Div> <Br/> <Asp: Button ID = "BtnExport" runat = "server" OnClick = "BtnExport_Click" Text = "Export to Excel"/> </Form> |
Step 2: Copy the following code in the corresponding default. aspx. cs:
The code is as follows: |
Copy code |
Protected void Page_Load (object sender, EventArgs e) { If (! Page. IsPostBack) { BindData (); } } Private void BindData () { String query = "SELECT * from mers "; SqlConnection myConnection = new SqlConnection (ConnectionString ); SqlDataAdapter ad = new SqlDataAdapter (query, myConnection ); DataSet ds = new DataSet (); Ad. Fill (ds, "MERS "); GridView1.DataSource = ds; GridView1.DataBind (); } Public override void VerifyRenderingInServerForm (Control control Control) { // Confirms that an HtmlForm control is rendered } Protected void button#click (object sender, EventArgs e) { Response. Clear (); Response. AddHeader ("content-disposition", "attachment?filename=filename.xls "); Response. Charset = "gb2312 "; Response. ContentType = "application/vnd.xls "; System. IO. StringWriter stringWrite = new System. IO. StringWriter (); System. Web. UI. HtmlTextWriter htmlWrite = new HtmlTextWriter (stringWrite ); GridView1.AllowPaging = false; BindData (); GridView1.RenderControl (htmlWrite ); Response. Write (stringWrite. ToString ()); Response. End (); GridView1.AllowPaging = true; BindData (); } Protected void paging (object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e. NewPageIndex; BindData (); }
|
In the above code, we first bind the gridview to the specified data source, and then define the relevant code in the event of button1 (that is, used to control the export. Here, we use the filenamein Response. AddHeader ("content-disposition", "attachment; filename = exporttoexcel.xls" parameter to specify the excelfile name to be imported, and call exporttoexcel.xls. Note that the content of the gridview may be displayed by page. Therefore, you must set the allowpaging attribute of the gridview to false each time you export an excel file, then, export the content of the current page's gridview to excel through the page stream, and then reset its allowpaging attribute. In addition, you need to write an empty VerifyRenderingInServerForm method (which must be written) to confirm that the HtmlForm control is displayed for the specified ASP. NET server control at runtime.
Next, we will introduce a method to export the content from the GridView to the Excel file. Let's first look at a UDF:
The code is as follows: |
Copy code |
Public static void ToExcel (System. Web. UI. Control ctl, string FileName) { HttpContext. Current. Response. Charset = "UTF-8 "; HttpContext. Current. Response. ContentEncoding = System. Text. Encoding. Default; HttpContext. Current. Response. ContentType = "application/ms-excel "; HttpContext. Current. Response. AppendHeader ("Content-Disposition", "attachment; filename =" + "" + FileName + ". xls "); Ctl. Page. EnableViewState = false; System. IO. StringWriter tw = new System. IO. StringWriter (); HtmlTextWriter hw = new HtmlTextWriter (tw ); Ctl. RenderControl (hw ); HttpContext. Current. Response. Write (tw. ToString ()); HttpContext. Current. Response. End (); } // Do not forget the following lines of code; otherwise, the import will fail! Public override void VerifyRenderingInServerForm (Control control Control) { // Confirms that an HtmlForm control is rendered } |
Usage: ToExcel (GVStaff, TextBox1.Text );
Excel export error
In the past, using DataGrid to export data to excel only requires the current code:
The code is as follows: |
Copy code |
Response. Clear (); Response. AddHeader ("content-disposition", "attachment?filename=filename.xls "); Response. Charset = ""; Response. ContentType = "application/vnd.xls "; System. IO. StringWriter stringWrite = new System. IO. StringWriter (); System. Web. UI. HtmlTextWriter htmlWrite = new HtmlTextWriter (stringWrite ); GvMaster. RenderControl (htmlWrite ); Response. Write (stringWrite. ToString ()); Response. End (); |
However, after using the gridview, the above code always prompts an error. After a good discussion, I finally found out the problem. The following is a solution:
1. Reload in the export interface:
The code is as follows: |
Copy code |
Public override void VerifyRenderingInServerForm (Control control Control) { // Base. VerifyRenderingInServerForm (control ); } |
2. Modify the following configuration in web. config:
The code is as follows: |
Copy code |
<Pages enableEventValidation = "false"> </pages> |
Or modify the header definition of the ASPX page:
The code is as follows: |
Copy code |
<% @ Page Language = "C #" EnableEventValidation = "false" AutoEventWireup = "true" CodeFile = "ExportGridView. aspx. cs" Inherits = "ExportGridView" %> |
Now, try again. You can export the content in the Gridview to Excel. You can try again.