html - 更改 HTML 导出到 Word 的边距

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

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

如果我打开下载的 word 文档然后另存为 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;
}    

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

最佳答案

这是我用 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()

关于html - 更改 HTML 导出到 Word 的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8232982/

相关文章:

c# - Word 自动化 - 文件正在被另一个应用程序或用户使用

sql-server-2008 - 导出/导入MS Sql 2008地理数据的最佳方法

c# - 删除或不写入行中的最后一个 ';' 符号

ios - 在不了解代码的情况下发送应用程序

html - 为什么css中一个td标签的类里面有括号?

html - 如何让div显示:none after a css animation that makes it fade away?

html - 如何创建一个跨越 2 列和 2 行的 CSS 网格布局框?

html - 将 100% 的宽度放到 <body> 标签内的 div,该标签的最大宽度为 : 1200px

ms-word - 拼写检查应忽略 MS Word 2010 中的下划线变量名称

vba - 在 Microsoft Word 2011 for OSX 中通过 POST 发送和接收数据