//关闭事件 private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel) { //This is called when you choose to close the workbook in Excel. //The event handlers are removed, and then the workbook is closed // 是否保存 //Wb.Saved = true; //事件移除 try { if (!Wb.Saved) { //switch (MessageBox.Show("你还没保存,是否要保存?", "提醒", MessageBoxButtons.YesNoCancel)) //{ // case DialogResult.Yes: //foreach (Excel.Worksheet sheet in wb.Worksheets) //{ // sheet.SelectionChange -= EventDel_SelectionChange; // sheet.Change -= EventDel_CellsChange; //} //app.WorkbookBeforeClose -= EventDel_BeforeBookClose; //Wb.SaveAs(); //Wb.Close(); ////关闭进程 //// Close(); //Close(app); // break; // case DialogResult.No: // foreach (Excel.Worksheet sheet in wb.Worksheets) // { // sheet.SelectionChange -= EventDel_SelectionChange; // sheet.Change -= EventDel_CellsChange; // } // app.WorkbookBeforeClose -= EventDel_BeforeBookClose; // Wb.Close(); // //关闭进程 // // Close(); // Close(app); // break; // case DialogResult.Cancel: // Cancel = true; // break; // default: break; //} } else { ExcelUtils.Close(app); } } catch (Exception ex) { ConfigUtils.Error("Error: " + ex.Message); ExcelUtils.Close(app); } //xlSheet1.Change -= EventDel_CellsChange; //xlSheet2.Change -= EventDel_CellsChange; //xlSheet3.Change -= EventDel_CellsChange; }
//关闭进程 public void Close() { if (app != null) { try { if (wb.Saved) { wb.Close(); app.Quit(); ExcelUtils.Close(app); } else { switch (MessageBox.Show("你还没保存,是否要保存?", "提醒", MessageBoxButtons.YesNo)) { case DialogResult.Yes: try { wb.SaveAs(); } catch (System.Exception ex) { ConfigUtils.Error(ex.Message, ex.StackTrace); if (ex.InnerException != null) { ConfigUtils.Error(ex.InnerException.Message, ex.InnerException.StackTrace); } // MessageBox.Show("Error: " + ex.Message); } finally { wb.Close(); app.Quit(); ExcelUtils.Close(app); } break; case DialogResult.No: wb.Close(); app.Quit(); ExcelUtils.Close(app); break; } } } catch (Exception ex) { ConfigUtils.Error(ex.Message, ex.StackTrace); MessageBox.Show("Error: " + ex.Message); } } }