<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
public class ExcelFormatUtil {
//单元格数据格式判断
public static String getValue(XSSFCell cell){
if (null != cell) {
switch (cell.getCellTypeEnum()) {
case NUMERIC: // 数字
if (DateUtil.isCellDateFormatted(cell)){
// 日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-
dd");
return sdf.format(cell.getDateCellValue());
}
// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计
数值
BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
return bd.toPlainString();
case STRING: // 字符串
return cell.getStringCellValue() + "";
case BOOLEAN: // Boolean
return cell.getBooleanCellValue() + "";
case FORMULA: // 公式
return cell.getCellFormula() + "";
case BLANK: // 空值
return "";
评论1