工作总结 npoi 模板 导出公式 excel
Apache POI(5):公式(formula)
Apache POI(5):公式(formula)
-
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");
-
}
-
-
}
效果截图
sheet.ForceFormulaRecalculation =
true
;
1.使用cell.setCellFormula方法重新在制定Cell里写入公式。
2.使用sheet.setForceFormulaRecalculation(true);方法强制让改Sheet执行公式。
EvaluateFormulaCell
#region 插入块
int rowsindex = 0;
for (int i = 0; i < lastarea.Count - 1; i++)
{
int tj = rowsindex + 70;
for (; rowsindex < tj; rowsindex++)
{
sheet.CreateRow(rowsindex + 70); //有些行为null 统一创建一下
sheet.CopyRow(rowsindex, rowsindex + 70);
}
}
#endregion
//统计
if ((HSSFCell)sheet.GetRow(dataarea + 40).GetCell(3) == null) //一个道理 为空 创建
{
ICell cell = (HSSFCell)sheet.GetRow(dataarea + 40).CreateCell(3);
}
((HSSFCell)sheet.GetRow(dataarea + 40).GetCell(3)).SetCellType(CellType.FORMULA); //设置表达式格式
//((HSSFRow)sheet.GetRow(dataarea + 40)).GetCell(3).SetCellValue("=SUM(D" + (dataarea + 1) + ":D" + (dataarea + 40) + ")");
((HSSFRow)sheet.GetRow(dataarea + 40)).GetCell(3).SetCellFormula("SUM(D" + (dataarea + 1) + ":D" + (dataarea + 40) + ")"); //设置表达式的值 不用=