Output ASP.NET page to Excel
First, define document type, character encoding
Response.Clear (); response.buffer = true; response.charset = "UTF-8"; // This trip is important, Attachment parameter represents as an attachment download, you can change to Online to open // 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 Application / MS-Excel Application / MS-Word Application / MS-TXT Application / MS-HTML or other browsing 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 (OstringWriter);
Third, bind the target data to the input stream output
This.RenderControl (OHTMLTextWriter); // this represents the output this page, you can also bind DataGrid, or other control response.write (ostringWriter.toString ()); response.end ();
Summary: This routine in Microsoft Visual Studio .NET
The test is passed under the 2003 platform, suitable for C # and VB, when using VB, THIS