/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/Formulas.cs

# · C# · 120 lines · 65 code · 36 blank · 19 comment · 0 complexity · 9c30ccd3a2a2c1e3077f13c968b7e50a MD5 · raw file

  1. using System;
  2. using ClosedXML.Excel;
  3. namespace ClosedXML_Examples.Misc
  4. {
  5. public class Formulas : IXLExample
  6. {
  7. #region Variables
  8. // Public
  9. // Private
  10. #endregion
  11. #region Properties
  12. // Public
  13. // Private
  14. // Override
  15. #endregion
  16. #region Events
  17. // Public
  18. // Private
  19. // Override
  20. #endregion
  21. #region Methods
  22. // Public
  23. public void Create(String filePath)
  24. {
  25. var wb = new XLWorkbook();
  26. var ws = wb.Worksheets.Add("Formulas");
  27. ws.Cell(1, 1).Value = "Num1";
  28. ws.Cell(1, 2).Value = "Num2";
  29. ws.Cell(1, 3).Value = "Total";
  30. ws.Cell(1, 4).Value = "cell.FormulaA1";
  31. ws.Cell(1, 5).Value = "cell.FormulaR1C1";
  32. ws.Cell(1, 6).Value = "cell.Value";
  33. ws.Cell(1, 7).Value = "Are Equal?";
  34. ws.Cell(2, 1).Value = 1;
  35. ws.Cell(2, 2).Value = 2;
  36. var cellWithFormulaA1 = ws.Cell(2, 3);
  37. // Use A1 notation
  38. cellWithFormulaA1.FormulaA1 = "=A2+$B$2"; // The equal sign (=) in a formula is optional
  39. ws.Cell(2, 4).Value = cellWithFormulaA1.FormulaA1;
  40. ws.Cell(2, 5).Value = cellWithFormulaA1.FormulaR1C1;
  41. ws.Cell(2, 6).Value = cellWithFormulaA1.Value;
  42. ws.Cell(3, 1).Value = 1;
  43. ws.Cell(3, 2).Value = 2;
  44. var cellWithFormulaR1C1 = ws.Cell(3, 3);
  45. // Use R1C1 notation
  46. cellWithFormulaR1C1.FormulaR1C1 = "RC[-2]+R3C2"; // The equal sign (=) in a formula is optional
  47. ws.Cell(3, 4).Value = cellWithFormulaR1C1.FormulaA1;
  48. ws.Cell(3, 5).Value = cellWithFormulaR1C1.FormulaR1C1;
  49. ws.Cell(3, 6).Value = cellWithFormulaR1C1.Value;
  50. ws.Cell(4, 1).Value = "A";
  51. ws.Cell(4, 2).Value = "B";
  52. var cellWithStringFormula = ws.Cell(4, 3);
  53. // Use R1C1 notation
  54. cellWithStringFormula.FormulaR1C1 = "=\"Test\" & RC[-2] & \"R3C2\"";
  55. ws.Cell(4, 4).Value = cellWithStringFormula.FormulaA1;
  56. ws.Cell(4, 5).Value = cellWithStringFormula.FormulaR1C1;
  57. ws.Cell(4, 6).Value = cellWithStringFormula.Value;
  58. // Setting the formula of a range
  59. var rngData = ws.Range(2, 1, 4, 7);
  60. rngData.LastColumn().FormulaR1C1 = "=IF(RC[-3]=RC[-1],\"Yes\", \"No\")";
  61. // Using an array formula:
  62. // Just put the formula between curly braces
  63. ws.Cell("A6").Value = "Array Formula: ";
  64. ws.Cell("B6").FormulaA1 = "{A2+A3}";
  65. ws.Range(1, 1, 1, 7).Style.Fill.BackgroundColor = XLColor.Cyan;
  66. ws.Range(1, 1, 1, 7).Style.Font.Bold = true;
  67. ws.Columns().AdjustToContents();
  68. // You can also change the reference notation:
  69. wb.ReferenceStyle = XLReferenceStyle.R1C1;
  70. // And the workbook calculation mode:
  71. wb.CalculateMode = XLCalculateMode.Auto;
  72. ws.Range("A10").AddToNamed("A10_R1C1_A10_R1C1");
  73. ws.Cell("A10").Value = 0;
  74. ws.Cell("A11").FormulaA1 = "A2 + A10_R1C1_A10_R1C1";
  75. ws.Cell("A12").FormulaR1C1 = "R2C1 + A10_R1C1_A10_R1C1";
  76. ws.Cell("A13").FormulaR1C1 = "=SUM(R[-5]:R[-4])";
  77. ws.Cell("A14").FormulaA1 = "=SUM(8:9)";
  78. wb.SaveAs(filePath);
  79. }
  80. // Private
  81. // Override
  82. #endregion
  83. }
  84. }