excel中的数据:

package poi;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; public class TestReadFormula {
private static FormulaEvaluator evaluator;
public static void main(String[] args) throws IOException {
InputStream is=new FileInputStream("ReadFormula.xls");
HSSFWorkbook wb=new HSSFWorkbook(is);
Sheet sheet=wb.getSheetAt(0); evaluator=wb.getCreationHelper().createFormulaEvaluator(); for (int i = 1; i <4; i++) {
Row row=sheet.getRow(i);
for (Cell cell : row) {
System.out.println(getCellValue(cell));
}
}
wb.close(); } private static String getCellValue(Cell cell) {
if (cell==null) {
return "isNull";
}
System.out.println("rowIdx:"+cell.getRowIndex()+",colIdx:"+cell.getColumnIndex());
String cellValue = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print("STRING :");
cellValue=cell.getStringCellValue();
break; case Cell.CELL_TYPE_NUMERIC:
System.out.print("NUMERIC:");
cellValue=String.valueOf(cell.getNumericCellValue());
break; case Cell.CELL_TYPE_FORMULA:
System.out.print("FORMULA:");
cellValue=getCellValue(evaluator.evaluate(cell));
break;
default:
System.out.println("Has Default.");
break;
} return cellValue;
} private static String getCellValue(CellValue cell) {
String cellValue = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print("String :");
cellValue=cell.getStringValue();
break; case Cell.CELL_TYPE_NUMERIC:
System.out.print("NUMERIC:");
cellValue=String.valueOf(cell.getNumberValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print("FORMULA:");
break;
default:
break;
} return cellValue;
} }

Output:

rowIdx:1,colIdx:0
STRING :begin
rowIdx:1,colIdx:1
STRING :end
rowIdx:1,colIdx:2
FORMULA:String :beginend
rowIdx:2,colIdx:0
NUMERIC:1.0
rowIdx:2,colIdx:1
NUMERIC:3.0
rowIdx:2,colIdx:2
FORMULA:String :13
rowIdx:3,colIdx:0
NUMERIC:1.0
rowIdx:3,colIdx:1
NUMERIC:3.0
rowIdx:3,colIdx:2
FORMULA:NUMERIC:4.0

Formula Evaluation:

User API How-TO

The following code demonstrates how to use the FormulaEvaluator in the context of other POI excel reading code.

There are several ways in which you can use the FormulaEvalutator API.

Using FormulaEvaluator.evaluate(Cell cell)

This evaluates a given cell, and returns the new value, without affecting the cell

FileInputStream fis = new FileInputStream("c:/temp/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("c:/temp/test.xls")
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); // suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cellValue.getBooleanValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cellValue.getNumberValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cellValue.getStringValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break; // CELL_TYPE_FORMULA will never happen
case Cell.CELL_TYPE_FORMULA:
break;
}

Thus using the retrieved value (of type FormulaEvaluator.CellValue - a nested class) returned by FormulaEvaluator is similar to using a Cell object containing the value of the formula evaluation. CellValue is a simple value object and does not maintain reference to the original cell.

Using FormulaEvaluator.evaluateFormulaCell(Cell cell)

evaluateFormulaCell(Cell cell) will check to see if the supplied cell is a formula cell. If it isn't, then no changes will be made to it. If it is, then the formula is evaluated. The value for the formula is saved alongside it, to be displayed in excel. The formula remains in the cell, just with a new value

The return of the function is the type of the formula result, such as Cell.CELL_TYPE_BOOLEAN

FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); // suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); if (cell!=null) {
switch (evaluator.evaluateFormulaCell(cell)) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
System.out.println(cell.getErrorCellValue());
break; // CELL_TYPE_FORMULA will never occur
case Cell.CELL_TYPE_FORMULA:
break;
}
}

Using FormulaEvaluator.evaluateInCell(Cell cell)

evaluateInCell(Cell cell) will check to see if the supplied cell is a formula cell. If it isn't, then no changes will be made to it. If it is, then the formula is evaluated, and the new value saved into the cell, in place of the old formula.

FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); // suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); if (cell!=null) {
switch (evaluator.evaluateInCell(cell).getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
System.out.println(cell.getErrorCellValue());
break; // CELL_TYPE_FORMULA will never occur
case Cell.CELL_TYPE_FORMULA:
break;
}
}

Re-calculating all formulas in a Workbook

FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
Sheet sheet = wb.getSheetAt(sheetNum);
for(Row r : sheet) {
for(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
evaluator.evaluateFormulaCell(c);
}
}
}
}

Alternately, if you know which of HSSF or XSSF you're working with, then you can call the static evaluateAllFormulaCells method on the appropriate HSSFFormulaEvaluator or XSSFFormulaEvaluator class.

http://poi.apache.org/spreadsheet/eval.html

POI读取公式的值的更多相关文章

  1. POI单元格添加公式以及读取公式结果的值

    POI提供了为单元格添加条件样式的方法,但是我并没有找到获取单元格改变后样式的方法,获取到样式依旧是没有改变之前的. 比如为单元格添加条件样式用于监听单元格值是否被修改,如果单元格值被修改那么字体颜色 ...

  2. java用poi读取Excel表格中的数据

    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版.Apache POI 代 ...

  3. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

  4. 使用poi读取excel数据示例

    使用poi读取excel数据示例 分两种情况: 一种读取指定单元格的值 另一种是读取整行的值 依赖包: <dependency> <groupId>org.apache.poi ...

  5. NPOI 读取excel到DataTable 读取隐藏列 读取公式列

    处理思路: 1.打开excel 用NPOI进行读取: 2.读取第一个Sheet: 读取过程中: a.先设置相应列 不隐藏 b.读取Cell时 先判断是否的包含公式 相应代码如下: public sta ...

  6. POI读取/写入Excel文件

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  7. jspsmart(保存文件)+poi(读取excel文件)操作excel文件

    写在前面: 项目环境:jdk1.4+weblogic 需求:能上传excel2003+2007 由于项目不仅需要上传excel2003,还要上传excel2007,故我们抛弃了jxl(只能上传exce ...

  8. POI读取excel文件。

    1) poi读取现成.xls文件,不需要自己建立.xls ====ReadExcel​类​==== package cust.com.excelToDataTest; import java.io.F ...

  9. poi读取Excel模板并修改模板内容与动态的增加行

    有时候我们可能遇到相当复杂的excel,比如表头的合并等操作,一种简单的方式就是直接代码合并(浪费时间),另一种就是写好模板,动态的向模板中增加行和修改指定单元格数据. 1.一个简单的根据模板shee ...

随机推荐

  1. tabbar动画切换

    效果1: UIViewController *vc = self.viewControllers[self.selectedIndex]; CATransition *animation =[CATr ...

  2. javaCore分析示例(转)

    当两个或多个线程彼此形成循环依赖关系时,就出现了死锁.例如,如果线程 A 处于等待线程 B 的等待状态,而同时线程 B 处于等待线程 A 的等待状态,则出现了死锁.一旦形成此情况,线程 A 和线程 B ...

  3. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  4. 全栈project师的悲与欢

    从小米辞职出来创业的两个多月里,通过猎头或自己投简历,先后面试了知乎,今日头条,豌豆荚,美团,百度,App Annie,去哪儿,滴滴打车等技术团队,一二面(技术面)差点儿都轻松的过了,三面却没有毕业那 ...

  5. Javascript DOM 01 基础篇

    DOM基础   DOM是什么        答:文件对象模型(Document Object Model,简称DOM),DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构!来自网络 ...

  6. 新浪微博中tableview中头部信息

    摘自http://www.cnblogs.com/gcb999/p/3151665.html #import <UIKit/UIKit.h> @class User; @protocol ...

  7. [ASP.NET]以iTextSharp手绘表格并产生PDF下载

    原文 [ASP.NET]以iTextSharp手繪表格並產生PDF下載 大家使用iTextSharp的機緣都不太一樣, 由於單位Crystal Report的License數量有限主管要我去找一個免費 ...

  8. POJ 3986 Math teacher's homework

    题目 给出\(n,m_1,m_2,...,m_n\),求\(x_1 xor x_2 xor ... xor x_n=k (0 \leq x_i \leq m_i)\)的解的数量.二进制位数小于\(32 ...

  9. DNS:因特网的目录服务

    作者:华科小涛,http://www.cnblogs.com/hust-ghtao/ 有两种方式来识别主机:通过主机名或IP地址.人们当然喜欢便于记忆的主机名,而路由器则喜欢定长的.有层次结构的IP地 ...

  10. Win32 进程间通信的分析与比较(13种方法)

    1 进程与进程通信 进程是装入内存并准备执行的程序,每个进程都有私有的虚拟地址空间,由代码.数据以及它可利用的系统资源(如文件.管道等)组成.多进程/多线 程是Windows操作系统的一个基本特征.M ...