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

时间:2021-01-21 07:36:21

i have following question:

我有以下问题:

i read a table from microsoft sql management and want to export this table everyday into a folder with the same excel-name, that means overwrite.

我从microsoft sql management中读取了一个表,并希望每天将这个表导出到一个具有相同的*名称的文件夹中,这意味着覆盖。

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

I already used the Display.Alerts=false, but the MessageBox is still showing, can someone help me? Thanks a lot of!!!

我已经用过显示器了。警告=false,但是MessageBox仍在显示,有人能帮助我吗?谢谢很多! ! !

1 个解决方案

#1


1  

Simply delete the file first:

只需先删除文件:

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

#1


1  

Simply delete the file first:

只需先删除文件:

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