将.docx转换为html

时间:2011-11-15 11:50:04

标签: 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 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结果文件中。如何将它们包含在结果中?

1 个答案:

答案 0 :(得分:2)

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

另一种选择是将其转换为PDF并允许用户查看PDF,因为现在大多数浏览器都支持PDF查看或者有一个插件来支持它。

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