C#与Excel互操作问题,保存excel文件不流畅
我可以打开并写入 Excel 文件,但是当我尝试通过传递路径来保存文件时,保存操作会提示“保存”对话框。 我期望它能够完全将文件保存在指定路径下,
代码如下:
excelApp.Save(exportToDirectory);
excelApp.Quit();
其中,exportToDirectory 是:“C:\files\strings.xlsx”。
PS:我已经检查过excel版本和类似问题。
谢谢
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
The code is as below:
excelApp.Save(exportToDirectory);
excelApp.Quit();
where, exportToDirectory is: "C:\files\strings.xlsx".
PS: I have already checked with the excel version and similar issue.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要使用
Workbook.SaveAs
而不是Application.Save
:You need to use
Workbook.SaveAs
instead ofApplication.Save
:设置以下属性也可能有帮助:
Setting the following properties might also help:
只需将ActiveWorkbook.Saved属性设置为true,您就可以在没有任何对话框的情况下退出();
just set the ActiveWorkbook.Saved property to true and you can Quit() without any Dialog box;
好吧,Microsoft 是这样做的:
请参阅他们的一篇知识库文章。
Well, here's how Microsoft does it:
See one of their KB articles.
ExcelApp.Interactive = false
禁止显示任何对话框。excelApp.ActiveWorkbook.SaveAs(exportDirectory)
ExcelApp.Interactive = false
suppresses any dialog box.excelApp.ActiveWorkbook.SaveAs(exportDirectory)
我发现 excelApp.ActiveWorkbook.save() 可以保存文件,然后我可以退出而无需询问。
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();
myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();