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

                   }
            }
        }

エクセル文書:

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 に答える