Apache-poi-formula
提供:Dev Guides
Apache POI –フォーミュラ
この章では、Javaプログラミングを使用してセルにさまざまな数式を適用するプロセスについて説明します。 Excelアプリケーションの基本的な目的は、数式を適用して数値データを維持することです。
数式では、Excelシートの動的な値または値の場所を渡します。 この式を実行すると、目的の結果が得られます。 次の表に、Excelで頻繁に使用されるいくつかの基本的な数式を示します。
Operation | Syntax |
---|---|
Adding multiple numbers | = SUM(Loc1:Locn) *or *= SUM(n1,n2,) |
Count | = COUNT(Loc1:Locn)* or *= COUNT(n1,n2,) |
Power of two numbers | = POWER(Loc1,Loc2)* or *= POWER(number, power) |
Max of multiple numbers | = MAX(Loc1:Locn)* or *= MAX(n1,n2,) |
Product | = PRODUCT(Loc1:Locn)* or *= PRODUCT(n1,n2,) |
Factorial | = FACT(Locn)* or *= FACT(number) |
Absolute number | = ABS(Locn)* or *= ABS(number) |
Today date | =TODAY() |
Converts lowercase | = LOWER(Locn)* or *= LOWER(text) |
Square root | = 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* ファイルは次のようになります。