In fact there are a lot of examples of Excel on the Web, but not very good, their code is not very full, read is very obscure. After these days of groping, finally can finish I want to export the report Excel effect. Here's My.
A. Front page map
Contents of the first page of the GridView
The contents of the second page of the GridView:
You may encounter this situation, that is, when you export Excel, when you open Excel, the content of the second page of the GridView is not exported to the Excel table. In fact, to solve such a situation, very easy, just before the export, the GridView Settings page set to Flase can be.
Here are all the contents of the GridView I exported, open the Excel table for example the following:
This will allow you to export all of the contents of the GridView, including the first and second pages of the GridView.
Second, the implementation of the Code
1. Front-Desk Code:
<div style= "margin:20px;" >
<asp:gridview id= "Gvrecord" runat= "Server" allowpaging= "True" cellpadding= "3"
autogeneratecolumns= "False" bordercolor= "white"
Borderstyle= "Ridge" borderwidth= "2px" backcolor= "White" cellspacing= "1"
Gridlines= "None" onprerender= "Gvrecord_prerender"
Onpageindexchanged= "Gvrecord_pageindexchanged"
onpageindexchanging= "Gvrecord_pageindexchanging" >
<pagersettings firstpagetext= "Home page" lastpagetext= "last Page"
nextpagetext= "Next" previouspagetext= "Previous"/>
<rowstyle forecolor= "Black" backcolor= "#E5F1FF" horizontalalign= "Center"/>
<Columns>
<asp:templatefield headertext= "Extension" >
<ItemTemplate>
<asp:label id= "Label1" runat= "server" text= ' <%# Bind ("Extn")%> ' ></asp:Label>
</ItemTemplate>
<itemstyle width= "200px"/>
</asp:TemplateField>
<asp:templatefield headertext= "Wake up Time" >
<ItemTemplate>
<asp:label id= "Label2" runat= "server" text= ' <%# Bind ("Calltime")%> ' ></asp:Label>
</ItemTemplate>
<itemstyle width= "300px"/>
</asp:TemplateField>
<asp:templatefield headertext= "Status" >
<ItemTemplate>
<asp:label id= "Label3" runat= "server" text= ' <%# getresult (Eval ("Status"). ToString ())%> ' ></asp:Label>
</ItemTemplate>
<itemstyle width= "250px"/>
</asp:TemplateField>
<asp:templatefield headertext= "CALL number" >
<ItemTemplate>
<asp:label id= "Label4" runat= "server" text= ' <%# Bind ("Callcount")%> ' ></asp:Label>
</ItemTemplate>
<itemstyle width= "150px"/>
</asp:TemplateField>
</Columns>
<footerstyle backcolor= "#C6C3C6" forecolor= "Black"/>
<pagerstyle backcolor= "#C6C3C6" forecolor= "Black" horizontalalign= "right"/>
<selectedrowstyle backcolor= "#9471DE" font-bold= "True" forecolor= "white"/>
</asp:GridView>
<p>
<asp:button id= "Btnexcel" cssclass= "button" runat= "server" text= "statements
"
onclick= "Btnexcel_click"/></p>
</div>
2. Background code such as the following:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using Model;
Using DAL;
Using System.Collections.Generic;
Using System.IO;
public partial class _3c_callmanager_callrecord:system.web.ui.page
{
int selectflag = 0;
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Bind ();
}
}
public string GetResult (String str)
{
if (str = = "0")
return "Haven ' t called";
if (str = = "1")
return "Check-out";
if (str = = "2")
Return "Success";
if (str = = "3")
return "fail";
if (str = = "4")
return "Dealing";
if (str = = "5")
Return "Artificial wake";
Else
return "Unkown";
}
public void bind () {
Selectflag = 0;
Gvrecord.datasource = Vgcallservice.getinfo ();
Gvrecord.databind ();
}
protected void Btnexcel_click (object sender, EventArgs e)
{
DateTime dt = System.DateTime.Now;
String str = dt. ToString ("Yyyymmddhhmmss");
str = str + ". xls";
gvrecord.allowpaging = false;
if (Selectflag = = 0)
Bind ();
if (Selectflag = = 1)
Selectbind ();
Gridviewtoexcel (Gvrecord, "Application/ms-excel", str);
Export (Gvrecord, "Application/ms-excel", str);
}
<summary>
Export grid data to Excel
</summary>
<param name= "Ctrl" > grid name (e.g. GridView1) </param>
<param name= "FileType" > File type to export (Excel:application/ms-excel) </param>
<param name= "FileName" > file name to save </param>
public static void Gridviewtoexcel (Control ctrl, String FileType, String FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = system.text.encoding.utf8;//Note coding
HttpContext.Current.Response.AppendHeader ("Content-disposition",
"Attachment;filename=" + httputility.urlencode (filename, System.Text.Encoding.UTF8). ToString ());
HttpContext.Current.Response.ContentType = Filetype;//image/jpeg;text/html;image/gif;vnd.ms-excel/msword
Ctrl. Page.enableviewstate = false;
StringWriter tw = new StringWriter ();
HtmlTextWriter HW = new HtmlTextWriter (TW);
Ctrl. RenderControl (HW);
HttpContext.Current.Response.Write (TW. ToString ());
HttpContext.Current.Response.End ();
}
<summary>
ReLoad this verifyrenderinginserverform is neccessary
</summary>
<param name= "Control" ></param>
public override void Verifyrenderinginserverform (Control control)
{
}
protected void Gvrecord_prerender (object sender, EventArgs e)
{
if (selectflag==0)
Bind ();
}
protected void Gvrecord_pageindexchanged (object sender, EventArgs e)
{
}
protected void Gvrecord_pageindexchanging (object sender, Gridviewpageeventargs e)
{
This.gvRecord.PageIndex = E.newpageindex;
Bind ();
}
}
Note the problem description:
1) front page <%@ pages language= "C #" enableeventvalidation= "false" autoeventwireup= "true" codefile= " CallRecord.aspx.cs "inherits=" _3c_callmanager_callrecord "%> Note add enableeventvalidation=" false ", Otherwise you will get an error.
2) The code of the GridView binding database
Gvrecord.datasource = Vgcallservice.getinfo ();//vgcallservice.getinfo () is a collection of databases, and I'm encapsulated, making different adjustments based on the collection you get.
Gvrecord.databind ();
3) Click on the key code of the next page of the GridView, in the Pageindexchanging event
This.gvRecord.PageIndex = E.newpageindex; Note that this must not be less.
Bind ();
4) In the Export button event of Excel, remember to gvrecord.allowpaging = false; After canceling the page of the GridView, call the Gridviewtoexcel method.
5) The following must not be less.
public override void Verifyrenderinginserverform (Control control)
{
}
GridView Export Excel Sample Example