- Notifications
You must be signed in to change notification settings - Fork906
Evaluating Formulas
Francois Botha edited this pageOct 22, 2016 ·7 revisions
If you call cell.Value ClosedXML will try to resolve the formula and give you the result.
For example:
varwb=newXLWorkbook();varws=wb.AddWorksheet("Sheet1");ws.Cell("A1").SetValue(1).CellBelow().SetValue(1);ws.Cell("B1").SetValue(1).CellBelow().SetValue(1);ws.Cell("C1").FormulaA1="\"The total value is:\" & SUM(A1:B2)";varr=ws.Cell("C1").Value;Assert.AreEqual("The total value is: 4",r.ToString());// It also works if you use: ws.Cell("C1").GetString()
You can even resolve your own formulas without using cells. For Example:
If you're not referencing a worksheet you can use:
varsum=XLWorkbook.EvaluateExpr("SUM(1,2,3)");// sum = 6// SUM(Sheet1!A1:B2) will fail because it doesn't know which workbook to use
If you're not referencing a range without a worksheet you can use:
varsum=workbook.Evaluate("SUM(Sheet1!A1:B2)");// SUM(A1:B2) will fail because it doesn't know which sheet to use
If you have the worksheet you can evaluate at your heart's content:
varsum=worksheet.Evaluate("SUM(A1:B2)");
- Not all formulas are included and you'll probably get a nasty error if the formula isn't supported or if there's an error in the formula. Please test your formulas before going to production.
- I'm adding new formulas all the time but if your formula isn't included please let me know via the Issue Tracker. I'll do my best to include the formula asap.
ce.RegisterFunction("DATE",3,Date);// Returns the serial number of a particular datece.RegisterFunction("DATEVALUE",1,Datevalue);// Converts a date in the form of text to a serial numberce.RegisterFunction("DAY",1,Day);// Converts a serial number to a day of the monthce.RegisterFunction("DAYS360",2,3,Days360);// Calculates the number of days between two dates based on a 360-day yearce.RegisterFunction("EDATE",2,Edate);// Returns the serial number of the date that is the indicated number of months before or after the start datece.RegisterFunction("EOMONTH",2,Eomonth);// Returns the serial number of the last day of the month before or after a specified number of monthsce.RegisterFunction("HOUR",1,Hour);// Converts a serial number to an hource.RegisterFunction("MINUTE",1,Minute);// Converts a serial number to a minutece.RegisterFunction("MONTH",1,Month);// Converts a serial number to a monthce.RegisterFunction("NETWORKDAYS",2,3,Networkdays);// Returns the number of whole workdays between two datesce.RegisterFunction("NOW",0,Now);// Returns the serial number of the current date and timece.RegisterFunction("SECOND",1,Second);// Converts a serial number to a secondce.RegisterFunction("TIME",3,Time);// Returns the serial number of a particular timece.RegisterFunction("TIMEVALUE",1,Timevalue);// Converts a time in the form of text to a serial numberce.RegisterFunction("TODAY",0,Today);// Returns the serial number of today's datece.RegisterFunction("WEEKDAY",1,2,Weekday);// Converts a serial number to a day of the weekce.RegisterFunction("WEEKNUM",1,2,Weeknum);// Converts a serial number to a number representing where the week falls numerically with a yearce.RegisterFunction("WORKDAY",2,3,Workday);// Returns the serial number of the date before or after a specified number of workdaysce.RegisterFunction("YEAR",1,Year);// Converts a serial number to a yearce.RegisterFunction("YEARFRAC",2,3,Yearfrac);// Returns the year fraction representing the number of whole days between start_date and end_datece.RegisterFunction("AND",1,int.MaxValue,And);ce.RegisterFunction("OR",1,int.MaxValue,Or);ce.RegisterFunction("NOT",1,Not);ce.RegisterFunction("IF",3,If);ce.RegisterFunction("TRUE",0,True);ce.RegisterFunction("FALSE",0,False);ce.RegisterFunction("ABS",1,Abs);ce.RegisterFunction("ACOS",1,Acos);ce.RegisterFunction("ACOSH",1,Acosh);ce.RegisterFunction("ASIN",1,Asin);ce.RegisterFunction("ASINH",1,Asinh);ce.RegisterFunction("ATAN",1,Atan);ce.RegisterFunction("ATAN2",2,Atan2);ce.RegisterFunction("ATANH",1,Atanh);ce.RegisterFunction("CEILING",1,Ceiling);ce.RegisterFunction("COMBIN",2,Combin);ce.RegisterFunction("COS",1,Cos);ce.RegisterFunction("COSH",1,Cosh);ce.RegisterFunction("DEGREES",1,Degrees);ce.RegisterFunction("EVEN",1,Even);ce.RegisterFunction("EXP",1,Exp);ce.RegisterFunction("FACT",1,Fact);ce.RegisterFunction("FACTDOUBLE",1,FactDouble);ce.RegisterFunction("FLOOR",1,Floor);ce.RegisterFunction("GCD",1,255,Gcd);ce.RegisterFunction("INT",1,Int);ce.RegisterFunction("LCM",1,255,Lcm);ce.RegisterFunction("LN",1,Ln);ce.RegisterFunction("LOG",1,2,Log);ce.RegisterFunction("LOG10",1,Log10);ce.RegisterFunction("MDETERM",1,MDeterm);ce.RegisterFunction("MINVERSE",1,MInverse);ce.RegisterFunction("MMULT",2,MMult);ce.RegisterFunction("MOD",2,Mod);ce.RegisterFunction("MROUND",2,MRound);ce.RegisterFunction("MULTINOMIAL",1,255,Multinomial);ce.RegisterFunction("ODD",1,Odd);ce.RegisterFunction("PI",0,Pi);ce.RegisterFunction("POWER",2,Power);ce.RegisterFunction("PRODUCT",1,255,Product);ce.RegisterFunction("QUOTIENT",2,Quotient);ce.RegisterFunction("RADIANS",1,Radians);ce.RegisterFunction("RAND",0,Rand);ce.RegisterFunction("RANDBETWEEN",2,RandBetween);ce.RegisterFunction("ROMAN",1,2,Roman);ce.RegisterFunction("ROUND",2,Round);ce.RegisterFunction("ROUNDDOWN",2,RoundDown);ce.RegisterFunction("ROUNDUP",1,2,RoundUp);ce.RegisterFunction("SERIESSUM",4,SeriesSum);ce.RegisterFunction("SIGN",1,Sign);ce.RegisterFunction("SIN",1,Sin);ce.RegisterFunction("SINH",1,Sinh);ce.RegisterFunction("SQRT",1,Sqrt);ce.RegisterFunction("SQRTPI",1,SqrtPi);ce.RegisterFunction("SUBTOTAL",2,255,Subtotal);ce.RegisterFunction("SUM",1,int.MaxValue,Sum);ce.RegisterFunction("SUMIF",2,3,SumIf);ce.RegisterFunction("SUMSQ",1,255,SumSq);ce.RegisterFunction("TAN",1,Tan);ce.RegisterFunction("TANH",1,Tanh);ce.RegisterFunction("TRUNC",1,Trunc);ce.RegisterFunction("AVERAGE",1,int.MaxValue,Average);ce.RegisterFunction("AVERAGEA",1,int.MaxValue,AverageA);ce.RegisterFunction("COUNT",1,int.MaxValue,Count);ce.RegisterFunction("COUNTA",1,int.MaxValue,CountA);ce.RegisterFunction("COUNTBLANK",1,int.MaxValue,CountBlank);ce.RegisterFunction("COUNTIF",2,CountIf);ce.RegisterFunction("MAX",1,int.MaxValue,Max);ce.RegisterFunction("MAXA",1,int.MaxValue,MaxA);ce.RegisterFunction("MIN",1,int.MaxValue,Min);ce.RegisterFunction("MINA",1,int.MaxValue,MinA);ce.RegisterFunction("STDEV",1,int.MaxValue,StDev);ce.RegisterFunction("STDEVA",1,int.MaxValue,StDevA);ce.RegisterFunction("STDEVP",1,int.MaxValue,StDevP);ce.RegisterFunction("STDEVPA",1,int.MaxValue,StDevPA);ce.RegisterFunction("VAR",1,int.MaxValue,Var);ce.RegisterFunction("VARA",1,int.MaxValue,VarA);ce.RegisterFunction("VARP",1,int.MaxValue,VarP);ce.RegisterFunction("VARPA",1,int.MaxValue,VarPA);ce.RegisterFunction("ASC",1,Asc);// Changes full-width (double-byte) English letters or katakana within a character string to half-width (single-byte) charactersce.RegisterFunction("CHAR",1,_Char);// Returns the character specified by the code numberce.RegisterFunction("CLEAN",1,Clean);// Removes all nonprintable characters from textce.RegisterFunction("CODE",1,Code);// Returns a numeric code for the first character in a text stringce.RegisterFunction("CONCATENATE",1,int.MaxValue,Concat);// Joins several text items into one text itemce.RegisterFunction("DOLLAR",1,2,Dollar);// Converts a number to text, using the $ (dollar) currency formatce.RegisterFunction("EXACT",2,Exact);// Checks to see if two text values are identicalce.RegisterFunction("FIND",2,3,Find);//Finds one text value within another (case-sensitive)ce.RegisterFunction("FIXED",1,3,Fixed);// Formats a number as text with a fixed number of decimalsce.RegisterFunction("LEFT",1,2,Left);// LEFTB Returns the leftmost characters from a text valuece.RegisterFunction("LEN",1,Len);//, Returns the number of characters in a text stringce.RegisterFunction("LOWER",1,Lower);// Converts text to lowercasece.RegisterFunction("MID",3,Mid);// Returns a specific number of characters from a text string starting at the position you specifyce.RegisterFunction("PROPER",1,Proper);// Capitalizes the first letter in each word of a text valuece.RegisterFunction("REPLACE",4,Replace);// Replaces characters within textce.RegisterFunction("REPT",2,Rept);// Repeats text a given number of timesce.RegisterFunction("RIGHT",1,2,Right);// Returns the rightmost characters from a text valuece.RegisterFunction("SEARCH",2,Search);// Finds one text value within another (not case-sensitive)ce.RegisterFunction("SUBSTITUTE",3,4,Substitute);// Substitutes new text for old text in a text stringce.RegisterFunction("T",1,T);// Converts its arguments to textce.RegisterFunction("TEXT",2,_Text);// Formats a number and converts it to textce.RegisterFunction("TRIM",1,Trim);// Removes spaces from textce.RegisterFunction("UPPER",1,Upper);// Converts text to uppercasece.RegisterFunction("VALUE",1,Value);// Converts a text argument to a numberce.RegisterFunction("HYPERLINK",1,Hyperlink);
- How do I deliver an Excel file in ASP.NET?
- Does it support Excel 2003 and prior formats (.xls)?
- How can I insert an image?
- Text with numbers are getting converted to numbers, what's up with that?
- How do I get the result of a formula?
- Data Types
- Creating Multiple Worksheets
- Organizing Sheets
- Loading and Modifying Files
- Using Lambda Expressions
- Cell Values
- Workbook Properties
- Using Formulas
- Evaluating Formulas
- Creating Rows And Columns Outlines
- Hide Unhide Rows And Columns
- Freeze Panes
- Copying Worksheets
- Using Hyperlinks
- Data Validation
- Hide Worksheets
- Sheet Protection
- Tab Colors
- Conditional Formatting
- Pivot Table example
- Sparklines
- Copying IEnumerable Collections
- Inserting Data
- Inserting Tables
- Adding DataTable as Worksheet
- Adding DataSet
- Styles - Alignment
- Styles - Border
- Styles - Fill
- Styles - Font
- Styles - NumberFormat
- NumberFormatId Lookup Table
- Style Worksheet
- Style Rows and Columns
- Using Default Styles
- Using Colors
- ClosedXML Predefined Colors
- Excel Indexed Colors
- Using Rich Text
- Using Phonetics
- Defining Ranges
- Merging Cells
- Clearing Ranges
- Deleting Ranges
- Multiple Ranges
- Shifting Ranges
- Transpose Ranges
- Named Ranges
- Accessing Named Ranges
- Copying Ranges
- Using Tables
- Sorting Data
- Selecting Cells and Ranges
- Row Height and Styles
- Selecting Rows
- Inserting Rows
- Inserting and Deleting Rows
- Adjust Row Height and Column Width to Contents
- Row Cells
- Column Width and Styles
- Selecting Columns
- Inserting Columns
- Inserting and Deleting Columns
- Adjust Row Height and Column Width to Contents
- Column Cells
- Pages Tab
- Paper Size Lookup Table
- Margins Tab
- Headers and Footers Tab
- Sheet Tab
- Print Areas and Page Breaks