Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Create a Formula in Excel using Java?

Improve Article
Save Article
  • Last Updated : 06 Dec, 2022
Improve Article
Save Article

Apache POI is a popular open-source Java library that provides programmers with APIs for creating, modifying, and editing MS Office files. Excel is very excellent at calculating formulas. And perhaps most Excel documents have formulas embedded. Therefore, it’s trivial that on a fine day you have to deal with formulas when reading and writing Excel documents from Java. The good news is that the Apache POI library provides excellent support for working with formulas in Excel. 

Creating Formula in Excel using Java

We will use the Apache POI library in the project to write this article to share with you my experience in dealing with formulas in Excel files.


import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class GFG {
    public static void main(String[] args) throws Exception
        // Naming WorkBook
        String excelfilename = "GeeksForGeeks.xlsx";
        // Creating Workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        // Creating Spreadsheet
        XSSFSheet spreadsheet
            = workbook.createSheet("formula");
        // Create a row object using XSSFRow for creating
        // Row
        XSSFRow row;
        // Create a column object using XSSFCell for
        // creating Cell
        XSSFCell cell;
        // Creating row and start from 0 index value
        row = spreadsheet.createRow(1);
        // Creating column and start from 0 index value
        cell = row.createCell(1);
        // set Value of cell to apply formula
        cell.setCellValue("Value of X = ");
        cell = row.createCell(2);
        row = spreadsheet.createRow(2);
        cell = row.createCell(1);
        cell.setCellValue("Value of Y = ");
        cell = row.createCell(2);
        row = spreadsheet.createRow(3);
        cell = row.createCell(1);
        cell.setCellValue("SUM = ");
        cell = row.createCell(2);
        // Creating SUM formula
        cell = row.createCell(3);
        row = spreadsheet.createRow(4);
        cell = row.createCell(1);
        cell.setCellValue("POWER =");
        cell = row.createCell(2);
        // Create POWER formula
        cell = row.createCell(3);
        row = spreadsheet.createRow(5);
        cell = row.createCell(1);
        cell.setCellValue("MAX = ");
        cell = row.createCell(2);
        // Creating MAX formula
        cell = row.createCell(3);
        row = spreadsheet.createRow(6);
        cell = row.createCell(1);
        cell.setCellValue("ABS Value = ");
        cell = row.createCell(2);
        // Creating Absolute number formula
        cell = row.createCell(3);
        row = spreadsheet.createRow(7);
        cell = row.createCell(1);
        cell.setCellValue("PRODUCT = ");
        cell = row.createCell(2);
        // Creating SQRT formula
        cell = row.createCell(3);
        // This is a helpful wrapper around looping over
        // allcells, and calling evaluateFormulaCell on each
        // one.
        // To make Auto size column
        try {
            // Place the output file in default location and
            // also kept in try catch block
            FileOutputStream outputfile
                = new FileOutputStream(excelfilename);
            // Write to workbook
            // Close the output file
            // Display message for console window when
            // program is successfully executed
                               + " is written successfully");
        catch (FileNotFoundException e) {
            // Display error message for console window when
            // program is not successfully executed
            System.out.println("ERROR!! " + e.getMessage());
        finally {

Output: On the console window

  • When the program is successfully executed.

GeeksForGeeks.xlsx is written successfully

  •  When the program is not successfully executed.

ERROR!! GeeksForGeeks.xlsx (The process cannot access the file because it is being used by another process)

Output: Workbook(excel file)



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!