无法使用oledb连接更新excel woksheet中的单元格

时间:2022-09-27 10:41:40

I am trying to update execl sheet paricular sheet by writing this code:

我试图通过编写此代码来更新execl sheet paricular sheet:

    connExcel.Open();
    string SheetName = null;
    DataTable dtExcelSchema = null;
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    //checking sheet name
    if (dtExcelSchema.Rows[2]["TABLE_NAME"].ToString().Contains("_xlnm#Database"))
    {
        SheetName = dtExcelSchema.Rows[2]["TABLE_NAME"].ToString();
    }

    if (!dtExcelSchema.Rows[2]["TABLE_NAME"].ToString().Contains("_xlnm#Database"))
    {
        SheetName = dtExcelSchema.Rows[2]["TABLE_NAME"].ToString();
    }
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, SheetName, null });
    OleDbCommand cmd = new OleDbCommand("UPDATE [" + SheetName + "] SET F11=22052", connExcel);
    int result = cmd.ExecuteNonQuery();
    Console.WriteLine(result);
    connExcel.Close();

But this update query is updating the whole column while i just want update particular cell in excel say 'C9' accodring to excel sheet.

但是这个更新查询正在更新整个列,而我只想更新excel中的特定单元格,说“C9”符合excel表格。

Any help will be appreciated.

任何帮助将不胜感激。

My excel sheet looks like this.. 无法使用oledb连接更新excel woksheet中的单元格

我的excel表看起来像这样..

1 个解决方案

#1


0  

I think you are misunderstanding the syntax. You do it like this:

我认为你误解了语法。你这样做:

 OleDbCommand cmd = new OleDbCommand("UPDATE ["+ SheetName +"$C9:C9] SET F11=22052", connExcel);

#1


0  

I think you are misunderstanding the syntax. You do it like this:

我认为你误解了语法。你这样做:

 OleDbCommand cmd = new OleDbCommand("UPDATE ["+ SheetName +"$C9:C9] SET F11=22052", connExcel);