如何在 ASP.NET MVC 中即时将 HTML 转换为 Word 文档?
How do I convert HTML to a Word document on the fly in ASP.NET MVC?
我有一个 HTML 字符串,我想将其转换为 word 文档并在单击按钮时下载。
我知道如何下载文件,所以这不是问题的一部分 - 只是一些上下文。
除了实现我自己的 OpenXML 解决方案之外,我一直无法找到执行此操作的任何库或代码示例。
我可以将它下载为 .rtf 文件,这很好 - 但特别是 .docx 格式给我带来了麻烦。
为了构建 .doc 文件(支持 html),您需要格式化 html 以包含 office 理解的指标,然后您需要编写响应格式。
这种方法的灵感来自于 vb http://www.codeproject.com/Articles/7341/Dynamically-generate-a-MS-Word-document-using-HTML 中代码项目的 post,并且大部分是逐字逐句但翻译成 c#
//build the content for the dynamic Word document
//in HTML alongwith some Office specific style properties.
var strBody = new StringBuilder();
strBody.Append("<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>Time</title>");
//The setting specifies document's view after it is downloaded as Print
//instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" +
"<xml>" +
"<w:WordDocument>" +
"<w:View>Print</w:View>" +
"<w:Zoom>90</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/>" +
"</w:WordDocument>" +
"</xml>" +
"<![endif]-->");
strBody.Append("<style>" +
"<!-- /* Style Definitions */" +
"@page Section1" +
" {size:8.5in 11.0in; " +
" margin:1.0in 1.25in 1.0in 1.25in ; " +
" mso-header-margin:.5in; " +
" mso-footer-margin:.5in; mso-paper-source:0;}" +
" div.Section1" +
" {page:Section1;}" +
"-->" +
"</style></head>");
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
"<div class=Section1>");
strBody.Append(yourCustomHTMLString);
strBody.Append("</div></body></html>");
//Force this content to be downloaded
//as a Word document with the name of your choice
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename=myword.doc");
Response.Write(strBody.ToString());
我有一个 HTML 字符串,我想将其转换为 word 文档并在单击按钮时下载。
我知道如何下载文件,所以这不是问题的一部分 - 只是一些上下文。
除了实现我自己的 OpenXML 解决方案之外,我一直无法找到执行此操作的任何库或代码示例。
我可以将它下载为 .rtf 文件,这很好 - 但特别是 .docx 格式给我带来了麻烦。
为了构建 .doc 文件(支持 html),您需要格式化 html 以包含 office 理解的指标,然后您需要编写响应格式。
这种方法的灵感来自于 vb http://www.codeproject.com/Articles/7341/Dynamically-generate-a-MS-Word-document-using-HTML 中代码项目的 post,并且大部分是逐字逐句但翻译成 c#
//build the content for the dynamic Word document
//in HTML alongwith some Office specific style properties.
var strBody = new StringBuilder();
strBody.Append("<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>Time</title>");
//The setting specifies document's view after it is downloaded as Print
//instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" +
"<xml>" +
"<w:WordDocument>" +
"<w:View>Print</w:View>" +
"<w:Zoom>90</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/>" +
"</w:WordDocument>" +
"</xml>" +
"<![endif]-->");
strBody.Append("<style>" +
"<!-- /* Style Definitions */" +
"@page Section1" +
" {size:8.5in 11.0in; " +
" margin:1.0in 1.25in 1.0in 1.25in ; " +
" mso-header-margin:.5in; " +
" mso-footer-margin:.5in; mso-paper-source:0;}" +
" div.Section1" +
" {page:Section1;}" +
"-->" +
"</style></head>");
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
"<div class=Section1>");
strBody.Append(yourCustomHTMLString);
strBody.Append("</div></body></html>");
//Force this content to be downloaded
//as a Word document with the name of your choice
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename=myword.doc");
Response.Write(strBody.ToString());