本文介绍了不会自动更新相关的Excel单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个方法(在其他职位几乎相同)

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);
    }
}

和调用该方法:

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



到目前为止,它工作正常,因为当我打开TEST.XLS文件, B5 被更新,以 17 。但是,如果有一个单元格: B1 取决于 B5 B1 = B5 * 5 ,那么 B1 不会得到自动更新。我必须手动打开Excel文件,以便警告要获得 B1 更新保存。我怎样才能做到这一点编程?

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?

推荐答案

我不认为你可以依靠的Excel更新计算列当您使用ACE驱动程序与Excel工作表进行交互。当您使用OLEDB到工作簿中的工作表上运行,它是治疗工作表状结构的数据库表。

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.

我想你可能需要使用的来读/写文件。有计算器上的多个线程上使用的OpenXML是值得检查出更多的信息。

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.

这的显示你究竟是如何使用的OpenXML强制细胞重新计算。

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

这篇关于不会自动更新相关的Excel单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 12:52