package com.glaf;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
public class PoiUtils {
private static NumberFormat nf = NumberFormat.getNumberInstance();
static {nf.setGroupingUsed(false);}
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
// 判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: // 数字
if(DateUtil.isCellDateFormatted(cell)){//日期类型
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
cellValue = format.format(cell.getDateCellValue());
}else{
cellValue = nf.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_STRING: // 字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = nf.format(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: // 公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue.trim();
}
}
POI读取单元格值工具类(解决纯数字读取多带.0)
最新推荐文章于 2024-11-06 09:45:56 发布