Skip to main content
 首页 » 编程设计

c#之将 HTML 转换为 Word 文档

2024年02月13日12pengyingh

如何将页面 HTML 转换为 word 文档。例如,我有一个网页,我需要将其保存在 word 文档中。如何将 HTML 页面转换为 Word 文档。以及如何在word文档中插入分页符

请您参考如下方法:

您可以在下面找到将 html 转换为 word 的示例。

它运行良好,我已经在 MS Office 2013 和 Open Office 上对其进行了测试。

注意:对于分页符,请在行下方插入

<br clear=all style='mso-special-character:line-break;page-break-before:always'>  

例子:

Dim StrLocLetterText As StringBuilder = New StringBuilder() 
Dim ObjLocHtml1 As String = "<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:word"" xmlns=""http://www.w3.org/TR/REC-html40""><head></head><body>" 
Dim ObjLocHtml2 As String = "</body></html>" 
 
StrLocLetterText.Append(ObjLocHtml1) 
StrLocLetterText.Append(<Content within body tag>) 
StrLocLetterText.Append(ObjLocHtml2) 
HttpContext.Current.Response.Clear() 
HttpContext.Current.Response.Charset = "" 
HttpContext.Current.Response.ContentType = "application/msword" 
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + <FileName> + ".doc") 
HttpContext.Current.Response.Write(StrLocLetterText) 
HttpContext.Current.Response.End() 
HttpContext.Current.Response.Flush()