C# 动态调用com

 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值