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 the specified file type, which 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 directly supported for Application / MS-Excel Application / MS-Word Application / MS-TXT Application / MS-HTML or other browsers
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;
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.