Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过Office API将多个Word文档保存为HTML_C#_Api_Ms Word_Ms Office_Office 2007 - Fatal编程技术网

C# 通过Office API将多个Word文档保存为HTML

C# 通过Office API将多个Word文档保存为HTML,c#,api,ms-word,ms-office,office-2007,C#,Api,Ms Word,Ms Office,Office 2007,我有大量的Word文档需要解析。由于它们都是从同一个模板创建的,我认为最好的方法是将它们保存为HTML文件并解析HTML本身 虽然将单个Word文档保存为HTML非常容易,但我还没有找到从Word内部执行批量过程的方法。因此,我试图找到一种方法来利用MicrosoftOffice/Word API来实现这一点 如何使用Word API将许多Word文档保存为HTML? 提前谢谢 更新:更多详细信息 一些文档的扩展名为.doc,而其他文档的扩展名为.docx。我希望这不是一个问题,但如果是的话,我

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

虽然将单个Word文档保存为HTML非常容易,但我还没有找到从Word内部执行批量过程的方法。因此,我试图找到一种方法来利用MicrosoftOffice/Word API来实现这一点

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

提前谢谢

更新:更多详细信息

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

说到DocX,我认为可以使用以下代码将
.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);
        }
    }
}

这是最好的方法吗?

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


另外,不要为每个文件创建word应用程序实例,而是将名称字符串[]传递给convert api,并在完成“另存为”后仅处理文档实例。同意,这是一种方法,Vinay关于只创建一个实例的观点非常正确。