我正在尝试替换我的文件,但它给了我警告:
该文件已存在。要替换文件
吗?
在代码中,我不想要警告
代码:
String fromFile = @"\\test.net\excel\123.xls";
String toFile = @"\\test.net\excel\123.xls";
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(fromFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
File.Delete(toFile);
wb.SaveAs(toFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, Type.Missing);
wb.Close(false, Type.Missing, Type.Missing);
app.Quit();
答案 0 :(得分:0)
添加
app.DisplayAlerts = false;
在SaveAs
来电之前并添加
app.DisplayAlerts = true;
在Close
事件之后。