javapoi读取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 = Row(0);
HSSFCell hcell = Cell(0);
String cellValue = CellValue(hcell);
System.out.println(cellValue);
}
public String getCellValue(HSSFCell cell) {
String value = null;
if (cell != null) {
switch (CellType()) {
java valueof
case HSSFCell.CELL_TYPE_FORMULA:
// CellFormula();
try {
value = String.NumericCellValue());
} catch (IllegalStateException e) {
value = String.RichStringCellValue());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = String.NumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
value = String.RichStringCellValue());
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();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。