将.docx转换为html [英] Converting .docx to html

查看:721
本文介绍了将.docx转换为html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to convert .docx file to .html. I work in C#. My code is this:

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结果文件中.如何将它们包括在结果中?

The problem is, this doesn't include the header and the footer. They aren't in the .html result file. How can I include them in the result?

推荐答案

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

The reason you see the headers and footers in Word is because you are basically in a Print view. In an HTML document you are in a "Draft" style view where headers and footers do not exist. You can setup a different style for your HTML document for when it is printed called a print stylesheet. This print stylesheet is only used when you print the document in the browser.

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

Another option would be to just convert it to a PDF and allow the user's to view the PDF since most browsers now either support PDF viewing or have a plugin to support it.

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

You could also add the headers and footers to the html file as an element and then use some CSS trickery to make the elements display at the top and bottom. Here is a link describing how to do this.

这篇关于将.docx转换为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆