解决:Could not resolve external workbook name ‘xxx.xls’ Workbook environment has not been set up.
如果你用poi 解析Excel 时 遇到了这样一个错,这是说明在你的Excel 中,引用了别的Excel文件的内容,使用下面的获取的方法得以解决这类问题。
/**
* 读取单元格内容 包括计算公式的结果,引用公式的结果(引用公式值当前的sheet单元格,引用了另一个Excel文件的内容例:='C:\Users\Desktop\[测试引用.xlsx]Sheet1'!A3)
* @param cell
* @return
*/
public static String getCellValue(Cell cell){
System.out.println(cell);
String value = null;
if(cell != null){
System.out.println(cell.getCellType());
switch (cell.getCellType()){
case BLANK:
value = "";
break;
case BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
switch (cell.getCachedFormulaResultType()){
case NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
value = sdf.format(date);
}else{
BigDecimal n = new BigDecimal(cell.getNumericCellValue());
DecimalFormat decimalFormat = new DecimalFormat("0");
decimalFormat.setMaximumFractionDigits(18);
value = decimalFormat.format(n.doubleValue());
}
break;
case STRING:
value = String.valueOf(cell.getStringCellValue());
if(value != null){
value = value.trim();
}
break;
case BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR: value = "";
break;
default:
value = cell.getRichStringCellValue().getString();
break;
}
break;
case NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
value = sdf.format(date);
}else{
BigDecimal n = new BigDecimal(cell.getNumericCellValue());
DecimalFormat decimalFormat = new DecimalFormat("0");
decimalFormat.setMaximumFractionDigits(18);
value = decimalFormat.format(n.doubleValue());
}
break;
case STRING:
value = String.valueOf(cell.getStringCellValue());
if(value != null){
value = value.trim();
}
break;
default:
value = cell.getRichStringCellValue().getString();
break;
}
}
return value;
}