How to Create Formula Cell in Excel Sheet using Java and Apache POI?
Last Updated :
19 May, 2022
In the previous article, we have seen how to read data from the formula cell, here we are going to create the formula cell using Apache POI.
In this article, we are creating an Excel file with three columns consisting of values and the last column is the formula cell which is calculated from the other cells by defining a certain formula, here we are creating the formula cell by adding all other data in the cells.
Pre-requisites:
To work will these, we needed the following software in your systems.
- Make sure your system has Java, if not download the latest java SDK version here.
- Create the Maven project.
For creating the maven project refer to this article on how to create a selenium maven project with eclipse.
- Add the Apache POI dependency into the POM.xml file
Program for creating Formula cell in Excel using Apache POI:
Java
package GFG_Maven.GFG_MAven;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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 Geeks {
@Test
public void geekforgeeks() throws IOException{
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("Numbers");
XSSFRow row=sheet.createRow(0);
row.createCell(0).setCellValue(10);
row.createCell(1).setCellValue(10);
row.createCell(2).setCellValue(10);
row.createCell(3).setCellFormula("A1+B1+C1");
FileOutputStream file = new FileOutputStream("C:\\Users\\ADMIN\\Desktop\\calc.xlsx");
workbook.write(file);
file.close();
System.out.println("calc.xlsx file is created with Formula cell");
}
}
Code Explanation:
After creating the workbook, we have created the sheet in the workbook as "Numbers".
XSSFSheet sheet=workbook.createSheet("Numbers")
Then we have created the first row in that sheet by the createRow() method.
XSSFRow row=sheet.createRow(0);
After that, we have created the cells in the first row by row.createCell(cell number).setCellValue(value);
row.createCell(0).setCellValue(10);
Now, in the fourth cell, we are using the formula to calculate the cell value by using the setCellFormula(formula).
row.createCell(3).setCellFormula("A1+B1+C1");
In the end, we are going to write the values into the Excel sheet by creating a file output stream into the defined location
FileOutputStream file = new FileOutputStream("specify the location for creating the file");
Writing the cell values into the file by workbook.write(file),
Output:
After we run the code, we get the output and the Excel files are created.
Now we can notice that the Excel file is created in the defined location. I have given desktop location, so the file is created on my desktop
If you open the Excel file the cell values are created in the sheet with the formula cell.
Similar Reads
How to Read Data From Formula Cell in Excel Sheet using Selenium and Apache POI? The Apache POI is one of the most popular java libraries for selenium data-driven testing. It is mainly used for handling all types of Microsoft documents such as MS Word, and MS Excel. MS word and others. Apache POI is an open-source library and POI stands for "Poor Obfuscation Implementation". Get
2 min read
How to Create a Formula in Excel using Java? 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
3 min read
Creating Sheets in Excel File in Java using Apache POI Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, java doesnât provide built-in support for working with ex
3 min read
How to Fill Background Color of Cells in Excel using Java and Apache POI? Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, Java doesnât provide built-in support for working with ex
3 min read
How to Create Different Data Format of Cells in a Spreadsheet Using Java? Formatting in excel is a neat trick in excel which is used to change the appearance of the data represented in the worksheet, formatting can be done in multiple ways such as we can format the data of the cells by using the styles. By default, all worksheet cells are formatted with the General number
4 min read