1

I wrote this method (almost similar in other post)

public void update(string fileName, string sheetName)
{
    string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties='Excel 12.0;HDR=NO'";

    try
    {
        OleDbConnection oledbConn = new OleDbConnection(connString);

        oledbConn.Open();

        OleDbCommand cmd = new OleDbCommand("UPDATE ["+sheetName+"$B5:B5] SET F1=17", oledbConn);

        cmd.ExecuteNonQuery();

        oledbConn.Close();
    }
    catch(Exception ex)
    {
        Debug.Write("Error: " + ex.Message);
    }
}

and calling that method:

update("test.xls", "test");

So far, it works fine because when I open the test.xls file, B5 gets updated to 17. However, if there is a cell: B1 is dependent on B5: B1=B5*5, then B1 will not get updated automatically. I have to manually open the Excel file and save it with warning in order to get B1 updated. How can I do it programmatically?

Jacob
  • 77,566
  • 24
  • 149
  • 228
olidev
  • 20,058
  • 51
  • 133
  • 197

1 Answers1

4

I don't think that you can depend on Excel updating calculated columns when you use the ACE driver to interact with the Excel worksheet. When you are using OLEDB to operate on the workbook's worksheet, it is treating the worksheet as a database table like structure.

I think you may want to use OpenXML to read/write to the file. There are several threads on StackOverflow with more info on using OpenXML that are worth checking out.

This post shows your exactly how to force a cell re-calc using OpenXML.

Community
  • 1
  • 1
codechurn
  • 3,870
  • 4
  • 45
  • 65