Excel Formula Example
This program demonstrates using formulas in Excel.
Problem Statement
Write a Java program that adds a formula to an Excel cell.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 4 | public class ExcelFormulaExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Excel Formula Example"); |
| 7 | Workbook wb=new XSSFWorkbook(); |
| 8 | Sheet sheet=wb.createSheet("EduinqData"); |
| 9 | Row row=sheet.createRow(0); |
| 10 | row.createCell(0).setCellValue(10); |
| 11 | row.createCell(1).setCellValue(20); |
| 12 | row.createCell(2).setCellFormula("A1+B1"); |
| 13 | try(FileOutputStream fos=new FileOutputStream("formula.xlsx")) { |
| 14 | wb.write(fos); |
| 15 | System.out.println("Eduinq Excel Formula Added Successfully"); |
| 16 | } catch(IOException e) { |
| 17 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 18 | } |
| 19 | } |
| 20 | } |
Program Output
Eduinq Excel Formula Example Eduinq Excel Formula Added Successfully
Explanation
Formulas are added using setCellFormula(). - Excel evaluates formula when opened. - Exception handling ensures safe operations. - In enterprise systems, formulas automate calculations. Conclusion: Shows adding formulas in Excel.