/** poi特殊日期格式:数字格式化成-yyyy年MM月dd日,格式
**/
private static ArrayList PoiDateList = new ArrayList() {
{
add("年");
add("月");
add("日");
}
};///
///获取XSSFRow的值(全部统一转成字符串)///
///
///
///
public static String GetValue(Row row, intindex) {
Cell rowCell=row.getCell(index);return rowCell == null ? "": GetValueByCellStyle(rowCell, rowCell.getCellType());
}///
///根据单元格的类型获取单元格的值///
///
///
///
public static String GetValueByCellStyle(Cell rowCell, introwCellType) {
String value= "";switch(rowCellType) {caseCell.CELL_TYPE_STRING:
value=rowCell.getStringCellValue();break;caseCell.CELL_TYPE_NUMERIC://获取单元格值的格式化信息
String dataFormat =rowCell.getCellStyle().getDataFormatString();//判断格式化信息中是否存在:年月日
AtomicReference isDate = new AtomicReference<>(false);if (!StringHelper.IsNullOrWhiteSpace(dataFormat))
PoiDateList.forEach(x-> isDate.set(isDate.get() ||dataFormat.contains(x)));if(DateUtil.isCellDateFormatted(rowCell)) {
value= new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
}else if(DateUtil.isCellInternalDateFormatted(rowCell)) {
value= new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
}//有些情况,时间搓?数字格式化显示为时间,不属于上面两种时间格式
else if (isDate.get()) {
value= new SimpleDateFormat("yyyy-MM-dd").format(rowCell.getDateCellValue());
}//有些情况,时间搓?数字格式化显示为时间,不属于上面两种时间格式
else if (dataFormat == null) {
value= new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
}else{if(StringHelper.IsNullOrWhiteSpace(dataFormat)) {
value=String.valueOf(rowCell.getNumericCellValue());
}else{if (rowCell.getCellStyle().getDataFormatString().contains("$")) {
value= "$" +rowCell.getNumericCellValue();
}else if (rowCell.getCellStyle().getDataFormatString().contains("¥")) {
value= "¥" +rowCell.getNumericCellValue();
}else if (rowCell.getCellStyle().getDataFormatString().contains("¥")) {
value= "¥" +rowCell.getNumericCellValue();
}else if (rowCell.getCellStyle().getDataFormatString().contains("€")) {
value= "€" +String.valueOf(rowCell.getNumericCellValue());
}else{
value=String.valueOf(rowCell.getNumericCellValue());
}
}
}break;caseCell.CELL_TYPE_BOOLEAN:
value=String.valueOf(rowCell.getBooleanCellValue());break;caseCell.CELL_TYPE_ERROR:
value=ErrorEval.getText(rowCell.getErrorCellValue());break;caseCell.CELL_TYPE_FORMULA://TODO: 是否存在 嵌套 公式类型
value =GetValueByCellStyle(rowCell, rowCell.getCachedFormulaResultType());break;default:
System.out.println(rowCell);break;
}returnvalue;
}