2010-09-12 8 views
5

Ho una grande quantità di documenti di Word che devo analizzare. Poiché tutti sono stati creati dallo stesso modello, penso che l'approccio migliore sarebbe salvarli come file HTML e analizzare l'HTML stesso.Salvataggio di più documenti di Word in formato HTML tramite l'API di Office

Mentre è abbastanza semplice salvare un singolo documento di Word come HTML, non ho trovato un modo per eseguire una procedura di massa da Word. Pertanto, sto cercando di trovare un modo per sfruttare l'API di Microsoft Office/Word per realizzare questo.

Come posso utilizzare l'API di Word per salvare molti documenti di Word in formato HTML?

Grazie in anticipo.

UPDATE: qualche dettaglio in più ...

Alcuni dei documenti sono di estensione .doc, mentre altri sono .docx. Spero che questo non sia un problema, ma se lo è, dovrò semplicemente convertirli tutti in .docx, si spera con l'API o con DocX.

Parlando di DOCX, ho saw on the author's blog che è possibile salvare un file .docx come HTML con il seguente codice:

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); 
     } 
    } 
} 

È questo il modo migliore per farlo?

risposta

4

Il codice che hai postato sopra dovrebbe fare il lavoro per te. Anche per quanto ne so Document.SaveAs Api può convertire qualsiasi documento (docx, doc, rtf) che può aprire in formato HTML (o qualsiasi altro formato)

invece di creare un'istanza di applicazione parola per ogni file passare la stringa [] dei nomi alla conversione API e solo disporre l'istanza del documento una volta terminato con salvataggio come

+0

D'accordo, questo è il modo per farlo e il punto di Vinay sulla creazione di una singola istanza è azzeccato. –