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 > (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.
Related
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)
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),\"\")";
I have a .NET Windows Forms applications and I need to copy a list of 8-digit numeric codes into the clipboard to be pasted to Excel sheet.
string tabbedText = string.Join("\n", codesArray);
Clipboard.SetText(tabbedText);
The problem is that when a code begins with one or more zeros (ex. "00001234") it's pasted as number with the zeros trimmed.
Is there a way how to set clipboard text so that Excel accepts it as text?
I would treat this problem inside of Excel (and not in your application programaticaly). Format your cells to be treated as text, and then paste from clipboard. This way leading zeros are always pasted.
EDIT: This doesn't work in Excel, in that the apostrophe gets pasted in and shows up too. I'm leaving the answer here as an explicit statement that this approach won't help for Excel.
It does work for OpenOffice Calc though.
The standard way to 'tell' Excel to treat a string as a string is to prefix it with an apostrophe. Have you tried something like:
string tabbedText = "'" + string.Join("\n'", codesArray);
(note the extra apostrophe in there... it's a bit hard to see).
Of course, this may cause you issues if you're planning to use this value thereafter in Excel calculations but there are ways to handle that too.
I can't seem to handle a CSV I got. It's a file generated by a bank, which looks like this:
"000,""PLN"",""XYZ"",""2011-08-31"",""2011-08-31"",""0,00"""
1,""E"",""2011-08-30"",""2011-08-31"",""2011-08-31"",""399,00"",""0000103817846977"",""UZNANIE OTRZYMANE ELIXIR"",""23103015080000000550217023"",""XXX"",""POLISA UBEZPIECZENIA NR XXX "",""000""
3,""E"",""2011-08-31"",""2011-08-31"",""2011-08-31"",""1433,00"",""0000154450232753"",""UZNANIE OTRZYMANE ELIXIR"",""000"",""XXX"",""POLISA UBEZPIECZENIA XXX "",""000""
(I changed all sensitive information).
I've been trying to parse it since morning but no biggie. I used the LINQ to CSV example found somwhere on the net, the CodeProject one (both of them threw an error which said that the CSV is corrupted) and I ended with FileHelpers which SEEMS to work BUT:
It splits the "399,00" and similar values into two fields.
When I use the [(FieldQuoted()] attribute it all goes to hell, since all the fields are quoted in DOUBLE quotation marks. I suspect that is the reason why the other parsers wouldn't work.
Any ideas how to handle it?
If the problem seems to be the double quote, you could preprocess each line by substituting the double double quotes by single double quotes:
line = line.Replace( "\"\"", "\"" );
Once the whole file has been processed, you can let it handled by any other CSV processor.
It will be probably easier to write your own, anyway.
I have been using Lumen, CommonLibrary, FileHelpers etc. and I ended up with TextFieldParser class (from Visual Basic namespace, but can be used in C# without any problem). I recommend you try that. The only downside is that it's relatively slow. But it seems to cope with edge cases quite well.
I even invented a trick getting it to work with obviously invalid CSV files (""" etc.; OpenOffice Calc couldn't handle them properly) - when I'd encounter such a line and got a MalformedLineException, I'd still parse it within the catch block with the HasFieldsEnclosedInQuotes property set to false, for a change.
It would split the line properly, just leaving all the values in double apostrophes. All I had to do then was to remove these double quotes "manually".
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 "="