// Note: if you use the gridview to export files on the visual studio2005 platform,
// The verifyrenderinginserverform method must be reloaded.
Public override void verifyrenderinginserverform (Control)
{
}
/// <Summary>
/// Method for exporting data to a file,
/// </Summary>
/// <Param name = "model"> model = 1: Export to execl, model = 2: Export to word </param>
Private void tofiles (INT Model)
{
String strfilename = datetime. Now. tostring ("yyyymmdd-hhmmss ");
System. Web. httpcontext Hc = system. Web. httpcontext. Current;
HC. response. Clear ();
HC. response. Buffer = true;
HC. response. contentencoding = system. Text. encoding. utf8; // set the output stream to simplified Chinese
If (model = 1)
{
// --- Export as an Excel file
HC. response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (strfilename, system. Text. encoding. utf8) + ". xls ");
HC. response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.
}
Else
{
// --- Export as a Word file
HC. response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (strfilename, system. Text. encoding. utf8) + ". Doc ");
HC. response. contenttype = "application/MS-word"; // set the output file type to word.
}
System. Io. stringwriter Sw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );
This. gridview1.rendercontrol (HTW );
HC. response. Write (SW. tostring ());
HC. response. End ();
}
//-Export as an Excel file
Protected void toexecl_click (Object sender, eventargs E)
{
Tofiles (1 );
}
//-Export as a Word file
Protected void button#click (Object sender, eventargs E)
{
Tofiles (2 );
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bl_song/archive/2009/07/13/4344895.aspx