Many times, you must export the ASP.NET web page to Excel or Word, especially the export of DataGrid content. In general, there are two main methods. One is to operate Excel or Word in a similar VBA using JScript or VBScript operations with JScript or VBScript. Another is to operate VBA objects with C # or VB.NET, which is very similar to the client, but this solution is rarely adopted because of a large consumption server resources, usually with HTTP's header, set a few critical in header Word makes IE know what type is, which is turned on correctly. The following detailed description:
Solution 1: Use contentType = "Application / OCTET-stream", "content-disposition" = "attachment; filename = ...", indicating that this is a file to download, then output the file stream to response
Response.contentType = "Application / OcTet-stream"
Response.Appendheader ("Content-Disposition", "Attachment; FileName =" & httputility.urlencode ("BILL.DOC", System.Text.Encoding.default))
Response.writefile (Bill.doc)
Solution 2: Use contenttype = "Application / Msword", "Content-Disposition" = "Attachment; FileName = ..."
HttpContext.current.response.Appendheader ("Content-Disposition", "Attachment; FileName = Excel.txt");
HttpContext.current.response.charset = "GB2312";
HttpContext.current.response.contentencoding = system.text.Encoding.default;
HttpContext.current.response.contenttype = "Application / MS-Excel"; // image / jpeg; text / html; image / gif; VND.MS-Excel / Msword
// httpContext.current.response.contenttype = "Application / Microsoft Excel";
// ctl.page.enableviewState = false;
System.io.stringWriter TW = new system.io.stringwriter ();
System.Web.ui.htmlTextWriter HW = New System.Web.ui.htmlTextWriter (TW);
CTL.RenderControl (HW);
HttpContext.current.Response.write (Tw.toString ());
HttpContext.current.Response.end ();
CTL.visible = false;