使用c#OLEDB连接更新excel中的单元格

时间:2017-02-03 06:06:03

标签: c# excel oledb

我的问题需要你的帮助: 我这里有一个代码,使用c#更新excel中的单元格。我的问题是我必须首先打开我的excel文件,以便它可以更新单元格。我需要做的是更新单元格而不打开excel文件。有可能吗?

到目前为止,这就是我所拥有的:

    private void WOupdate()
    {
        string WOExcel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\t-jjtabije\\Desktop\\WO And CAO Update.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text'";
        string conStr, sheetName;
        conStr = string.Empty;
        //Get the name of the First Sheet.
        using (OleDbConnection kuneksyon = new OleDbConnection(WOExcel07ConString))
        {
            using (OleDbCommand utos = new OleDbCommand())
            {
                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {
                    utos.Connection = kuneksyon;
                    kuneksyon.Open();
                    DataTable dtExcelSchema = kuneksyon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    kuneksyon.Close();
                    utos.Connection = kuneksyon;
                    utos.CommandText = "UPDATE [" + sheetName + "] SET [LTE WO]= '" + Path.GetFileName(WOfilename) + "', [REFARM LTE BW]='" + WObw.ToString() + "', [WO Status-LTE]='" + WOstat.ToString() + "' WHERE SITEID= '" +WOsiteid.ToString()+ "'";
                    kuneksyon.Open();
                    MessageBox.Show(WOfilename + WOsiteid + WOservice + WObw + WOstat);
                    oda.SelectCommand = utos;
                    utos.ExecuteNonQuery();
                    kuneksyon.Close(); ;
                }
            }
        }
    }

0 个答案:

没有答案