首页 > 代码库 > Excel中自定义属性
Excel中自定义属性
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 = http://www.mamicode.com/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 = http://www.mamicode.com/new Func<string, string>(s => { string strValue = http://www.mamicode.com/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 =http://www.mamicode.com/ getPropertyValue(pName); if (string.IsNullOrEmpty(pValue)) { break; } else { strResult += pValue; } } return strResult; }
注掉的代码,在多线程操作中会出错
Excel中自定义属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。