I'm having an issue with EPPlus in C#. I created a document and it works fine except for one part. I'm trying to create a formula that accesses data from a different sheet.
summarySheet.Cells["A2"].Formula = "$Details.B19";
I've also tried
summarySheet.Cells["A2"].Formula = "=$Details.B19";
The cell showed #Name? for the value. When I checked the formula, the capital letters had been reduced to lowercase letters:
=$details.b19
So the formula doesn't work. On every formula that I've tried using a formula that access a different sheet, I get the same result. However, the formulas stay capitalized for the cell info from the same sheet.
I tried subtracting data from one sheet from data from the same sheet:
summarySheet.Cells["A2"].Formula = "=$Details.B19 - B20";
And I get:
=$details.b19 - B20
So the capitalization error only occurs when accessing cells from a different sheet.
I'm guessing that it's something I'm doing wrong. My experience with C# is limited to personal projects and I've only started messing with EPPlus.
Thanks for looking and thanks in advance for any help that's given.
Use the following
summarySheet.Cells["A2"].Formula = string.Format("'{0}'!{1}", sourceSheet.Name, sourceSheet.Cells["B19"].Address);
Related
I have winform project in c# that I want to copy data from Excel cells to my datagirdview. But the excell cells is formatted with 2 digits after the comma.I want to get all value.
What i mean :
The excell cell value = 884,79258
Formatted value = 884,79
I am getting the data with Clipboard.GetText() method. Bu it gives me formatted value(884,79).
But i want the get all value which is 884,79258.
These are my codes
String[] lines = Clipboard.GetText().Split('\n');
string result = lines[0];
Thanx.
Edit:
Thanx to dr.nulll. The link that he shared is worked.
I am sharing the link below for those who have similar request.
Copying decimal values from Excel to C# causes to copy only displayed values
Don’t go via the clipboard. Either use Excel via interop or a 3rd party library like NPOI. If you must use the clipboard then the value copied reflects the current formatting of the cell, so you need it set to show 4 digits after the decimal point.
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 am creating an Excel file using C#.
The end result should be:
Excel workbook with 5 sheets
1 sheet is generated using other 4 sheets.
4 sheets are generated from database
All the formulas should be added to sheet 1 just in case the user wants to add data manually to other 4 sheets
I am stuck at:
After I create all the 4 sheets, I am trying to add formulae to sheet 1.
Formula Logic - Look for value of column A (all rows one by one) of sheet 1 (current) in sheet 2's column A, and get the value of column K of that row.
I am trying to add the below VLOOKUP to the 12th row and will copy the formula to all rows.
oRng = worksheet.Range["J12"];
oRng.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-9],'Sheet2'!$A:$K,11,0)),'',VLOOKUP(RC[-9],'Sheet2'!$A:$K,11,0)";
But I get the error:
Exception from HRESULT: 0x800A03EC
I believe you are:
Mixing A1 and R1C1 style (which it doesn't like)
using '' instead of "" (which it doesn't like)
using $ for absolute references on A1 style references in FormulaR1C1 (which it doesn't like)
edit it seems less likely in your case as you've said the formula works with direct input, but you may also have a locale issue where excel wants semicolon list separators and you are using commas, see my answer here for a solution to that. (end edit)
So try:
oRng.Formula = #"=IF(ISERROR(VLOOKUP(A12,Sheet2!$A:$K,11,0)),"""",VLOOKUP(A12,Sheet2!$A:$K,11,0))";
or
oRng.FormulaR1C1 = #"=IF(ISERROR(VLOOKUP(RC[-9],Sheet2!C1:C11,11,0)),"""",VLOOKUP(RC[-9],Sheet2!C1:C11,11,0))";
(If you don't want to prefix your string with # then change all "" to \" inside the string)
Footnote
You can shorten your formula to this in later versions of Excel (2007 onwards):
oRng.Formula = #"=IFERROR(VLOOKUP(A12,Sheet2!$A:$K,11,0),"""")"
oRng.FormulaR1C1 = #"=IFERROR(VLOOKUP(RC[-9],Sheet2!C1:C11,11,0),"""")"
Put the correct number of parenthesis in your Excel formula : you are missing one at the end.
I am looking to have a validation in an excel sheet which is generated using C# such that it allows either date value or the text "train" in the excel cell. I found XlDVType.xlValidateDate for validating date in a cell and XlDVType.xlValidateList to allow text values. But I want a combination of both, user should either be able to enter date or a particular text in the cell. Can this be accomplished using XlDVType.xlValidateCustom ??
any help on this would be greatly appreciated.
I want a combination of both, user should either be able to enter date
or a particular text in the cell.
The following code snippet sets the desired validation for cell A1 in your worksheet:
Excel.Range range = xlWorkSheet.Range["A1"];
range.ClearFormats();
range.Validation.Delete();
range.Validation.Add(Excel.XlDVType.xlValidateCustom,
Formula1: "=OR(EXACT(LEFT(CELL(\"format\",A1)),\"D\"),EXACT(A1,\"train\"))");
The most complicated part is the actual validation formula. It uses the following functions:
EXACT(cell,string) compares the contents of a cell with a literal string and returns TRUE only if they are exactly the same.
CELL("format",cell) returns a code containing the format of the indicated cell, where the code for a date always starts with a D. See Office help for CELL for an explanation and a list of all possible codes. This snippet only looks at the first character, which has to be a D in order for the contents to be formatted as some kind of date.
LEFT(text,count) returns the leftmost count characters in text, with count defaulting to 1. In this case, it returns TRUE if the contents are some kind of date.
OR(logical1, logical2) returns logical1 OR logical2, so TRUE if the contents are either a date, or exactly equal to the string "train".
This solution is not yet ideal, because A1 is hardcoded into the formula. There is a way around that using INDIRECT, described here. You might need that, for example if you want to apply the validation to a whole range of cells in one go. In that case, the hardcoded A1 needs to be replaced by INDIRECT("RC",FALSE), which is the mechanism to indicate the current shell. You can read it as a short-cut for INDIRECT("R"&ROW()&"C"&COLUMN(),FALSE).
With that adjustment, he last line of code then looks like this (tested in Excel, but not from C#):
range.Validation.Add(Excel.XlDVType.xlValidateCustom,
Formula1: "=OR(EXACT(LEFT(CELL(\"format\",INDIRECT(\"RC\",FALSE))),\"D\"),EXACT(INDIRECT(\"RC\",FALSE),\"train\"))"
Update
As the OP points out, there appears to be a problem with this approach if a date has first been entered into the cell, and then is changed to an incorrect string or number value -- see the comments below. Using the CELL function therefore does not seem to be the best approach.
However, if the cell format is explicitly set to the (more flexible) Text format, it is possible to impose the validation with the following lines:
range.Validation.Delete();
range.NumberFormat = "Text";
range.Validation.Add(Excel.XlDVType.xlValidateCustom,
Formula1: =OR(ISNUMBER(DATEVALUE(INDIRECT(\"RC\",FALSE))),EXACT(INDIRECT(\"RC\",FALSE),\"train\"));
Again tested in Excel, but not from C#.
I am using the .Net 4.0 and excel 2003
How can i use an oledb connection to retrieve the cell format of an excel spreadsheet... I specifically want to find out if a cell column (or cell itself) is in a numeric percentage format.
I cannot seem to find this information in the GetOleDbSchemaTable method.
EX: My web app reads numbers from an excel spreadsheet. This works fine; However, if the numbers are in a percentage format, excel displays it as (fraction*100) but the actual value is a fractional decimal (1/3 = .3333..) - Excel displays as 33.33% - (Notice the decimal point).
Therefore, i need a way of distinguishing between what is a percentage & what is not to allow my webapp to work properly...
Any ideas?
Thanks in advance.....
You might be able to get out this information with OleDbConnection.GetSchema, but I'm not sure what information you'll get for an Excel sheet with that. Documentation here.
Search the NumberFormat property of the Cell (Range) in question for a % sign.
Also, if you can get the format type, then you're looking for a format that starts with 'P', like 'P1'.
EDIT: The only way I can see is either using XML or Automation. For automation you need to use the Interop Assembly. Namespace: Microsoft.Office.Interop.Excel, Interface: DisplayFormat, Property: NumberFormat.
Can you just read the first row of data as a string and parse it looking for '%' in the string.