Dependent Excel cells are not updated automatically

ⅰ亾dé卋堺 提交于 2019-12-08 09:23:57

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9169315/dependent-excel-cells-are-not-updated-automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!