- Apache POI Tutorial
- Apache POI - Home
- Apache POI - Overview
- Apache POI - Java Excel APIs
- Apache POI - Environment
- Apache POI - Core Classes
- Apache POI - Workbooks
- Apache POI - Spreadsheets
- Apache POI - Cells
- Apache POI - Fonts
- Apache POI - Formula
- Apache POI - Hyperlink
- Apache POI - Print Area
- Apache POI - Database
- Apache POI Resources
- Apache POI - Questions & Answers
- Apache POI - Quick Guide
- Apache POI - Useful Resources
- Apache POI - Discussion
Apache POI - 公式
本章将带您完成使用 Java 编程在单元格上应用不同公式的过程。Excel 应用程序的基本目的是通过应用公式来维护数值数据。
在公式中,我们传递动态值或 Excel 工作表中值的位置。执行此公式后,您将获得所需的结果。下表列出了 Excel 中常用的一些基本公式。
手术 | 句法 |
---|---|
添加多个数字 | = SUM(Loc1:Locn)或= SUM(n1,n2,) |
数数 | = COUNT(Loc1:Locn)或= COUNT(n1,n2,) |
两个数的幂 | = POWER(Loc1,Loc2)或= POWER(数字, 功率) |
最大多个数字 | = MAX(Loc1:Locn)或= MAX(n1,n2,) |
产品 | = 乘积(Loc1:Locn)或= 乘积(n1,n2,) |
阶乘 | = FACT(Locn)或= FACT(数字) |
绝对数 | = ABS(Locn)或= ABS(数字) |
今天日期 | =今天() |
转换为小写 | = LOWER(Locn)或= LOWER(text) |
平方根 | = SQRT(locn)或= SQRT(数字) |
以下代码用于向单元格添加公式并执行它。
import java.io.File; 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 Formula { public static void main(String[] args)throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook.createSheet("formula"); XSSFRow row = spreadsheet.createRow(1); XSSFCell cell = row.createCell(1); cell.setCellValue("A = "); cell = row.createCell(2); cell.setCellValue(2); row = spreadsheet.createRow(2); cell = row.createCell(1); cell.setCellValue("B = "); cell = row.createCell(2); cell.setCellValue(4); row = spreadsheet.createRow(3); cell = row.createCell(1); cell.setCellValue("Total = "); cell = row.createCell(2); // Create SUM formula 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); // Create POWER formula 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); // Create MAX formula cell.setCellFormula("MAX(C2,C3)"); cell = row.createCell(3); cell.setCellValue("MAX(C2,C3)"); row = spreadsheet.createRow(6); cell = row.createCell(1); cell.setCellValue("FACT = "); cell = row.createCell(2); // Create FACT formula cell.setCellFormula("FACT(C3)"); cell = row.createCell(3); cell.setCellValue("FACT(C3)"); row = spreadsheet.createRow(7); cell = row.createCell(1); cell.setCellValue("SQRT = "); cell = row.createCell(2); // Create SQRT formula cell.setCellFormula("SQRT(C5)"); cell = row.createCell(3); cell.setCellValue("SQRT(C5)"); workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); FileOutputStream out = new FileOutputStream(new File("formula.xlsx")); workbook.write(out); out.close(); System.out.println("fromula.xlsx written successfully"); } }
将上述代码保存为Formula.java,然后从命令提示符编译并执行它,如下所示 -
$javac Formula.java $java Formula
它将在当前目录中生成一个名为Formula.xlsx的 Excel 文件,并在命令提示符下显示以下输出。
fromula.xlsx written successfully
Formula.xlsx文件如下所示 -