简体   繁体   English

使用C#代码和oledb更新任何Excel单元格时,如何强制更新依赖单元格?

[英]How to force update dependent cell when updated any excel cell using c# code and oledb?

How to force update dependent cell when updated any excel cell using c# code and oledb? 使用C#代码和oledb更新任何Excel单元格时,如何强制更新依赖单元格? My site is hosted in cloud environment. 我的网站托管在云环境中。 User click on button and at that time my code get required values from database and update the few excel cells and prompt user to download this excel sheet. 用户单击按钮,那时我的代码从数据库中获取必需的值,并更新了一些excel单元格,并提示用户下载此excel工作表。 lots of calucation depends on this updated excel cells. 大量的计算取决于此更新的Excel单元格。

When I mannually update this cell all calculations works fine but when updating using c# code it is not working. 当我手动更新此单元格时,所有计算都可以正常工作,但是使用c#代码更新时,则无法正常工作。

Pleasse check my Code here 请在这里检查我的代码

            String connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 XML;HDR=NO\";";

            System.Data.OleDb.OleDbConnection MyConnection;

            System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();

            string sql = null;

            MyConnection = new System.Data.OleDb.OleDbConnection(connstring);

            MyConnection.Open();

            myCommand.Connection = MyConnection;

            //term in months
            sql = "UPDATE [InputSheet$C7:C7] SET F1 = " + model.Lender.ApprovedTerm;
            myCommand.CommandText = sql;
            myCommand.ExecuteNonQuery();

            //intrest rate
            sql = "UPDATE [InputSheet$C8:C8] SET F1 = " + model.Lender.ContractRate;
            myCommand.CommandText = sql;
            myCommand.ExecuteNonQuery();

            //loan amount
            sql = "UPDATE [InputSheet$C9:C9] SET F1 = " + model.Lender.ApprovedLoanAmt;
            myCommand.CommandText = sql;
            myCommand.ExecuteNonQuery();

            MyConnection.Close();

I don't want to use Open XML. 我不想使用Open XML。 Is there any other way to force update dependent cell? 还有其他强制更新依赖单元的方法吗?

What you are asking, is how to force recalculation of a formula when you change an Excel sheet's values. 您要问的是如何在更改Excel工作表的值时强制重新计算公式。 You can't do when you use the OLE DB provider, since it treats the Excel sheet as just another database. 当您使用OLE DB提供程序时,您将无法执行操作,因为它会将Excel工作表视为另一个数据库。 Only Excel itself is able to execute the formulas and recalculate results. 只有Excel本身才能执行公式并重新计算结果。

If your code uses a template Excel file, you can change open the file in Excel, the file's properties to recalculate formulas by changing the Calculation options in the formula menu. 如果您的代码使用模板Excel文件,则可以通过在“公式”菜单中更改“计算”选项来更改在Excel中打开文件的属性,以重新计算公式。

A better option though is to use Open XML instead of JET to set the values and change the file's calculation options to recalculate on load, as shown in this SO question 不过,更好的选择是使用Open XML而不是JET来设置值并更改文件的计算选项以在加载时重新计算, 如此SO问题所示

spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

Even that won't recalculate the formulas though, it will change the calculation properties to recalculate each time you open the file in Excel. 即使那样也不会重新计算公式,但是每次您在Excel中打开文件时,它都会更改计算属性以重新计算。

A better option is to use a library like EPPlus , which can calculate formulas . 更好的选择是使用类似EPPlus的库,该库可以计算公式 The library is a lot easier to use than the Open XML SDK too: 该库也比Open XML SDK易于使用:

using(var package = new ExcelPackage(new FileInfo(@"c:\temp\tmp.xlsx")))
{
   var sheet=package.Workbook.Worksheets["my sheet"];
   var lender=model.Lender;
   sheet.Cells["C7"].Value=lender.ApprovedTerm;
   sheet.Cells["C8"].Value=lender.ContractRate;
   sheet.Cells["C9"].Value=lender.ApprovedLoanAmt;
   // calculate all formulas in the sheet
   sheet.Calculate();
}

The Open XML SDK does not require installation of Office or an SDK on the server - it's not Office Interop, it's a library that allows direct manipulation of the XLSX files. 开放XML SDK 不需要安装的Office或服务器上的SDK -这不是办公室互操作,这是一个库,允许XLSX文件的直接操作。 It also doesn't require installation of any drivers, like OLEDB does. 它也不需要像OLEDB一样安装任何驱动程序。

EPPlus makes working with XLSX files even easier, adding easier formatting, manipulation, LINQ queries and even loading directly from DataTables or collections. EPPlus使处理XLSX文件变得更加容易,添加了更简单的格式,操作,LINQ查询,甚至直接从DataTables或集合中加载。 You only have to add the library's NuGet package to a project. 您只需要将库的NuGet包添加到项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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