This chapter takes you through the process of applying different formulas on cells using Java programming. The basic purpose of Excel application is to maintain numerical data by applying formulas on it.

    In a formula, we pass dynamic values or locations of the values in the Excel sheet. On executing this formula, you get the desired result. The following table lists a few basic formulas that are frequently used in Excel.

    Operation Syntax
    Adding multiple numbers = SUM(Loc1:Locn) or = SUM(n1,n2,)
    Count = COUNT(Loc1:Locn) or = COUNT(n1,n2,)
    Power of two numbers = POWER(Loc1,Loc2) or = POWER(number, power)
    Max of multiple numbers = MAX(Loc1:Locn) or = MAX(n1,n2,)
    Product = PRODUCT(Loc1:Locn) or = PRODUCT(n1,n2,)
    Factorial = FACT(Locn) or = FACT(number)
    Absolute number = ABS(Locn) or = ABS(number)
    Today date =TODAY()
    Converts lowercase = LOWER(Locn) or = LOWER(text)
    Square root = SQRT(locn) or = SQRT(number)
    Operation Syntax
    Adding multiple numbers = SUM(Loc1:Locn) or = SUM(n1,n2,)
    Count = COUNT(Loc1:Locn) or = COUNT(n1,n2,)
    Power of two numbers = POWER(Loc1,Loc2) or = POWER(number, power)
    Max of multiple numbers = MAX(Loc1:Locn) or = MAX(n1,n2,)
    Product = PRODUCT(Loc1:Locn) or = PRODUCT(n1,n2,)
    Factorial = FACT(Locn) or = FACT(number)
    Absolute number = ABS(Locn) or = ABS(number)
    Today date =TODAY()
    Converts lowercase = LOWER(Locn) or = LOWER(text)
    Square root = SQRT(locn) or = SQRT(number)

    The following code is used to add formulas to a cell and execute it.

    1. import java.io.File;
    2. import java.io.FileOutputStream;
    3.  
    4. import org.apache.poi.xssf.usermodel.XSSFCell;
    5. import org.apache.poi.xssf.usermodel.XSSFRow;
    6. import org.apache.poi.xssf.usermodel.XSSFSheet;
    7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    8.  
    9. public class Formula {
    10. public static void main(String[] args)throws Exception {
    11. XSSFWorkbook workbook = new XSSFWorkbook();
    12. XSSFSheet spreadsheet = workbook.createSheet("formula");
    13. XSSFRow row = spreadsheet.createRow(1);
    14. XSSFCell cell = row.createCell(1);
    15.  
    16. cell.setCellValue("A = ");
    17. cell = row.createCell(2);
    18. cell.setCellValue(2);
    19. row = spreadsheet.createRow(2);
    20. cell = row.createCell(1);
    21. cell.setCellValue("B = ");
    22. cell = row.createCell(2);
    23. cell.setCellValue(4);
    24. row = spreadsheet.createRow(3);
    25. cell = row.createCell(1);
    26. cell.setCellValue("Total = ");
    27. cell = row.createCell(2);
    28.  
    29. // Create SUM formula
    30. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    31. cell.setCellFormula("SUM(C2:C3)");
    32. cell = row.createCell(3);
    33. cell.setCellValue("SUM(C2:C3)");
    34. row = spreadsheet.createRow(4);
    35. cell = row.createCell(1);
    36. cell.setCellValue("POWER =");
    37. cell=row.createCell(2);
    38.  
    39. // Create POWER formula
    40. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    41. cell.setCellFormula("POWER(C2,C3)");
    42. cell = row.createCell(3);
    43. cell.setCellValue("POWER(C2,C3)");
    44. row = spreadsheet.createRow(5);
    45. cell = row.createCell(1);
    46. cell.setCellValue("MAX = ");
    47. cell = row.createCell(2);
    48.  
    49. // Create MAX formula
    50. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    51. cell.setCellFormula("MAX(C2,C3)");
    52. cell = row.createCell(3);
    53. cell.setCellValue("MAX(C2,C3)");
    54. row = spreadsheet.createRow(6);
    55. cell = row.createCell(1);
    56. cell.setCellValue("FACT = ");
    57. cell = row.createCell(2);
    58.  
    59. // Create FACT formula
    60. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    61. cell.setCellFormula("FACT(C3)");
    62. cell = row.createCell(3);
    63. cell.setCellValue("FACT(C3)");
    64. row = spreadsheet.createRow(7);
    65. cell = row.createCell(1);
    66. cell.setCellValue("SQRT = ");
    67. cell = row.createCell(2);
    68.  
    69. // Create SQRT formula
    70. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    71. cell.setCellFormula("SQRT(C5)");
    72. cell = row.createCell(3);
    73. cell.setCellValue("SQRT(C5)");
    74. workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
    75. FileOutputStream out = new FileOutputStream(new File("formula.xlsx"));
    76. workbook.write(out);
    77. out.close();
    78. System.out.println("fromula.xlsx written successfully");
    79. }
    80. }

    Save the above code as Formula.java and then compile and execute it from the command prompt as follows.

    1. $javac Formula.java
    2. $java Formula

    It will generate an Excel file named formula.xlsx in your current directory and display the following output on the command prompt.

    1. fromula.xlsx written successfully

    The formula.xlsx file looks as follows.

    formula.jpeg