【问题标题】:Change Margins on HTML export to Word将 HTML 导出到 Word 的边距更改
【发布时间】:2012-01-04 04:50:16
【问题描述】:

实际导出没问题:

Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/msword";
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Content-Type", "application/word");
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.doc", docName));
Response.Charset = "utf-8";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
mainbody.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

所以我的问题是:如何更改生成的 word doc 的边距?

如果我打开下载的 word doc 然后另存为 HTML,定义边距的指令是:

@page WordSection1
{
  size:8.5in 11.0in;
  margin:1.0in 1.0in 1.0in 1.0in;
  mso-header-margin:.5in;
  mso-footer-margin:.5in;
  mso-paper-source:0;
}    

我想知道如何在响应标头中更改它,以便可以在一张纸上正确输出表单。有什么想法吗?

【问题讨论】:

  • 这个运气好吗?我们正在生成一个包含图像的 html 文档,并且设置了右边距,因此隐藏了图像和文本。它实际上是表格列。
  • 学习如何自己编写 HTML 比尝试让软件为你编写 HTML 更容易。

标签: html ms-word export margin


【解决方案1】:

这是我在 VB.NET 而不是 C# 中解决这个问题的方法。我收到了code from here

divContent.RenderControl(New HtmlTextWriter(New IO.StringWriter(sb)))

memoBody = sb.ToString

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" & fileName & "")
Response.ContentType = "application/msword"
Dim sbResponseString As New StringBuilder
Dim wordHeader as String = "<html xmlns:o=""urn:schemas-microsoft-com:office:office"" "
wordHeader &= "xmlns:w=""urn:schemas-microsoft-com:office:word"" "
wordHeader &= "xmlns=""http://www.w3.org/TR/REC-html40""> "
wordHeader &= "<head><title>Document Title</title>"
wordHeader &= "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>"
wordHeader &= "<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->"
wordHeader &= "<style> @page Section1 {size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; "
wordHeader &= "border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; "
wordHeader &= "margin:0.75in 0.50in 0.75in 0.50in ; mso-header-margin:.5in; "
wordHeader &= "mso-footer-margin:.5in; mso-paper-source:0;} "
wordHeader &= "div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, "
wordHeader &= "div.MsoFooter{margin:0in; margin-bottom:.0001pt; "
wordHeader &= "mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; "
wordHeader &= "font-size:12.0pt; font-family:'Arial';} "
wordHeader &= "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; "
wordHeader &= "margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center "
wordHeader &= "3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}--></style></head> "

Dim wordBody as String = "<body><div class=Section1>" & memoBody & "</div></body></html>"

sbResponseString.Append(wordHeader)
sbResponseString.Append(wordBody)

Dim newResponseString As String = sbResponseString.ToString
Response.Write(newResponseString)
Response.End()

【讨论】:

    【解决方案2】:

    如果您的情况允许,发出一个 docx 文件可能会更容易,该文件基本上是几个压缩的 XML 文件,因为这将允许您控制页面布局方面(显然还有许多其他内容)。

    然后,您可以使用 altChunk 元素将 HTML 的其余部分嵌入到 docx 文件的一部分中。

    参考:http://www.codeproject.com/Articles/32907/HTML-to-WordML

    您可能喜欢的工作更多,但您将获得对输出文档的各种控制。

    【讨论】:

      【解决方案3】:

      我发现在我的案例中,最简单的解决方案是使用 Word 2013,将 CSS 链接到保存的 HTML。这让我可以控制边距以及我可能想要调整的任何其他内容。

      在“开发者”选项卡 -> 文档模板 -> 链接 CSS 下找到该选项。

      信用:https://superuser.com/questions/65107/how-to-apply-external-css-stylesheet-to-document-in-microsoft-word/65144#65144

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-09
        • 2011-01-05
        • 2013-08-10
        • 2022-11-14
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        • 2014-03-29
        相关资源
        最近更新 更多