在按钮上提供Word文档单击C#asp.net页面

时间:2011-08-30 09:25:22

标签: c# asp.net events html-to-text

当代码放在onClick事件上时,它不会显示打开的保存对话框,并且不会抛出任何异常,但在onLoad事件上正常工作,打开一个打开的保存对话框来保存word文件..

string strDocBody;
strDocBody = "<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head>" + "<title>Version and Release Management</title>";

strDocBody = strDocBody + "<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:Zoom>100</w:Zoom>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->";

strDocBody = strDocBody + "<style> @page" + "{size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt;" + " margin:0.75in 0.50in 0.75in 0.50in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "-->" + "</style>" + "</head>";

strDocBody = strDocBody + "<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + "<h1>This is my Heading</h1>" + "<h2>This is my Sub Heading</h2>" + "<p style='color:navy;'> This is blue text</p>" + "<p style='font-weight:bold; color:green;'><u> This is green bold underlined text </u></p>" + "<p style='color:red'><I>" + DateTime.Now + "</I></p>" + "<!--[if supportFields]>" + "<div style='mso-element:header' id=h1><p class=MsoHeader><span style='mso-tab-count:4'></span><span style='mso-field-code: PAGE '></span> </p></div>" + "<div style='mso-element:footer' id=f1> " + "<p class=MsoFooter style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'><o:p> </o:p></p> " + "Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'>1</span></span> of <span style='mso-field-code: NUMPAGES '></span>" + " <span style='mso-tab-count: 12'> <span style='mso-field-code: DATE '></span> " + " </p></div><![endif]-->" + "</div> </body> </html> ";

//Force this content to be downloaded as a Word document
Response.AddHeader("Content-Type", "application/msword");
Response.AddHeader("Content-disposition", "attachment; filename=TEST.doc");
Response.Charset = "";
Response.Write(strDocBody);

3 个答案:

答案 0 :(得分:0)

根据我的经验,这确实不适用于onClick,因为标题已经发送。

答案 1 :(得分:0)

这可能是因为标题已经发布到客户端浏览器。尝试使用Response.Clear()清除,确保您不在Ajax调用中。另一个技巧是使用动态编译的参数的客户端锚点打开一个新页面。

编辑:当然,如果您清除响应,请求用户下载文件后页面将为空白。始终牢记客户端 - 服务器http通信的无状态同步行为。

答案 2 :(得分:0)

我想您正在尝试将strDocBody中的数据作为Word文件提供给用户。为此,此数据必须是Web服务器传递给浏览器的唯一内容。这就是为什么当你把它放在你的OnLoad事件处理程序中时它工作正常。当你把它放在按钮点击处理程序中时,情况并非如此。

如果您想在点击按钮时出现此行为,则此按钮必须将用户重定向到另一个URL,该URL将发送文档数据,而不是其他任何内容。

专门为服务文档创建一个页面,让它为 Document.aspx 。 现在,我不知道您是否需要在按钮点击时执行任何其他逻辑。如果没有,您可以使用LinkBut​​ton:

<asp:LinkButton runat="server" ID="docLink" Text="Document" 
                PostBackUrl="Document.aspx" />

否则,只需在onClick处理程序中添加重定向调用:

protected void btnButton1_Click(object sender, EventArgs e)
{
    // ....
    Response.Redirect("Document.aspx");
}