【问题标题】:Saving multiple Word documents as HTML through Office API通过 Office API 将多个 Word 文档保存为 HTML
【发布时间】:2011-04-11 06:44:27
【问题描述】:

我有大量需要解析的 Word 文档。由于它们都是从同一个模板创建的,我认为最好的方法是将它们保存为 HTML 文件并解析 HTML 本身。

虽然将单个 Word 文档另存为 HTML 非常容易,但我还没有找到从 Word 内部执行批量处理的方法。因此,我试图找到一种利用 Microsoft Office/Word API 来完成此任务的方法。

如何使用 Word API 将多个 Word 文档保存为 HTML?

提前致谢。

更新:更多细节...

一些文件的扩展名是.doc,而另一些文件的扩展名是.docx。我希望这不是问题,但如果是,我只需将它们全部转换为 .docx,希望使用 API 或 DocX。

说到 DocX,我 saw on the author's blog 可以使用以下代码将 .docx 文件保存为 HTML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Convert Input.docx into Output.doc
            Convert(@"C:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.doc", WdSaveFormat.wdFormatDocument);

            /*
             * Convert Input.docx into Output.pdf
             * Please note: You must have the Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS installed
             * http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en
             */
            Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.pdf", WdSaveFormat.wdFormatPDF);

            // Convert Input.docx into Output.html
            Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.html", WdSaveFormat.wdFormatHTML);
        }

        // Convert a Word 2008 .docx to Word 2003 .doc
        public static void Convert(string input, string output, WdSaveFormat format)
        {
            // Create an instance of Word.exe
            Word._Application oWord = new Word.Application();

            // Make this instance of word invisible (Can still see it in the taskmgr).
            oWord.Visible = false;

            // Interop requires objects.
            object oMissing = System.Reflection.Missing.Value;
            object isVisible = true;
            object readOnly = false;
            object oInput = input;
            object oOutput = output;
            object oFormat = format;

            // Load a document into our instance of word.exe
            Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            // Make this document the active document.
            oDoc.Activate();

            // Save this document in Word 2003 format.
            oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            // Always close Word.exe.
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
    }
}

这是最好的方法吗?

【问题讨论】:

    标签: c# api ms-word ms-office office-2007


    【解决方案1】:

    您在上面发布的代码应该可以为您完成这项工作。另外据我所知,Document.SaveAs Api 可以将任何可以用 word 打开的文档(docx、doc、rtf)转换为 HTML(或任何其他格式)

    也不是为每个文件创建一个 word 应用程序实例,而是将名称的字符串 [] 传递给转换 api,并且只有在完成另存为后才释放文档实例

    【讨论】:

    • 同意,这是这样做的方法,Vinay 关于只创建单个实例的观点是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多