Recently, when developing the ISO file management system, I have encountered the need to output ASPX directly to Excel, and I will share my experience with you.
In fact, it is easy to use the ASP.NET output of Word, Excel, TXT, HTM and other types of documents such as the specified content. It is mainly divided into three steps.
First, define document type, character encoding
Response.clear (); response.buffer = true; response.charset = "UTF-8";
// This is very important, the attachment parameter represents the download as an attachment, you can change it to ONLINE
//filename=FileFlow.xls Specifies the name of the output file, pay attention to its extension and specified file type, can be: .doc || .xls ||.txt || .htm
Response.Appendheader ("Content-Disposition", "Attachment; FileName = fileflow.xls"); response.contentencoding = system.text.encoding.Getencoding ("UTF-8");
//Response.contentType Specify file types can be Application / MS-Excel || Application / MS-Word || Application / MS-TXT || Application / MS-HTML || or other browser to support documentation directly
Response.contentType = "Application / MS-Excel"; this.enableViewState = false;
Second, define an input stream
System.io.stringWriter ostringwriter = new system.io.stringWriter (); system.Web.ui.htmlTextWriter OhtmlTextWriter = new system.web.ui.htmlTextWriter (OstringWriter);
Third, bind the target data to the input stream output
THIS.RenderControl (OhtmlTextWriter);
// this represents the output page, you can also bind DataGrid, or other controls that support Obj.RenderControl () properties
Response.write (ostringwriter.tostring ()); response.end ();
Summary: This routine is tested under the Microsoft Visual Studio .Net 2003 platform, for C # and VB, change the THIS keyword to ME when using VB.