Private Void Btnmime_click ( Object Sender, system. eventargs E)
{
Dgshow. allowpaging = False ;
Binddata ();
Response. contenttype = "Application/vnd. MS-excel" ;
Response. addheader ( "Content-disposition" , "Inline; filename ="
+ Httputility. urlencode ( "Download file .xls" , Encoding. utf8 ));
// If the output is word, modify it to the followingCode
// Response. contenttype = "application/MS-word"
// Response. addheader ("content-disposition", "inline?filename=test.doc ")
Stringbuilder sb = new stringbuilder ();
System. Io. stringwriter Sw = new system. Io. stringwriter (SB );
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (SW );
SB. append ("");
Dgshow. rendercontrol (HW );
SB. append ("");
Response. Write (sb. tostring ());
Response. End ();
Dgshow. allowpaging =True;
Binddata ();
}
Note: 1. If a button Column exists in the DataGrid, hide it before export.
2. If the DataGrid has pagination and all data needs to be printed, the pagination should be canceled first.
Supplement:
If you bind the new control gridview to the data in vs. net2005, the code above will prompt an exception error. The solution is as follows:
1. Reload in the export interface
Public Override VoidVerifyrenderinginserverform (Control)
{
// Base. verifyrenderinginserverform (control );
}
2. Modify in Web. config
"False"> Or "C #" enableeventvalidation = "False" Autoeventwireup = "True"
Codefile ="Exportgridview. aspx. cs"Inherits ="Exportgridview"%>