-
Notifications
You must be signed in to change notification settings - Fork 892
Using Formulas
Francois Botha edited this page Feb 26, 2018
·
3 revisions
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Formulas");
ws.Cell(1, 1).Value = "Num1";
ws.Cell(1, 2).Value = "Num2";
ws.Cell(1, 3).Value = "Total";
ws.Cell(1, 4).Value = "cell.FormulaA1";
ws.Cell(1, 5).Value = "cell.FormulaR1C1";
ws.Cell(1, 6).Value = "cell.Value";
ws.Cell(1, 7).Value = "Are Equal?";
ws.Cell(2, 1).Value = 1;
ws.Cell(2, 2).Value = 2;
var cellWithFormulaA1 = ws.Cell(2, 3);
// Use A1 notation
cellWithFormulaA1.FormulaA1 = "=A2+$B$2"; // The equal sign (=) in a formula is optional
ws.Cell(2, 4).Value = cellWithFormulaA1.FormulaA1;
ws.Cell(2, 5).Value = cellWithFormulaA1.FormulaR1C1;
ws.Cell(2, 6).Value = cellWithFormulaA1.Value;
ws.Cell(3, 1).Value = 1;
ws.Cell(3, 2).Value = 2;
var cellWithFormulaR1C1 = ws.Cell(3, 3);
// Use R1C1 notation
cellWithFormulaR1C1.FormulaR1C1 = "RC[-2]+R3C2"; // The equal sign (=) in a formula is optional
ws.Cell(3, 4).Value = cellWithFormulaR1C1.FormulaA1;
ws.Cell(3, 5).Value = cellWithFormulaR1C1.FormulaR1C1;
ws.Cell(3, 6).Value = cellWithFormulaR1C1.Value;
ws.Cell(4, 1).Value = "A";
ws.Cell(4, 2).Value = "B";
var cellWithStringFormula = ws.Cell(4, 3);
// Use R1C1 notation
cellWithStringFormula.FormulaR1C1 = "=\"Test\" & RC[-2] & \"R3C2\"";
ws.Cell(4, 4).Value = cellWithStringFormula.FormulaA1;
ws.Cell(4, 5).Value = cellWithStringFormula.FormulaR1C1;
ws.Cell(4, 6).Value = cellWithStringFormula.Value;
// Setting the formula of a range
var rngData = ws.Range(2, 1, 4, 7);
rngData.LastColumn().FormulaR1C1 = "=IF(RC[-3]=RC[-1],\"Yes\", \"No\")";
// Using an array formula:
// Just put the formula between curly braces
ws.Cell("A6").Value = "Array Formula: ";
ws.Cell("B6").FormulaA1 = "{A2+A3}";
ws.Range(1, 1, 1, 7).Style.Fill.BackgroundColor = XLColor.Cyan;
ws.Range(1, 1, 1, 7).Style.Font.Bold = true;
ws.Columns().AdjustToContents();
// You can also change the reference notation:
wb.ReferenceStyle = XLReferenceStyle.R1C1;
// And the workbook calculation mode:
wb.CalculateMode = XLCalculateMode.Auto;
wb.SaveAs("Formulas.xlsx");
- 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