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. python成长之路——第六天

    定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...

  2. excel列显示形式互换(字母与数字)

    以office2007为例: excel选项>公式>使用公式下的'R1C1引用样式' 打上钩显示形式为数字,不打钩显示形式为字母

  3. 《算法导论》读书笔记之动态规划—最长公共子序列 & 最长公共子串(LCS)

    From:http://my.oschina.net/leejun2005/blog/117167 1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要 ...

  4. HDU 3974 Assign the task 简单搜索

    根据Rex 的思路才知道可以这么写. 题目意思还是很好理解的,就是找到当前雇员最近的任务. 做法是,可以开辟一个 tim 变量,每次有雇员得到昕任务时候 ++tim 然后取寻找最近的任务的时候写一个搜 ...

  5. The Hungarian algorithm Template

    The Hungarian algorithm with The adjacency matrix : 计算最大匹配问题 int n1, n2, m, ans; int res[MAXN]; bool ...

  6. Verilog中的标点

    在Verilog中有时候会误用的上引号 1,define 中的 `define INITIAL  0 这个单引号用的是键盘左上角的那个单引号,其实就是一个小撇. 2,4'd0 这个 用的是才是叫真正的 ...

  7. 在Eclipse/MyEclipse中安装spket插件

    Spket ide是强大的工具包为了JavaScript和XML的开发,这个强大的编辑器对JavaScript, XUL/XBLand Yahoo! Widget的开发都有全面的支持 ,比如代码完毕, ...

  8. VC++的内联汇编

    1.移植性差,假设是软件项目建议不要用.假设希望一段程序仅在单一设备上执行而且效率极高,能够使用内联汇编. 比方.单片机开发. 2. ......持续更新中. .... .

  9. Ganglia 权威指南-安装Ganglia过程

    转自于:http://blog.csdn.net/xxd851116/article/details/21527055 http://www.dataguru.cn/article-3816-1.ht ...

  10. MSSQL奇技淫巧

    MSSQL:获得库每个表的记录数和容量 sp_msforeachtable是MS未公开的存储过程: exec sp_msforeachtable @command1="print '?'&q ...