C#下如何通过不添加对Office的引用,而创建Excel对象

        添加命名空间:  using System.Reflection;

        ///


        /// 将DataGridView中的数据打印出来
        ///

        /// 接收的消息常量
        public void CmDbGridEhDataPrint(ref System.Windows.Forms.Message m)  //CM_DBGRIDEH_DATA_PRINT
        {
            object ObjApp;
            object ObjBook;
            object ObjBooks;
            object ObjSheets;
            object ObjSheet;
            object[] Arg1, Arg2, Arg3, Arg4, Arg5;
            int I, J, K;
            object Cells1;
            System.Data.DataTable MyTable = null;
            DataGridView obj;
            if (!(ActiveControl is DataGridView))
            {
                return;
            }
            else
            {
                obj = (DataGridView)ActiveControl;

               //反射取得类型
                Type ObjExcel = Type.GetTypeFromProgID("Excel.Application");
                if (ObjExcel == null)
                {
                    SysPublicTools.DJShow("请先安装Excel97/2000。", 1);
                    return;
                }
                try
                {

                     //创建Excel实例
                    ObjApp = Activator.CreateInstance(ObjExcel);

                    //设置可见属性
                    ObjApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Type.DefaultBinder, ObjApp, new object[] { "True"});
                    ObjApp.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, Type.DefaultBinder, ObjApp, new object[] { "True" });

                    //获取Workbook集
                    ObjBooks = ObjApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, Type.DefaultBinder, ObjApp, null);

                    //添加一个新的Workbook
                    ObjBook = ObjBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, Type.DefaultBinder, ObjBooks, null);

                    //获取Sheet集
                    ObjSheets = ObjBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, Type.DefaultBinder, ObjBook, null);

                    //获取第一个Sheet对象
                    Arg1 = new object[1] { 1 };
                    ObjSheet = ObjSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, Type.DefaultBinder, ObjSheets, Arg1);

                    Arg2 = new object[2] { 1, 1 };
                    Cells1 = ObjSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, Type.DefaultBinder, ObjSheet, Arg2);
                    Cells1.GetType().InvokeMember("Value2", BindingFlags.SetProperty, Type.DefaultBinder, Cells1, new Object[] { "shubozhong博客" });

                    Arg3 = new object[2] { 2, 1 };
                    Cells1 = ObjSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, Type.DefaultBinder, ObjSheet, Arg3);
                    Cells1.GetType().InvokeMember("Value2", BindingFlags.SetProperty, Type.DefaultBinder, Cells1, new Object[] { this.Text });

                    J = 1;
                    for (I = 0; I < obj.ColumnCount; I++)
                    {
                        if (!obj.Columns[I].Visible)
                        {
                            continue;
                        }
                        else
                        {
                            Arg4 = new object[2] { 3, J };
                            Cells1 = ObjSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, Type.DefaultBinder, ObjSheet, Arg4);
                            Cells1.GetType().InvokeMember("Value2", BindingFlags.SetProperty, Type.DefaultBinder, Cells1, new Object[] { obj.Columns[I].HeaderText });
                            J = J + 1;
                        }
                    }
                    if (obj.DataSource != null)
                    {
                        if (obj.DataSource is System.Data.DataTable)
                        {
                            MyTable = (System.Data.DataTable)obj.DataSource;
                        }
                        else
                        {
                            if (obj.DataSource is BindingSource)
                            {
                                BindingSource bs = (BindingSource)obj.DataSource;
                                MyTable = (System.Data.DataTable)bs.DataSource;
                            }
                        }
                    }
                    K = 4;
                    if (MyTable != null)
                    {
                        if (MyTable.Rows.Count > 0)
                        {
                            foreach (DataRow NewRow in MyTable.Rows)
                            {
                                J = 0;
                                for (I = 0; I < obj.Columns.Count; I++)
                                {
                                    J++;
                                    if (!(obj.Columns[I].Visible))
                                    {
                                        J--;
                                        continue;
                                    }
                                    else
                                    {
                                        if (MyTable.Columns[I].ToString().Length != 0)
                                        {
                                            Arg5 = new object[2] { K, J };
                                            Cells1 = ObjSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, Type.DefaultBinder, ObjSheet, Arg5);
                                            Cells1.GetType().InvokeMember("Value2", BindingFlags.SetProperty, Type.DefaultBinder, Cells1, new Object[] { NewRow.ItemArray[I].ToString() });
                                        }
                                    }
                                }
                                K = K + 1;
                            }
                        }
                    }
                    //ObjApp.GetType().InvokeMember("Quit",BindingFlags.InvokeMethod,Type.DefaultBinder,ObjApp,null);
                }
                catch (Exception err)
                {
                    SysPublicTools.DJShow("创建Excel文件失败,请检查Office软件是否已经正确安装!"+err.Message, 1);
                }
            }
        }

 

上面的代码中,关键的就是用到的  Type.GetType().InvokeMember(),这个方法对于调用是否成功,起着很重要的作用.

你可能感兴趣的:(.NET/C#)