C# 将.docx转换为html

C# 将.docx转换为html,c#,html,ms-word,C#,Html,Ms Word,我想将.docx文件转换为.html。我在C#工作。我的代码是: Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document(); Object o

我想将.docx文件转换为.html。我在C#工作。我的代码是:

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
        Object oMissing = System.Reflection.Missing.Value;
        wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        word.Visible = false;
        Object filepath = @"C:\Users\John\Desktop\begin.docx";
        Object confirmconversion = System.Reflection.Missing.Value;
        Object readOnly = false;
        Object saveto = @"C:\Users\John\Desktop\result.html";
        Object oallowsubstitution = System.Reflection.Missing.Value;

        wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                      ref oMissing, ref oMissing, ref oMissing);
        object fileFormat = WdSaveFormat.wdFormatHTML;
        wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
                       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                       ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
                       ref oMissing);

问题是,这不包括页眉和页脚。它们不在.html结果文件中。如何将它们包含在结果中?

您在Word中看到页眉和页脚的原因是因为您基本上处于打印视图中。在HTML文档中,您处于“草稿”样式视图中,其中不存在页眉和页脚。在打印HTML文档时,可以为其设置不同的样式,称为。此打印样式表仅在浏览器中打印文档时使用

另一个选择是将其转换为PDF,并允许用户的浏览器查看PDF,因为大多数浏览器现在要么支持PDF查看,要么有一个插件支持它


您还可以将页眉和页脚作为元素添加到html文件中,然后使用一些CSS技巧使元素显示在顶部和底部。描述如何执行此操作。

您是否尝试过手动执行此操作?我的意思是从Word保存到HTML。它包括页眉和页脚吗?是的,我试过了,但没有。一个(商业)库是一个选项吗?是的,但我已经试过很多。问题是Word文档由页面(带有页眉和页脚)组成,HTML文件只是一个页面,没有页眉/页脚的位置。