C#后期绑定操作Excel

后期绑定的好处是:对于不同版本的软件都能支持。如果机器A上安装的Office 2003,机器B上安装Office 2007,没问题,下面代码都能正常运行。但是如果采用前期绑定就不行了。关于“前期绑定和后期绑定”,“Excel专业开发”一书的3.3.3节(43页)说的非常清楚。


                 private void Test()
                {
                        DataTable table = new DataTable();
                        table.Columns.Add( "ID", System.Type.GetType( "System.Int32"));
                        table.Columns.Add( "Name", System.Type.GetType( "System.String"));
                         for ( int i = 0; i < 22; i++)
                        {
                                DataRow row = table.NewRow();
                                row[ "ID"] = i;
                                row[ "Name"] = "name" + i;
                                table.Rows.Add(row);
                        }
                         try
                        {
                                 this.Cursor = Cursors.AppStarting;
                                 //例如在中文系统下安装的是英文的Office,就需要指定CultureInfo为en-US
                                ExportToExcel(table, new System.Globalization.CultureInfo( "en-US"));
                                 this.Cursor = Cursors.Default;
                        }
                         catch (Exception ex)
                        {
                                MessageBox.Show( "Exception \n" + ex.Message + "\nStack Trace: \n" + ex.StackTrace.ToString(), "信息提示",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                }

                 public void ExportToExcel(System.Data.DataTable table)
                {
                        ExportToExcel(table, System.Globalization.CultureInfo.CurrentCulture);
                }

                 public void ExportToExcel(System.Data.DataTable table, System.Globalization.CultureInfo cultureInfoOfOffice)
                {
                         object excel;
                         object book;
                         object books;
                         object sheet;
                         object sheets;
                         object range;
                         object[] parameters;
                        Type ExcelType;

                        parameters = new object[1];
                         //获取Excel类型
                        ExcelType = Type.GetTypeFromProgID( "Excel.Application");
                        excel = Activator.CreateInstance(ExcelType);
                         //获取workbooks集合
                        books = excel.GetType().InvokeMember( "Workbooks", BindingFlags.GetProperty, null, excel, null);
                         //新增workbook.
                         //BUG:自动化 Excel 时出现“格式太旧或是类型库无效”错误
                         //http://support.microsoft.com/kb/320369/zh-cn
                         //如果满足以下条件,在调用某个 Excel 方法时会收到此错误:
                         //* 该方法需要一个 LCID(区域设置标识符)。
                         //* 运行的是英语版本的 Excel。但是,计算机的区域设置是针对非英语语言配置的。
                         //如果客户端计算机运行的是英语版本的 Excel 并且当前用户的区域设置配置为英语之外的某个语言,则 Excel 将尝试查找针对所配置语言的语言包。
                         //如果没有找到所需语言包,则会报告错误。
                        book = books.GetType().InvokeMember( "Add", BindingFlags.InvokeMethod, null, books, null, cultureInfoOfOffice);
                         //获取worksheets集合
                        sheets = book.GetType().InvokeMember( "Worksheets", BindingFlags.GetProperty, null, book, null);
                         //获取第一个 worksheet.

                        parameters[0] = 1;
                        sheet = sheets.GetType().InvokeMember( "Item", BindingFlags.GetProperty, null, sheets, parameters);
                         //获取A1单元格所在区域
                         object[] header = new Object[table.Columns.Count];
                        parameters = new object[1];
                        parameters[0] = "A1:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) + "1";
                        range = sheet.GetType().InvokeMember( "Range", BindingFlags.GetProperty, null, sheet, parameters);
                         //在A1单元格中填充数据.
                        parameters = new object[1];
                         for ( int i = 0; i < table.Columns.Count; i++)
                        {
                                header[i] = table.Columns[i].ToString();
                        }
                        parameters[0] = header;
                        range.GetType().InvokeMember( "Value", BindingFlags.SetProperty, null, range, parameters, cultureInfoOfOffice);
                        parameters = new object[2];
                        parameters[0] = "A2:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) + (table.Rows.Count + 1).ToString().Trim();
                        parameters[1] = Missing.Value;
                        range = sheet.GetType().InvokeMember( "Range", BindingFlags.GetProperty, null, sheet, parameters);
                        parameters = new object[1];
                        Object[,] data = new Object[table.Rows.Count, table.Columns.Count];
                         for ( int i = 0; i < table.Rows.Count; i++)
                        {
                                 for ( int j = 0; j < table.Columns.Count; j++)
                                {
                                        data[i, j] = table.Rows[i][j].ToString();
                                }
                        }
                        parameters[0] = data;
                        range.GetType().InvokeMember( "Value", BindingFlags.SetProperty, null, range, parameters, cultureInfoOfOffice);
                        parameters = new object[1];
                         //启动 Excel
                        parameters[0] = true;
                        excel.GetType().InvokeMember( "Visible", BindingFlags.SetProperty, null, excel, parameters);
                        excel.GetType().InvokeMember( "UserControl", BindingFlags.SetProperty, null, excel, parameters);
                }

DataTable的使用技巧可参考:
http://www.cnblogs.com/dreamof/archive/2008/07/31/1257660.html

你可能感兴趣的:(职场,C#,Excel,休闲,后期绑定)