Apache POI公式


本章將介紹如何使用Java程式設計應用不同單元公式的過程。 Excel應用程式的基本目的是通過應用公式就可以保持數值資料。

在公式中,我們通過動態值,或在Excel工作表中的值的位置。在執行這個公式,就會得到想要的結果。下表列出了常用的在Excel中的幾個基本公式。

操作 語法
新增多個數值 = SUM(Loc1:Locn) or = SUM(n1,n2,)
計數 = COUNT(Loc1:Locn) or = COUNT(n1,n2,)
兩個數的冪 = POWER(Loc1,Loc2) or = POWER(number, power)
多個數的最大值 = MAX(Loc1:Locn) or = MAX(n1,n2,)
乘積 = PRODUCT(Loc1:Locn) or = PRODUCT(n1,n2,)
階乘 = FACT(Locn) or = FACT(number)
絕對數位 = ABS(Locn) or = ABS(number)
今天的日期 =TODAY()
轉換成小寫 = LOWER(Locn) or = LOWER(text)
平方根 = SQRT(locn) or = SQRT(number)

以下程式碼用於公式新增至單元格,並執行它。

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.setCellType(XSSFCell.CELL_TYPE_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.setCellType(XSSFCell.CELL_TYPE_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.setCellType(XSSFCell.CELL_TYPE_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.setCellType(XSSFCell.CELL_TYPE_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.setCellType(XSSFCell.CELL_TYPE_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檔案如下所示。

Formula