A cell can contain both a formula and a value. When formulas are entered in Microsoft Excel, the formulas are immediately calculated and the cell value is updated. ExcelWriter does not calculate formulas on the server. It sets the formula for a given cell and relies on Microsoft Excel to calculate the value when the spreadsheet is opened on the client machine.
ExcelApplication preserves both formulas and their corresponding values when it opens an existing spreadsheet. This is because ExcelApplication is not only used for writing Excel files, but also for reading Excel files (for example, an application may allow users to edit a spreadsheet and upload it back to the server).*
ExcelTemplate, on the other hand, is a lightweight object used for quickly populating a spreadsheet with data and is not designed to be used for reading values. Therefore, it does not preserve values of cells containing formulas, since it is expected that the generated spreadsheet will be opened and recalculated on the client machine.
Therefore, in order to read values from cells containing formulas that were already calculated in Excel, the spreadsheet should be opened and processed only with the ExcelApplication object.
* Note: ExcelApplication will only return accurate values for a formula that was already calculated in Excel. For formulas referencing cells that are populated with new data on the server, the value of the formula cell cannot be expected to be accurate, since the formula will not be recalculated until the ExcelWriter-generated file is opened on the client machine. |