解决方案:由于删除sheet会弹出一个是否删除的对话框,因此需要将该对话框去掉。
C#
//建立一个Excel.Application的新进程 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Workbooks workbooks = app.Workbooks; string syspath = string.Empty; syspath = System.Web.HttpContext.Current.Server.MapPath("\\Export\\kaoqin_tmp.xlsx"); _Workbook workbook = workbooks.Add(syspath); Sheets sheets = workbook.Worksheets; _Worksheet Sheet2 = (_Worksheet)sheets[2]; //删除sheet的方法放在 app.DisplayAlerts之间 app.DisplayAlerts = false; Sheet2.Delete();//删除sheet的方法 app.DisplayAlerts = true;
代码: