0

我在 Web 应用程序中动态生成带有 html 的 word 文档。这很好用,但是我的文档在 Web 布局中打开。我确定我在某处读到了一种使生成的文档在打印布局中打开的方法,但我现在在任何地方都找不到它。

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=ContentDocument.doc");

    StringBuilder htmlCode = new StringBuilder();
    htmlCode.Append("<html>");
    htmlCode.Append("<head><style type=\"text/css\">body {font-family:arial;font-size:14.5;}</style></head>");
    htmlCode.Append("<body>");

    ... populate htmlCode ...

    htmlCode.Append("</body></html>");
    HttpContext.Current.Response.Write(htmlCode.ToString());
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();

我认为这可能是为标题添加特定内容的情况。有谁知道如何让生成的文档在打印布局中打开?

4

1 回答 1

2

您可以通过以下代码在打印布局中打开文档

        string strBody = string.Empty;
        strBody = @"<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'>";

        strBody = strBody + "<!--[if gte mso 9]>" +
        "<xml>" +
        "<w:WordDocument>" +
        "<w:View>Print</w:View>" +
        "<w:Zoom>100</w:Zoom>" +
        "</w:WordDocument>" +
        "</xml>" +
        "<![endif]-->";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
    HttpContext.Current.Response.AddHeader("Content-Disposition",    "inline;filename=ContentDocument.doc");

    StringBuilder htmlCode = new StringBuilder();
    htmlCode.Append("<html>");
    htmlCode.Append("<head>"+strBody+" <style type=\"text/css\">body {font-family:arial;font-size:14.5;}</style></head>");
    htmlCode.Append("<body>");

     ... populate htmlCode ...

    htmlCode.Append("</body></html>");
    HttpContext.Current.Response.Write(htmlCode.ToString());
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();
于 2013-04-04T09:44:25.050 回答