privatevoid button1_Click(object sender, EventArgs e) { object objApp; object objBook; object objBooks; object objSheets; object objSheet; object objRange; object objCells; object[] Parameters; try { // 获取Excel类型并建立其实例 Type objExcelType = Type.GetTypeFromProgID("Excel.Application"); if (objExcelType ==null) { return; } objApp = Activator.CreateInstance(objExcelType); if (objApp ==null) { return; } //获取Workbook集 objBooks = objApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, objApp, null); //添加一个新的Workbook objBook = objBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objBooks, null); //获取Sheet集 objSheets = objBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, objBook, null); //获取第一个Sheet对象 Parameters =new Object[1] { 1 }; objSheet = objSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, objSheets, Parameters); try { //获取操作范围for (int i =1; i <100; i++) { Parameters =new Object[2] { 1, i }; objCells = objSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, objSheet, Parameters); //向指定单元格填写内容值 Parameters =new Object[1] { "Hello, World!" }; objCells.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, objCells, Parameters); } } catch (Exception operException) { MessageBox.Show(operException.Message); } finally { //不提示保存 Parameters =new Object[1] { false }; objApp.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, objApp, Parameters); //保存文件并退出 Parameters =new Object[1] { @"D:\a.xls" }; objBook.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook, Parameters); objApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, objApp, null); GC.Collect(); } } catch (Exception theException) { String errorMessage; errorMessage ="Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } }