我有以下问题:
我从microsoft sql管理系统中读取了一个表,并希望每天将此表导出到具有相同excel-name的文件夹中,这意味着覆盖。
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仍在显示,有人可以帮我吗?非常感谢!!!
答案 0 :(得分:1)
首先删除文件:
if (File.Exists(@"Y:\\Favoriten\\Flop_N7.xls")) {
File.Delete(@"Y:\\Favoriten\\Flop_N7.xls");
}