When I try to add a SUMIF formula to an Excel file that has data, Excel says the file has “unreadable content” and asks if it has to try to recover the contents of the file. If I then click “Yes”, it removes said formula and opens the file.
When I try to copy-paste the same SUMIF formula that was generated in the Excel file manually, it works. When I try another formula (a simple “SUM” for instance) it works.
Does anyone have any idea what could be wrong?
I’m using the OpenXML SDK from Microsoft to write to the Excel file. Again, this code works perfectly for some formulas (e.g. SUM), but not for SUMIF.
/// <summary>
/// Gets or set the cell formula
/// </summary>
public string Formula
{
get
{
return _cell.CellFormula.Text;
}
set
{
if(_cell.CellFormula == null)
_cell.CellFormula = new CellFormula();
_cell.CellFormula.Text = value;
}
}
Edit: After opening the Excel file and checking the xml files within, I found that the SUMIF function is saved in the exact same way as the SUM function (“=SUMIF(J3:J33;L34;N3:N33)” and “=SUM(N3:N33)” respectively, both without quotes), so there is no real difference in how the formula is written to the file.
Thanks in advance!
— Spoiler: The solution is to use “,” instead of “;” when working with formulas in your code.
Whenever I get unreadable content errors when working with the Open XML SDK, I will create a blank worksheet and add the piece that is causing the errors to that worksheet. I will then use the Open XML SDK 2.0 Productivity Tool to see what gets generated behind the scenes and use the code it produces to get rid of the unreadable content errors.
I did those steps and noticed the following were added when you add the
SUMIFformula. The first being you need to add the formula to the cell using the following code:This will produce the following XML:
The text value is just the result of the summation, which is zero in my case since the range I defined were empty.
Next you need to make sure the
worksheetcontains asheetDimensionwhich is defined as:The code that code generated for me was:
The XML looks like:
Next you need to make sure the
worksheetPartcontains acalcChainPartwhich will have acalcChainelement with acalculationCellchild.This just tells excel which sheet contains the formula and which cell it is applied to. Here is the code and XML for mine:
Finally, the
workbookPartneeds acalculationPropertieselement which defines the collection of properties the application uses to record calculation status and details. Calculation is the process of computing formulas and then displaying the results as values in the cells that contain the formulas.As you can see, all of these various elements and parts are created for you behind the scenes when you add a formula to a cell when using Excel. Unfortunately, you are responsible for adding the necessary elements when adding formulas using the Open XML SDK. Most likely one of these elements are missing from your Excel document, which is why you are probably getting an unreadable content error when opening up your Excel document.