java poi读取excel公式,返回计算值(转)
Posted linxixinxiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi读取excel公式,返回计算值(转)相关的知识,希望对你有一定的参考价值。
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.*; public class FormulaToString /** * @param args */ public void fileInput() throws IOException HSSFWorkbook hw = new HSSFWorkbook(new FileInputStream( "d:/My Documents/Desktop/poi.xls")); HSSFSheet hsheet = hw.getSheet("poi test"); HSSFRow hrow = hsheet.getRow(0); HSSFCell hcell = hrow.getCell(0); String cellValue = this.getCellValue(hcell); System.out.println(cellValue); public String getCellValue(HSSFCell cell) String value = null; if (cell != null) switch (cell.getCellType()) case HSSFCell.CELL_TYPE_FORMULA: // cell.getCellFormula(); try value = String.valueOf(cell.getNumericCellValue()); catch (IllegalStateException e) value = String.valueOf(cell.getRichStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: value = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: value = String.valueOf(cell.getRichStringCellValue()); break; return value; public static void main(String[] args) try // TODO Auto-generated method stub FormulaToString fts = new FormulaToString(); fts.fileInput(); catch (IOException e) e.printStackTrace();
以上是关于java poi读取excel公式,返回计算值(转)的主要内容,如果未能解决你的问题,请参考以下文章
excel表格中,lookup函数返回值是公式要怎么设置??
使用 Apache POI 从 Java 中的 Excel 中读取公式字段