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公式,返回计算值(转)
点赞
收藏