package com.bojinsoft;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
public class ExcelUtil {
//单元格类型转换
public static String cellvalue(Cell cell){
String cellValue="";
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
switch (cell.getCellType()) {
case 0:
if (DateUtil.isCellDateFormatted(cell)) {//判断单元格格式为日期格式,根据上述格式转换
cellValue = String.valueOf(fmt.format(cell.getDateCellValue()));
break;
} else {
//判断是否有小数点
if(cell.getNumericCellValue()%1==0){
//没有小数点的转为整数
cellValue =String.valueOf((long) cell.getNumericCellValue());
}else{
//有小数点的保留小数点
cellValue =String.valueOf(new Double(cell.getNumericCellValue()));
}
break;
}
case 1:
cellValue = cell.getStringCellValue();
break;
case 2:
try {
cellValue = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
cellValue = String.valueOf(cell.getRichStringCellValue());
}
break;
case 3:
cellValue ="";
break;
case 4:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case 5:
cellValue = "";
break;
}
return cellValue;
}
}