C# and Excel Interop issue, Saving the excel file not smooth

旧城冷巷雨未停 提交于 2019-11-30 18:42:50

You need to use Workbook.SaveAs instead of Application.Save:

Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(missing);
...
wb.SaveAs(@"C:\temp\test.xlsx", missing, missing, missing, missing,
          missing, Excel.XlSaveAsAccessMode.xlExclusive,
          missing, missing, missing, missing, missing);

Setting the following properties might also help:

excelApp.DisplayAlerts = false;
excelApp.ScreenUpdating = false;
excelApp.Visible = false;
excelApp.UserControl = false;
excelApp.Interactive = false;

Well, here's how Microsoft does it:

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book1.xls", m_objOpt, m_objOpt, 
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, 
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

See one of their KB articles.

just set the ActiveWorkbook.Saved property to true and you can Quit() without any Dialog box;

ExcelApp.Interactive = false suppresses any dialog box.

excelApp.ActiveWorkbook.SaveAs(exportDirectory)

Jay

I found excelApp.ActiveWorkbook.save() which can save the file, then I can quit without for asking.

myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!