C# - Set excel cell with a formula containing double quotes - c#

I'm trying to set via c# code the formula of an excel cell.
I use Microsoft.Office.Interop.Excel, version 14.
I always get an excel error 0x800A03EC, which is a kind of generic error.
String formula = "=IF(A2=\"BLANCO\";\"\";C1+1)"
Range cell = (Microsoft.Office.Interop.Excel.Range)ws.Cells[2, 3];
cell.Formula = formula;
I also tried to escape the double quotes with an #
String formula = #"=IF(A2=""BLANCO"";"""";C1+1)"
Same error, same problem.
When I try to set a simple formula, where no quotes are involved it is working fine.
Anybody has a solution?

Don't have much context for your code, but slashes should work when used appropriately. I am not sure about the use of semicolons here.
This code is an example pulled straight from one of my Interop programs and works fine:
thisExcel.xlWorksheet.Range["AD44", Type.Missing].Value = "=IF(BULK!L7=\"#N/A Field Not Applicable\",\"( \"&'Title Look Up'!C3&\" )\",\"( \"&'Title Look Up'!C3&\" )\")";
If all you want is: 'if a2 reads blanco then nothing else increment value of c1 by 1', then you just need:
=IF(A2=\"BLANCO\",\"\",C1+1)
I would also try using .Value rather than .Formula.
Hope that help

Building upon #getglad's answer - Using a slash does work for double quotes.
My recent solution (as of 2020):
worksheet.Cells[row,col].Value = "=IFERROR(VLOOKUP(etc),\"\")";

Related

Change IXLCell number format from decimal point (.) to decimal comma (,) - c#

So it's pretty simple..(at least I thought so...)
I'm trying to export some data to excel from my app and I can't find a solution how to change decimal point to decimal comma (this is also an European standard)
This is the code currently and numbers are written like 12,561.00
var cell = worksheet.Cell(i, 1);
cell.SetDataType(XLDataType.Number);
cell.Style.NumberFormat.SetFormat("#,##0.00");
I can't find solution for changing this code to show it like 12.561,00
I've tried obvious solution like this and similiar variations:
cell.Style.NumberFormat.SetFormat("#.##0,00");
Also I've tried these solutions from this link
I suppose I have to change format number globally in whole worksheet or workbook but I couldn't find any option that could work.
Thanks!

Cannot export formula to .xlsx spreadsheet using c#

So I'm trying to use COUNTIF function to reference several different worksheets and count all occurrences of a keyword. I'm using the C# NuGet Package ClosedXML to export the data and the formula, but every time I do so, I get the following error in Numbers:
The formula couldn’t be imported and was replaced by the last calculated value. Original formula: =COUNTIF(2 WHATUP::Table 1::A2:A85,A6) + COUNTIF(3 Movies::Table 1::A2:A28,A6)
Excel gives me something similar. The only way to get it to accept it is to copy the formula, clear the cell, and paste the formula back. Here is the formula exactly as it is exported within my C# code (disregard the test worksheet names):
COUNTIF(2 WHATUP::Table 1::A2:A85,A4) + COUNTIF(3 Movies::Table 1::A2:A28,A4)
I've also tried the ! notation:
COUNTIF(2 WHATUP!A2:A85,A2) + COUNTIF(3 Movies!A2:A28,A2)
I've also tried using SUM to add them both together instead of +:
=SUM(COUNTIF(2WHATUP::Table 1::A2:A85,A2),COUNTIF(3Movies::Table 1::A2:A28,A2))
Yes, I've even tried adding more arguments within a single COUNTIF. This returns the same error as well.
=COUNTIF(1 Jello::Table 1::A2:A328, 3 Movies::Table 1::A2:A28, A2)
The only time I can get it to work is when I only use one COUNTIF to calculate a single range (without adding the results of another COUNTIF). But I need to add together the occurrences of a keyword throughout several worksheets, hence the use of several COUNTIFS/several arguments within a COUNTIF.
Please help! I have tried everything I can think of.
Thank you so much.
Looks like you have some space characters in the worksheet names, which then require beeing enclosed by single quotes.
Adopting your second example, following should work just fine:
=COUNTIF('2 WHATUP'!A2:A85,A2) + COUNTIF('3 Movies'!A2:A28,A2)

Aspose.Cells using array (Ctrl+Shift+Enter or CSE) formula in conditional formatting

I'm using Aspose.Cells .NET and I'm trying to use array formula (Ctrl+Shift+Enter or CSE) in conditional formatting expression. Array formula looks like this:
=VALUE
(INDEX('Raw data'!$A$5:$AE$32;
MATCH(1;(A5='Raw data'!$A$5:$A$32)*(B5='Raw data'!$B$5:$B$32);0);
3)
)
<> VALUE(C5)
(Line breaks and spaces for convenience. They are not present in actual formula)
Basically it does the following:
Looks for a row in another sheet (Raw data) by two columns A5='Raw data' Column A AND B5 = 'Raw data' Column B
Gets 3-rd column in a row that was found
Checks that its value not equal to C5 cell
It works when i place it directly into the cell (with Ctrl+Shift+Enter)
It also works when i create conditional formatting (with it) manually with Ms Excel 2010.
But when I try to apply it though Aspose.Cells it throws exception
Invalid formula
// Add an empty conditional formatting
int index = worksheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = ws.ConditionalFormattings[index];
//Add area for conditional formatting
fcs.AddArea(cellArea);
var condition = String.Format(formulaTemplate, initialDataRange, okpoCellName, initialDataOkpoColumnRange, initialDataColumn, currentCellName);
// Throws here
// condition = "=VALUE(INDEX('Raw data'!$A$5:$AE$32;MATCH(1;(A5='Raw data'!$A$5:$A$32)*(B5='Raw data'!$B$5:$B$32);0);3)) <> VALUE(C5)"
int conditionIndex = fcs.AddCondition(FormatConditionType.Expression, OperatorType.None, condition, "");
I suspect that when I add condition, Aspose tries to evaluate it as usual formula (not array formula) and fails. Is there any way to disable/postpone this evaluation?
Is this a bug in Aspose, or am I doing something wrong?
Well, Aspose.Cells might not support array formulas in conditional formattings. We recommend you to kindly post your issue/requirements into Aspose.Cells forums. Give us complete details and complete sample code with your expected Excel file which can be created by MS Excel manually. We can better help you there.
I am working as Support developer/ Evangelist at Aspose.

Excel formula with double quote with C#

I would like to write a formula in a Excel cell with SpreadSightLight in C#.
I have a following formula in excel
=COUNTIFS(Sheet2!E:E, D2, Shet2!A:A, ">0")
In the code it looks
"=COUNTIFS({0}!E:E,{1},{0}!A:A ,\">0\")";
I save the file from the code, but when i try to open the file Excel (2013) gives error message and the formula is removed
Removed Records: Formula from /xl/worksheets/sheet3.xml part
and the sheet3.xml contains the following line
COUNTIFS(Sheet2!E:E, D2, Sheet2!A:A ,">0")
I tried the verbatim string and the char(34) solutions also but causes the same error.
How can I solve this problem?
Thanks in advance.
I have not succeed to find the reason why there can be no double quotes written into an excel formula. However there is a solution to avoid double quotes:
string formula = "=COUNTIFS({0}!E:E, {1}, {0}!A:A , CONCATENATE(CHAR(62), CHAR(48)))";
In case anybody subsequently comes across this thread, I believe the problem is the greater than sign. It's getting confused with the end tag in XML. So it needs to be replaced by &gt (as hinted at by what you said your sheet3.xml contains).
I think the double quotes are fine as you have them - escaped with a preceding backslash.

wrting double problem to excel

i have a text and i try to write this text to excel file by c#. this text include not one kind of value. i mean, it can be include "abc" "1.6", "1/6" etc. when i write 1.86 or 1/86 i see "feb.86" in the excel. but i want to see what i write to. what can i do to solve this problem?
for numbers like 1/86 you can prefix it with '. But then you will get "Number Stored as Text" error in excel (little green triangle in cell). Otherwise you can always put things in double quotes to work:
="1/86"
PS:- procede with "="

Categories