2012-10-21 29 views
2

是否可以從特定文檔獲取設計屬性值?如果是這樣,我會用什麼方法,以及如何?如何從特定的Word或Excel文檔獲取設計屬性值?

實施例:

我有file.docx,那麼file.docx具有帶有字體宋體,大小74文本和風格的粗體和斜體。現在,我想要將屬性的值設置爲設置字體的位置,大小和樣式(注意:值,而不是屬性)。如果這是可能的,是否也會用於樣式表等?

回答

0

對於Word文檔:

using Microsoft.Office.Interop.Word; 

     namespace ConsoleApplication1 
     { 
      class Program 
      { 
       static void Main(string[] args) 
       { 

        Application application = new Application(); 
        application.Visible = false; 


        Document document = application.Documents.Open(@"C:\file.docx", Type.Missing, true); 

        // Loop through all words in the document. 
        int count = document.Words.Count; 
        for (int i = 1; i <= count; i++) 
        { 
         string text = document.Words[i].Text; //you can validate if string.IsNullOrEmpty.... 
         string fontName = document.Words[i].Font.Name; 
         string bold = document.Words[i].Font.Bold.ToString(); 
         string fontSize = document.Words[i].Font.Size.ToString(); 
         Console.WriteLine("Word {0} = {1} -- Font:{2}, Size: {3}, Bold:{4}", i, text, fontName, fontSize, bold); 
        } 


        application.Quit(); 

        Console.ReadLine(); 

        } 
      } 
     } 

Excel文檔:

using Microsoft.Office.Interop.Excel; 
using System.Runtime.InteropServices; 

... 

    static void Main(string[] args) 
      { 

       Application application = new Application(); 
       application.Visible = false; 
       string filePath = @"C:\excel.xlsx"; 

       var book = application.Workbooks.Open(filePath, Type.Missing, true); 

       Workbook workBook = application.Workbooks.Open(filePath, 
                   Type.Missing, true, Type.Missing, Type.Missing, 
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                   Type.Missing, Type.Missing); 



       foreach (Worksheet item in workBook.Sheets) 
       { 
        foreach (Range cell in item.Cells) 
        { 
         //Navigate huge options... 
         //.Borders 
         //.Style 
         //... 
        } 

       } 



       workBook.Close(false, filePath, null); 
       Marshal.ReleaseComObject(workBook); 

       application.Quit(); 

       Console.ReadLine(); 


      } 
+0

嗨,先生,謝謝你的回答。這是樣式表相同嗎?如果我不會使用單詞怎麼樣?我會從「設計」模板中獲取設計,然後從中放入我的內容? –

+0

嗨。我爲excel添加了一個示例。它的設計模板的方式非常廣泛和多樣。此示例將允許您打開文檔和循環元素...但找到您需要的內容我邀請您調查http://msdn.microsoft.com/en-us/library/ms262200.aspx和http:// msdn.microsoft.com/en-us/library/office/microsoft.office.interop.word(v=office.14).aspx – Mate

+0

請記住,如果您開發桌面應用程序並使用客戶端的辦公室DLL,您可能並不都具有相同的版本。如果可能的話,爲這個過程使用一個服務器...如果你想避免頭痛。答案是你可以得到所有的數據,要求縮小你正在查看文檔的變量 – Mate

相關問題