简体   繁体   中英

Not able to update cell in excel woksheet using oledb connection

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

    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.

Any help will be appreciated.

My excel sheet looks like this.. Excel模板

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM