Apache POI(5):公式(formula)

时间:2022-08-04 20:24:10
package com.hthk.iisz.util;

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 FormulaTest {

	public static void main(String[] args) throws Exception {
		formula();
	}

	public static void formula() throws Exception {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("formula");
		XSSFRow row = sheet.createRow(1);
		XSSFCell cell = row.createCell(1);
		cell.setCellValue("A =");
		cell = row.createCell(2);
		cell.setCellValue(2);
		row = sheet.createRow(2);
		cell = row.createCell(1);
		cell.setCellValue("B =");
		cell = row.createCell(2);
		cell.setCellValue(4);
		row = sheet.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 = sheet.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 = sheet.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 = sheet.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 = sheet.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");
	}

}

效果截图

Apache POI(5):公式(formula)