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 important, the attachment parameter represents the download as an attachment, you can change to Online to open //filename=FileFlow.xls Specify the name of the output file, note that its extension is compliant, 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 Specified file type can be Application / MS-Excel Application / MS-Word Application / MS-TXT Application / MS-HTML or other browser can directly support document 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; stringwriter
Third, bind the target data to the input stream output this.RenderControl (OHTMLTextWriter; // this means outputting this page, you can also bind DataGrid, or other control response.write (OstringWriter) (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.