在没有消息的文件夹中覆盖 Excel

Overwrite Excel in a folder without message

我有以下问题:

我从微软 sql 管理中读取了一个 table 并且希望每天将这个 table 导出到一个具有相同 excel 名称的文件夹中,这意味着覆盖。

 SqlConnection cnn;
        string connectionString = null;
        string sql = null;
        string data = null;
        int i, j = 0;

        Excel._Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        connectionString = "server=zumtesten;database=LogEMD;user=user123;password=user201611;";
        cnn = new SqlConnection(connectionString);
        cnn.Open();
        sql = "SELECT * FROM Flop";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
        DataSet ds = new DataSet();
        dscmd.Fill(ds);

        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
            {
                data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                xlWorkSheet.Cells[i + 1, j + 1] = data;
            }
        }

        xlApp.DisplayAlerts = false;

        xlWorkBook.SaveAs(@"Y:\Favoriten\Flop_N7.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

我已经使用了 Display.Alerts=false,但是 MessageBox 仍然显示,有人可以帮助我吗?非常感谢!!!

干脆先删除文件:

if (File.Exists(@"Y:\Favoriten\Flop_N7.xls")) {
   File.Delete(@"Y:\Favoriten\Flop_N7.xls");
}