Excel calculation problems - c#

I have .xlsx document in which I need to import some values to cells with defined names. Few of those cells are formatted as currency and values that I am importing in those cells are of decimal type which I import in this manner:
cell.CellValue = new CellValue(value.ToString().Replace(",", "."));
In the same spreadsheet there are a few cells that have a formula in which currency cells I imported are used (eg., I am importing value in cell H27 with defined name Total, and in the field I27 there is a formula =Total*0.23).
After import is complete, values are successfully imported (and correctly formatted as currency), but formula cells are not correctly calculated until I either click on formula check marks for each formula cell or I change the currency value (in this case, all formulas containing this cell are refreshed).
What do I have to do for cells with formulas to automatically calculate values after import is completed?

I've figured it out. The cells with formula have <CellFormula> fields inside of them. Those fields have bool attribute CalculateCell which, if set to true, tells Excel to calculate the formula after opening the file. Since I don't know up front which formula cells are affected by the cells, I manage all of them like this:
foreach (var cell in sheetData.Descendants<CellFormula>())
cell.CalculateCell = true;

Related

How can I hide and protect the text of one Cell in an excel file using openxml with c# (not the entire row/ column)?

I'm not sure if it is possible to hide and protect the data of a specific cell in excel file using openxml(not the entire row or column).
Anyone have an idea about that please?
Try using the Protection Class:
public class Protection : OpenXmlLeafElement
18.8.33 protection (Protection Properties)
Contains protection properties associated with the cell. Each cell has
protection properties that can be set. The cell protection properties
do not take effect unless the sheet has been protected.
hidden (Hidden Cell)
A boolean value indicating if the cell is hidden. When the cell is
hidden and the sheet on which the cell resides is protected, then the
cell value is displayed in the cell grid location, but the contents of
the cell will not be displayed in the formula bar. This is true for
all types of cell content, including formula, text, or numbers.
Therefore the cell A4 can contain a formula "=SUM(A1:A3)", but if the
cell protection property of A4 is marked as hidden, and the sheet is
protected, then the cell should display the calculated result
[Example: "6" end example], but will not display the formula used to
calculate the result.
The possible values for this attribute are defined by the W3C XML
Schema boolean datatype.
locked (Cell Locked)
A boolean value indicating if the cell is locked. When cells are
marked as "locked" and the sheet is protected, then the options
specified in the Sheet Part's sheetProtection element (ยง18.3.1.85) are
prohibited for these cells.
The possible values for this attribute are defined by the W3C XML
Schema boolean datatype.
https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc798966%28v%3Doffice.14%29

How to find and use specific cell

I have an excel file with a column of names and a column of numbers.
I had imported the excel into the Datagrid and manage to find the maximum number among from the column.
For example, the cell which is the maximum is at (x,y).
How do I get the program to know the cell and display the name in the same row?

Data missing when excel export from gridview

During exporting excel from gridview the data of a column known as Account Number prefixes 0 are removed. Please guide me for the same
You need to get Account number into a string variable so as to protect leading zero, then while exporting to excel, you need to set cell type to string before you set the value to cell.

C# Excel formula indicator

I want to convert the data of Excel file in the DataTable for edit. But cells with formulas should not be editable. How do I find that the cell contains a formula, if I get the calculated value in it? Thanks.
Try the .HasFormula property of Range
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.hasformula.aspx
Range.SpecialCellsmethod enables to get cells with constant values or formulas.

Reading formula calculated value from excel cell in c#

I am using linqtoexcel for reading from a excel sheet. the problem i face is that i can't read the formula calculated value from a cell, instead i get the actual value of the cell. Is there any way to read the end value of the cell from the excel sheet using linqtoexcel or by some other means.
linqtoexcel uses OleDB to access the sheet's data, and since OleDB does not allow the formulas to be accessed, you can't read the formula from the excel cell. You can only view the value for the cell.

Categories