更改HTML导出到Word的边距

时间:2011-11-22 19:59:51

标签: html ms-word export margin

实际出口没问题:

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();

所以我的问题是:如何更改生成的单词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;
}    

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

3 个答案:

答案 0 :(得分:3)

以下是我在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()

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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

在“开发者”标签下找到该选项 - &gt;文件模板 - &gt;链接的CSS。

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