2

是否可以从特定文档中获取设计属性值?如果是这样,我将使用什么方法,以及如何使用?

例子:

我有 file.docx,然后那个 file.docx 有一个 Tahoma 字体的文本,大小为 74,样式为粗体和斜体。现在,我想获取设置字体、大小和样式的属性值(注意:值,而不是属性)。如果这是可能的,它是否也适用于样式表等?

4

1 回答 1

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


            }
于 2012-10-21T07:49:44.730 回答