protected void Page_Load(object sender, EventArgs e) { string fileName = "路政许可事项办理流转情况汇总统计" + ".doc"; OutPutToWord("UTF-8", fileName, this.Page); } public void OutPutToWord(string OutEncode, string OutFileName, Page page) { try { page.Response.Clear(); page.Response.Buffer = true; page.Response.Charset = OutEncode; page.Response.ContentEncoding =System.Text.Encoding.Default; //page.Response.ContentEncoding = System.Text.Encoding.GetEncoding(OutEncode); page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(OutFileName, Encoding.UTF8)); page.Response.ContentType = "application/ms-winword"; StringWriter oStringWriter = new StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter); page.RenderControl(oHtmlTextWriter); page.Response.Write(oStringWriter.ToString()); page.Response.End(); } catch { page.ClientScript.RegisterStartupScript(page.GetType(), "onClick", "<script>alert('数据导出到Word失败!')</script>", true); } }
<html xmlns:x="urn:schemas-microsoft-com:office:office"> <head id="Head1" runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>路政许可事项办理流转情况汇总统计</x:Name> <x:WorksheetOptions> <x:Print> <x:ValidPrinterInfo /> </x:Print> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml> <title>路政许可事项办理流转情况汇总统计</title> <style type="text/css"> .style1 { color: windowtext; font-size: 16pt; font-weight: 700; font-style: normal; text-decoration: none; font-family: 黑体; text-align: center; vertical-align: middle; white-space: normal; border: .5pt solid windowtext; padding-left: 1px; padding-right: 1px; padding-top: 1px; } </style> </head> <body> <form id="form1" runat="server"> <table align="center" width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td align="center" style="font-size:20px; font-family:楷体">路政许可事项办理流转情况汇总统计</td></tr> <tr><td align="center" style="font-size:30px; font-family:楷体; color:Red;">路政许可事项办理流转情况汇总统计</td></tr> </table> <table align="center" border="1" cellpadding="0" cellspacing="0"> <tr><td style="height:35px; width="100px" align="center">111111</td><td style="width:500px" align="center">许可事项办</td></tr> </table> </form> </body> </html>