在Office自动化条件下读文档属性

在Office Add-in下我们读取CustomDocumentProperties应该是没有问题的。 可是你会发现在Office Automation条件下CustomDocumentProperties是无法直接读取的(至少在我的环境下:Visual Studio 2010, Office 2010是无法实现的)。所以我们必须使用反射来“曲线救国”。以下代码演示了读取CustomDocumentProperties的方法。在Book1.xlsx中我设置了一个自定义属性TestFlag(布尔值)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;

namespace ConsoleApplication14
{
    class Program
    {
        private static ILog log = log4net.LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            string test = @"C:\*****\Book1.xlsx";
            Excel.Application xlApp = null;
            Process[] ps = Process.GetProcessesByName("EXCEL");
            if (ps.Length > 0)
            {
                xlApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application;
            }
            else
            {
                xlApp = new Excel.Application();
            }
            xlApp.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(xlApp_WorkbookOpen);
            xlApp.WorkbookActivate += new Excel.AppEvents_WorkbookActivateEventHandler(xlApp_WorkbookActivate);
            xlApp.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(xlApp_WindowActivate);
            xlApp.SheetActivate += new Excel.AppEvents_SheetActivateEventHandler(xlApp_SheetActivate);
            log.Info("Before Workbook open");
            Excel.Workbook Wb = xlApp.Workbooks.Open(test);
            log.Info("After Workbook open");
            log.Info(Dump(Wb));
            xlApp.Visible = true;
            Console.ReadKey();
            xlApp.Quit();
        }

        static void xlApp_SheetActivate(object Sh)
        {
            log.Info("SheetActive event");
        }

        static void xlApp_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
        {
            log.Info("WN:" + Dump(Wb).ToString());
            log.Info("WindowActive event");
        }

        static void xlApp_WorkbookActivate(Excel.Workbook Wb)
        {
            log.Info("WA:" + Dump(Wb).ToString());
            log.Info("WorkbookActive event");
        }

        static void xlApp_WorkbookOpen(Excel.Workbook Wb)
        {
            log.Info("WO:" + Dump(Wb).ToString());
            log.Info("WorkbookOpen event");
        }

        static bool Dump(Excel.Workbook Wb)
        {
            bool result = false;
            try
            {
                var dps = Wb.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.Default | BindingFlags.GetProperty, null, Wb, null);
                var property = dps.GetType().InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, dps, new object[] { "TestFlag" });
                object propertyValue = property.GetType().InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, property, null);
                result = (bool)propertyValue;
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
            }
            return result;
        }
    }
}



欢迎访问《许阳的红泥屋


欢迎访问《许阳的红泥屋
posted @ 2012-10-23 13:29  许阳 无锡  阅读(183)  评论(0编辑  收藏  举报