C# 动态调用com
来源:互联网 发布:pdf 编辑软件 免费 编辑:程序博客网 时间:2024/05/20 13:18
class Excel { object excel; Type excelType; public Excel() { excel = Activator.CreateInstance(Type.GetTypeFromProgID("excel.application")); excelType = excel.GetType(); excelType.InvokeMember("Visible", BindingFlags.SetProperty, null, excel, new object[] { true }); } public void Open(string fileName) { InvokeMethod(excel, "Application.Workbooks.Open", new object[] { fileName }); } public void SaveAs(string fileName) { InvokeMethod(excel, "Application.Workbooks[1].SaveAs", fileName, XlFileFormat.xlWorkbookDefault ); } public object GetProperty(object obj,string property) { string[] ps = property.Split('.'); var reg = new System.Text.RegularExpressions.Regex(@"([0-9a-zA-Z-]+)(?:\[([0-9]+)\])?"); for (int i = 0; i < ps.Length; i++) { var mat=reg.Match(ps[i]); if (mat.Success == false) throw new Exception("属性错误"); obj= obj.GetType().InvokeMember(mat.Groups[1].Value, BindingFlags.GetProperty, null, obj, null); if (mat.Groups[2].Success) { obj = obj.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, obj, new object[] { int.Parse(mat.Groups[2].Value) }); } } return obj; } public object SetProperty(object obj, string property,object value) { string[] ps = property.Split('.'); var reg = new System.Text.RegularExpressions.Regex(@"([0-9a-zA-Z-]+)(?:\[([0-9]+)\])?"); for (int i = 0; i < ps.Length-1; i++) { var mat = reg.Match(ps[i]); if (mat.Success == false) throw new Exception("属性错误"); string p = mat.Groups[1].Value; obj = obj.GetType().InvokeMember(mat.Groups[1].Value, BindingFlags.GetProperty, null, obj, null); if (mat.Groups[2].Success) { obj = obj.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, obj, new object[] { int.Parse(mat.Groups[2].Value) }); } } var m = reg.Match(ps[ps.Length-1]); if (m.Success == false) throw new Exception("属性错误"); obj = obj.GetType().InvokeMember(m.Groups[1].Value, BindingFlags.SetProperty, null, obj, new object[] { value}); return obj; } public object InvokeMethod(object obj, string property,params object[] value) { string[] ps = property.Split('.'); var reg = new System.Text.RegularExpressions.Regex(@"([0-9a-zA-Z-]+)(?:\[([0-9]+)\])?"); for (int i = 0; i < ps.Length - 1; i++) { var mat = reg.Match(ps[i]); if (mat.Success == false) throw new Exception("属性错误"); string p = mat.Groups[1].Value; obj = obj.GetType().InvokeMember(mat.Groups[1].Value, BindingFlags.GetProperty, null, obj, null); if (mat.Groups[2].Success) { obj = obj.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, obj, new object[] { int.Parse(mat.Groups[2].Value) }); } } var m = reg.Match(ps[ps.Length - 1]); if (m.Success == false) throw new Exception("属性错误"); obj = obj.GetType().InvokeMember(m.Groups[1].Value, BindingFlags.InvokeMethod, null, obj, value ); return obj; } public void Quit() { excelType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, excel, null); } enum XlFileFormat { xlAddIn = 18//Excel 2007 加载项 , xlCSV = 6//CSV, xlCSVMac = 22//Macintosh CSV, xlCSVMSDOS = 24//MSDOS CSV, xlCSVWindows = 23//Windows CSV, xlCurrentPlatformText = -4158//当前平台文本, xlDBF2 = 7//DBF2, xlDBF3 = 8//DBF3, xlDBF4 = 11//DBF4, xlDIF = 9//DIF, xlExcel12 = 50//Excel 12, xlExcel2 = 16//Excel 2, xlExcel2FarEast = 27//Excel2 FarEast, xlExcel3 = 29//Excel3, xlExcel4 = 33//Excel4, xlExcel4Workbook = 35//Excel4 工作簿, xlExcel5 = 39//Excel5, xlExcel7 = 39//Excel7, xlExcel8 = 56//Excel8, xlExcel9795 = 43//Excel9795, xlHtml = 44//HTML 格式, xlIntlAddIn = 26//国际加载项, xlIntlMacro = 25//国际宏, xlOpenDocumentSpreadsheet = 60//OpenDocument 电子表格, xlOpenXMLAddIn = 55//打开 XML 加载项, xlOpenXMLStrictWorkbook = 61// (&H3D)Strict Open XML 文件, xlOpenXMLTemplate = 54//打开 XML 模板, xlOpenXMLTemplateMacroEnabled = 53//打开启用的 XML 模板宏, xlOpenXMLWorkbook = 51//打开 XML 工作簿, xlOpenXMLWorkbookMacroEnabled = 52//打开启用的 XML 工作簿宏, xlSYLK = 2//SYLK, xlTemplate = 17//模板, xlTemplate8 = 17//模板 8, xlTextMac = 19//Macintosh 文本, xlTextMSDOS = 21//MSDOS 文本, xlTextPrinter = 36//打印机文本, xlTextWindows = 20//Windows 文本, xlUnicodeText = 42//Unicode 文本, xlWebArchive = 45//Web 档案, xlWJ2WD1 = 14//WJ2WD1, xlWJ3 = 40//WJ3, xlWJ3FJ3 = 41//WJ3FJ3, xlWK1 = 5//WK1, xlWK1ALL = 31//WK1ALL, xlWK1FMT = 30//WK1FMT, xlWK3 = 15//WK3, xlWK3FM3 = 32//WK3FM3, xlWK4 = 38//WK4, xlWKS = 4//工作表, xlWorkbookDefault = 51//默认工作簿, xlWorkbookNormal = -4143//常规工作簿, xlWorks2FarEast = 28//Works2 FarEast, xlWQ1 = 34//WQ1, xlXMLSpreadsheet = 46//XML 电子表格 } }
由于C#版本兼容问题 写了这个方法方便以后使用
阅读全文
0 0
- C# 动态调用com
- C# 调用 Delphi 动态库 和 Com 接口
- C#调用COM组件
- c#如何调用com
- C#调用ATL COM
- C#调用ATL COM
- 在C#中调用COM/COM+
- c# 中调用COM组件
- C#中调用Com组建
- C#调用C++COM组件
- C++调用C# COM组件
- C++调用C#的COM
- C++调用C# COM组件
- C++调用C# com组件
- C++调用C# COM组件
- C#中调用COM对象
- c#动态加载com组件
- 基于COM组件技术C++调用C#的动态链接库
- #display:block、inline、inline-block的区别#
- 安卓,调用系统方法分享到微信
- 贪吃蛇无敌版 1.0
- JS数组和对象的一些原生方法(常用API)
- 谈谈射频电路设计及经验
- C# 动态调用com
- java项目生成可执行文件(exe)
- 【PostgreSQL-9.6.3】LOG: unrecognized configuration parameter "dynamic_shared_memory_type"
- 图解LSTM神经网络架构及其11种变体(附论文)
- 测试maven镜像是否可用
- HDU 6000 优先队列+快速IO
- 将wordpress admin bar中用户登出后跳转至登陆页面修改为登出前页面
- 1018:尼科彻斯定理
- scikit-learn Tutorials-KNN