import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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
{
String excelfilename = "GeeksForGeeks.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet
= workbook.createSheet("formula");
XSSFRow row;
XSSFCell cell;
row = spreadsheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue("Value of X = ");
cell = row.createCell(2);
cell.setCellValue(5.0);
row = spreadsheet.createRow(2);
cell = row.createCell(1);
cell.setCellValue("Value of Y = ");
cell = row.createCell(2);
cell.setCellValue(10);
row = spreadsheet.createRow(3);
cell = row.createCell(1);
cell.setCellValue("SUM = ");
cell = row.createCell(2);
cell.setCellFormula("SUM(C2:C3)");
cell = row.createCell(3);
cell.setCellValue("SUM(C2:C3)");
row = spreadsheet.createRow(4);
cell = row.createCell(1);
cell.setCellValue("POWER =");
cell = row.createCell(2);
cell.setCellFormula("POWER(C2,C3)");
cell = row.createCell(3);
cell.setCellValue("POWER(C2,C3)");
row = spreadsheet.createRow(5);
cell = row.createCell(1);
cell.setCellValue("MAX = ");
cell = row.createCell(2);
cell.setCellFormula("MAX(C2,C3)");
cell = row.createCell(3);
cell.setCellValue("MAX(C2,C3)");
row = spreadsheet.createRow(6);
cell = row.createCell(1);
cell.setCellValue("ABS Value = ");
cell = row.createCell(2);
cell.setCellFormula("ABS(C2)");
cell = row.createCell(3);
cell.setCellValue("ABS(C2)");
row = spreadsheet.createRow(7);
cell = row.createCell(1);
cell.setCellValue("PRODUCT = ");
cell = row.createCell(2);
cell.setCellFormula("PRODUCT(C2,C3)");
cell = row.createCell(3);
cell.setCellValue("PRODUCT(C2,C3)");
workbook.getCreationHelper()
.createFormulaEvaluator()
.evaluateAll();
spreadsheet.autoSizeColumn(1);
spreadsheet.autoSizeColumn(2);
spreadsheet.autoSizeColumn(3);
try {
FileOutputStream outputfile
= new FileOutputStream(excelfilename);
workbook.write(outputfile);
outputfile.close();
System.out.println(excelfilename
+ " is written successfully");
}
catch (FileNotFoundException e) {
System.out.println("ERROR!! " + e.getMessage());
}
finally {
workbook.close();
}
}
}