java poi读取excel数据怎么都是公式,怎么取值,谢谢帮忙
1个回答
展开全部
参考代码
public void getValueOfFormulaCell() throws IOException
{
FileInputStream xlsfile = new FileInputStream(new File("D:\\Desktop\\Temp\\marks.xls"));
HSSFWorkbook objWorkbook = new HSSFWorkbook(xlsfile);
Sheet sheet = objWorkbook.getSheetAt(0);
FormulaEvaluator evaluator = objWorkbook.getCreationHelper().createFormulaEvaluator();
// suppose your formula is in B4
CellReference cellReference = new CellReference("B4");
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;
}
}
public void getValueOfFormulaCell() throws IOException
{
FileInputStream xlsfile = new FileInputStream(new File("D:\\Desktop\\Temp\\marks.xls"));
HSSFWorkbook objWorkbook = new HSSFWorkbook(xlsfile);
Sheet sheet = objWorkbook.getSheetAt(0);
FormulaEvaluator evaluator = objWorkbook.getCreationHelper().createFormulaEvaluator();
// suppose your formula is in B4
CellReference cellReference = new CellReference("B4");
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;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |