1

How to force update dependent cell when updated any excel cell using c# code and oledb? 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. lots of calucation depends on this updated excel cells.

When I mannually update this cell all calculations works fine but when updating using c# code it is not working.

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. Is there any other way to force update dependent cell?

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Rahul Kale
  • 11
  • 4
  • What do you mean "dependent cell"? To you mean that you want to force the formula to recalculate? It won't do so by itself. *Why* don't you want to use OpenXML? It's faster, easier, safer (etc) to use a library like EPPlus to update cells directly, *doesn't* require a JET driver. And it [supports formula recalculation](http://epplus.codeplex.com/wikipage?title=About%20Formula%20calculation&referringTitle=Documentation) – Panagiotis Kanavos Oct 06 '15 at 07:50
  • Yes, I want to force the formula to recalculate. I don't want to use open XML because every time when we push new version on portal (cloud envirment) we need to install Office and it's SDK for OpenXML. – Rahul Kale Oct 06 '15 at 08:01
  • No you don't! This is 195% wrong! In fact it's the OLEDB provider that requires extra installation. I think you are confusing Open XML with Office Interop. The Open XML SDK is only a library for manipulating office files. You can add it to your project just like any other assembly, or use a library on top of it like EPPlus – Panagiotis Kanavos Oct 06 '15 at 08:21
  • Great. Tried EPPlus. Working fine. Thanks... – Rahul Kale Oct 06 '15 at 09:29

1 Answers1

-1

What you are asking, is how to force recalculation of a formula when you change an Excel sheet's values. You can't do when you use the OLE DB provider, since it treats the Excel sheet as just another database. Only Excel itself is able to execute the formulas and recalculate results.

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.

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

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.

A better option is to use a library like EPPlus, which can calculate formulas. The library is a lot easier to use than the Open XML SDK too:

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. It also doesn't require installation of any drivers, like OLEDB does.

EPPlus makes working with XLSX files even easier, adding easier formatting, manipulation, LINQ queries and even loading directly from DataTables or collections. You only have to add the library's NuGet package to a project.

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Using ForceFullCalculation is overkill (switches off Excel's smart recalculation and makes every single formula in the workbook calculate at every calculation) and can slow down the calculation of the workbook. Better to just set Calculation to automatic. – Charles Williams Oct 06 '15 at 09:01
  • @CharlesWilliams better to not have to force recalculations at all, and generate a complete Excel file from start. Besides, FullCalculationOnLoad is *not* available in Excel 2007, and I doubt if it's available eg in Google Docs. Unless the OP can guarantee the Excel version used, both options may be required. – Panagiotis Kanavos Oct 06 '15 at 09:06
  • you don't need either Forcefullcalculation or Fullcalculationonload: the file will be calculated when Excel opens it anyway. – Charles Williams Oct 06 '15 at 11:40
  • @CharlesWilliams in this case the OP's question (and the linked one) are meaningless. As I said though, better to create a complete file than depend on the application's behaviour or the end user's settings. Just because it's an Excel file doesn't mean it will be opened by Excel – Panagiotis Kanavos Oct 06 '15 at 11:42
  • For some cell still it is not working. I want to share screen shot. How can I share image here? – Rahul Kale Oct 06 '15 at 12:44
  • @RahulKale don't share the screenshot, it won't help. Post the formulas. They may not be supported by EPPlus and require manual recalculation, or EPPlus may not support deeply nested formulas. Also check the calculation settings of the resulting Excel sheet. If it's set to Automatic, the formulas *should* have been updated when you opened the file – Panagiotis Kanavos Oct 06 '15 at 12:47
  • Also, are all formulas in the same sheet or different worksheets? You need to call `Workbook.Calculate()` to recalculate formulas in the entire workbook – Panagiotis Kanavos Oct 06 '15 at 12:52
  • It is int same worksheet. I am updating C14 cell with value Yes. It should update value of cell E14 with Yes but it didn't. Formula of E14 is as follows. =IF(C14="","No","Yes") – Rahul Kale Oct 06 '15 at 15:10
  • Any updates on formula. Is any thing wrong in this formula. – Rahul Kale Oct 07 '15 at 08:05