2012-03-28 97 views
4

我有一個word文檔的文件夾,我想將其轉換爲html以供進一步處理。我只有Visual Studio 2010 Express版本。可以使用快遞版嗎?我已經找到了如何進行轉換的示例,但它們需要Microsoft.Office.Tools.Word庫,它不包含Express。使用Visual Studio 2010 Express將.doc保存/轉換爲.html

編輯:我發現它,它實際上是在稱爲Microsoft Word 12.0對象庫的COM對象中,它是Microsoft.Office.Interop.Word命名空間。

+0

它附帶辦公室。我想你可以參考VS2010 express – ivowiblo 2012-03-28 03:45:10

+0

Express版本只是一個IDE限制,而不是一個CLR限制 – Seph 2012-03-28 06:02:26

回答

9

你應該可以使用快遞版本。我修改了this question的答案。適應的代碼如下。您需要添加一個對Microsoft.Office.Interop.Word的引用才能工作。如果您缺少這個庫,請查看this article on MSDN

看着WdSaveFormat你也可以將它保存爲格式過濾的HTML(wdFormatFilteredHTML)。

namespace Sample { 
    using Microsoft.Office.Interop.Word; 
    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Linq; 
    using System.Text; 

    class Program { 

     public static void Main() 
     { 
      Convert("C:\\Documents", WdSaveFormat.wdFormatHTML); 
     } 

     private static void Convert(string path, WdSaveFormat format) 
     { 

      DirectoryInfo dirInfo = new DirectoryInfo(path); 
      FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 
      if (wordFiles.Length == 0) { 
       return; 
      } 

      object oMissing = System.Reflection.Missing.Value; 
      Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 
      try { 
       word.Visible = false; 
       word.ScreenUpdating = false; 
       foreach (FileInfo wordFile in wordFiles) { 
        Object filename = (Object)wordFile.FullName; 
        Document doc = word.Documents.Open(ref filename, 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, ref oMissing); 
        try { 
         doc.Activate(); 
         object outputFileName = wordFile.FullName.Replace(".doc", ".html"); 
         object fileFormat = format; 
         doc.SaveAs(ref outputFileName, 
            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 oMissing, ref oMissing, ref oMissing); 

        } 
        finally { 
         object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
         ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
         doc = null; 
        } 
       } 

      } 
      finally { 
       ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
       word = null; 
      } 
     } 
    } 
} 
+0

謝謝。我需要自動保存爲篩選過的HTML。得到它工作感謝你。 – Rhyous 2014-03-05 16:50:37

+0

任何想法爲什麼一些文本(撇號)被轉換爲一些問號符號?如果我在Word中將文檔另存爲html,則不會生成這些符號。 – 2014-09-09 02:07:15

+0

我已經看到,當UTF8保存爲ASCII時會發生這種情況。 – bloudraak 2014-09-09 02:43:39

相關問題