Http://www.cnblogs.com/caizinet/archive/2005/05/11/152908.html
Recently, I found this code on the csdn forum. I can export the page to excel like ASP, so that I can export some complicated Excel pages, instead of complicated templates!
Public void outputexcel ()
{
// Define the document type and character encoding
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
// The following line is very important. The attachment parameter indicates downloading as an attachment. You can change it to online.
// Filename=fileflow.xls specifies the name of the output file. Note that the extension is consistent with the specified file type. It can be. Doc. xls. txt. htm.
Response. appendheader ("content-disposition", "attachment?filename=fileflow.xls ");
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
// Response. contenttype specifies the file type which can be application/MS-Excel, application/MS-word, application/MS-txt, application/MS-HTML, or other browsers can directly support documents.
Response. contenttype = "application/MS-excel ";
This. enableviewstate = false;
// Define an input stream
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
This. rendercontrol (ohtmltextwriter );
// This indicates that the current page is output. You can also bind the DataGrid or other controls that support the obj. rendercontrol () attribute.
Response. Write (ostringwriter. tostring ());
Response. End ();
}
After calling page_load, You can output the Asp.net page to excel.