POI 操作Excel公式

2019-04-24 09:30|来源: 网路

本章将介绍如何使用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


相关问答

更多
  • 首先回答你第一个问题: 读取注解代码如下: Comment comment = sheet.getRow(7).getCell(5, Row.CREATE_NULL_AS_BLANK).getCellComment(); 根据批注定位单元格做不到。。。
  • java poi导出excel[2022-02-23]

    在不同的系统下字符集的编码有可能不一样,windows系统中从程序到底层是从Unicode编码进行转换,Unix系统下就不一定是从Unicode编码开始转换的,支不支持Unicode编码我也不知道,我估计就是系统的字符集编码不同的问题
  • 网上好多啊 我给你找了一个例子 view plaincopy to clipboardprint? public static void main(String[] args) { try { String filepath = "d:\\问题清单.xls"; FileInputStream fis = new FileInputStream(filepath); // POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("d:\\OA ...
  • 获取公式值可用 HSSFFormulaEvaluator e= New HSSFFormularEvaluator(workbook); e.evaluate(cell).getNumberiValue. 如果不是你要的结果。还有其它很多方法。 比如这样一个比较笨的方法。 double value = cell.getNumericCellValue(); value = value * 24 *3600; int h = (int) (value /3600) int m= (int )((value - ...
  • // 创建工作簿 Workbook workBook = new HSSFWorkbook(); // 创建工作表 Sheet sheet = workBook.createSheet("new"); // 创建行,Excel中的第一行在poi中索引为0 Row row = sheet.createRow(0); // 创建单元格,Excel中的第一列在poi中索引为0 Cell cell = row.createCell(0); // 设置单元格内容 cell.setCellValue("行号/列号"); ...
  • JAVA POI操作EXCEL[2022-07-01]

    读入数据写入一个List,失败的放一个List。 在用poi输出失败记录的List
  • poi版本无所谓,主要是看jasperreports的,因为它向下不兼容。系统稳定的情况下最好不要更换jasperreport。
  • 我相信您要查找的术语是命名范围 (尽管它们在某些版本的Excel中的功能区的“公式”选项卡中定义)。 假设你是这个意思: 然后,Apache POI需要的关键方法是: Workbook.getNumberOfNames() Workbook.getName(字符串) Workbook.getNameAt(INT) Workbook.createName() 如果你知道范围的名称,并想要得到它所指的单元格,你就会做类似的事情 Workbook wb = WorkbookFactory.create(new F ...
  • Excel文件格式只有几个基本类型,如Numeric和String。 大多数数字都存储为浮点值+格式规则。 这就是为什么你得到一个双倍而不是一个int - 这就是生活在文件中的原因! Apache POI提供DataFormatter类 ,它接受浮点值和格式字符串,并尽力返回Excel中显示的数字字符串。 对于你的情况,调用它可能会产生你想要的东西。 The Excel file format only has a few basic types, like Numeric and String. Most ...
  • 看到你想要使用POI的实际代码。 这里有一些做一些出口: import java.util.Date; import java.util.List; import java.util.ListIterator; import java.util.StringTokenizer; import java.io.*; import org.apache.poi.hssf.usermodel.*; public class XLSExporter implements Exporter { /** ...