如何在不收到警告的情况下存储Excel文件

时间:2015-04-09 19:20:30

标签: c# asp.net

我正在尝试替换我的文件,但它给了我警告:

  

该文件已存在。要替换文件

吗?

在代码中,我不想要警告

代码:

    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();

1 个答案:

答案 0 :(得分:0)

添加

app.DisplayAlerts = false;
SaveAs来电之前

并添加

app.DisplayAlerts = true;
Close事件之后