C#和Excel互操作问题,保存excel文件不流畅
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/785673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# and Excel Interop issue, Saving the excel file not smooth
提问by
I could Open and Write to the excel file, but when I try to save the file by passing a path to it, the save operation prompts with the Save dialog. I was expecting it to quitely Save the file at the specified path
我可以打开并写入 excel 文件,但是当我尝试通过传递路径来保存文件时,保存操作会提示保存对话框。我期待它完全将文件保存在指定的路径
The code is as below:
代码如下:
excelApp.Save(exportToDirectory);
excelApp.Quit();
where, exportToDirectoryis: "C:\files\strings.xlsx".
其中,exportToDirectory是:“C:\files\strings.xlsx”。
PS: I have already checked with the excel version and similar issue.
PS:我已经检查过excel版本和类似问题。
Thanks
谢谢
采纳答案by Mitch Wheat
You need to use Workbook.SaveAs
instead of Application.Save
:
您需要使用Workbook.SaveAs
代替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);
回答by C???
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.
请参阅他们的知识库文章之一。
回答by Bravax
Setting the following properties might also help:
设置以下属性也可能有帮助:
excelApp.DisplayAlerts = false;
excelApp.ScreenUpdating = false;
excelApp.Visible = false;
excelApp.UserControl = false;
excelApp.Interactive = false;
回答by Vincent Van Den Berghe
ExcelApp.Interactive = false
suppresses any dialog box.
ExcelApp.Interactive = false
抑制任何对话框。
excelApp.ActiveWorkbook.SaveAs(exportDirectory)
excelApp.ActiveWorkbook.SaveAs(exportDirectory)
回答by Jay
I found excelApp.ActiveWorkbook.save()
which can save the file, then I can quit without for asking.
我发现excelApp.ActiveWorkbook.save()
哪个可以保存文件,然后我可以不用问就退出。
回答by Gasy
just set the ActiveWorkbook.Savedpropertyto trueand you can Quit() without any Dialog box;
只需将ActiveWorkbook.Saved属性设置为true,您就可以在没有任何对话框的情况下退出();
回答by Ajay Padharia
myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();
myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();