Response. Clear (); // clear the output stream
Response. Buffer = true; // enable the buffer output
Response. charset = "gb2312"; // output character set (Chinese)
Response. appendheader ("content-disposition", "attachment?filename=the file name you want to save. xls ");
// If it is set to getencoding ("gb2312"); otherwise, the exported file will contain garbled characters !!!
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
This. gridview1.rendercontrol (ohtmltextwriter); // the content of the gridview is output to the stream. Other data components are similar
Response. Output. Write (ostringwriter. tostring ());
Response. Flush ();
Response. End ();
Note: In addition, you must add the following method. This is the rewrite registration method. Otherwise, an error will be reported.
Public override void verifyrenderinginserverform (Control)
{
}