码迷,mamicode.com
首页 > 其他好文 > 详细

Excel中自定义属性

时间:2014-11-16 23:01:19      阅读:741      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   div   on   

public static string GetCustomProperty(this Excel.Workbook book, string propertyName)
        {
            //string strResult = string.Empty;
            //Office.DocumentProperties pros = (Office.DocumentProperties)book.CustomDocumentProperties;
            //strResult = GetCustomProperty(pros, propertyName);
            //if (!string.IsNullOrEmpty(strResult))
            //{
            //    return strResult;
            //}
            //int index = 1;
            //while (true)
            //{
            //    string pName = propertyName + index++.ToString();
            //    string pValue = GetCustomProperty(pros, pName);
            //    if (string.IsNullOrEmpty(pValue))
            //    {
            //        break;
            //    }
            //    else
            //    {
            //        strResult += pValue;
            //    }
            //}
            //return strResult;
            string strResult = string.Empty;

            var bookType = book.GetType();
            var pros = bookType.InvokeMember("CustomDocumentProperties", System.Reflection.BindingFlags.GetProperty, null, book, null);
            var propertiesType = pros.GetType();

            var enumerator = (IEnumerator)propertiesType.InvokeMember("GetEnumerator", System.Reflection.BindingFlags.InvokeMethod, null, pros, null);
            Func<string, string> getPropertyValue = new Func<string, string>(s =>
            {
                string strValue = string.Empty;
                enumerator.Reset();
                while (enumerator.MoveNext())
                {
                    var p = enumerator.Current;
                    var name = p.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, p, null);
                    if (name != null && name.ToString().Equals(propertyName))
                    {
                        return (string)p.GetType().InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, p, null);
                    }
                }
                return strValue;
            });

            strResult = getPropertyValue(propertyName);
            if (!string.IsNullOrEmpty(strResult))
            {
                return strResult;
            }
            int index = 1;
            while (true)
            {
                string pName = propertyName + index++.ToString();
                string pValue = getPropertyValue(pName);
                if (string.IsNullOrEmpty(pValue))
                {
                    break;
                }
                else
                {
                    strResult += pValue;
                }
            }
            return strResult;
        }


注掉的代码,在多线程操作中会出错

 

Excel中自定义属性

标签:style   blog   io   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/willes/p/4102585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!