如何保存/覆盖现有的Excel文件使用Excel互操作 - C# [英] How to Save/Overwrite existing Excel file with Excel Interop - C#
问题描述
有没有办法挽救通过Excel互操作(我增加了工作表给它在这种情况下),而无需它提示用户是否要覆盖与修改现有的文件更改到Excel电子表格。我不希望用户甚至可以看到电子表格在我的应用程序有这么一个消息框弹出询问他们是否要覆盖该文件似乎很不合时宜,并可能混淆用户开放。
我使用的是workbook.SaveAs(fileloaction)方法。
下面是我在哪里初始化为EXCEL互操作的COM参考对象。
私人Excel.Application应用= NULL;
私人Excel.Workbook工作簿= NULL;
公共Excel.Workbook工作簿
{
{返回工作簿; }
集合{=工作簿的价值; }
}
私人Excel.Worksheet工作表= NULL;
私人Excel.Range workSheet_range = NULL;
下面是我使用的关闭/保存Excel文件中的代码。该workbook.close()方法行是据说抛出未处理的异常之一。
workbook.Close(真实的,startForm.excelFileLocation,Missing.Value);
对Marshal.ReleaseComObject(应用程序);
应用= NULL;
System.GC.Collect();
基本上,所有你需要的是的 ExcelApp。 DisplayAlerts =假 - 这里是我如何做到这一点,虽然:
ExcelApp.DisplayAlerts =假
ExcelWorkbook .Close(的SaveChanges:= TRUE,文件名:= CURDIR&安培; FileToSave)
希望这有助于
Is there a way to save changes to an excel spreadsheet through the excel interop (in this case I am adding a worksheet to it) without having it prompt the user if they want to overwrite the existing file with the changes. I do not want the user to even see the spreadsheet open in my application so having a message box popping up asking them if they want to overwrite the file seems very out of place and possibly confusing to the user.
I am using the workbook.SaveAs(fileloaction) method.
Here is where I am initializing the COM reference objects for the excel interop.
private Excel.Application app = null;
private Excel.Workbook workbook = null;
public Excel.Workbook Workbook
{
get { return workbook; }
set { workbook = value; }
}
private Excel.Worksheet worksheet = null;
private Excel.Range workSheet_range = null;
Below is the code I am using to close/save the excel file. The workbook.close() method line is the one that is reportedly throwing the unhandled exception.
workbook.Close(true, startForm.excelFileLocation, Missing.Value);
Marshal.ReleaseComObject(app);
app = null;
System.GC.Collect();
Basically, all you need is ExcelApp.DisplayAlerts = False - Here's how I do it, though:
ExcelApp.DisplayAlerts = False
ExcelWorkbook.Close(SaveChanges:=True, Filename:=CurDir & FileToSave)
Hope this helps
这篇关于如何保存/覆盖现有的Excel文件使用Excel互操作 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!