Data type processing tool class when importing into Excel

When processing the function of importing into Excel, you need to take the data strictly according to the data type in Excel when taking the value in Excel; therefore, a function class written for this function is as follows:

/**
 * Parse Excel file public class	
 * @author zhh
 *@date December 12, 2017 at 3:01:57 PM
 *
 */
public  class ExcelTool {
	/**
	 * Get data based on data type in Excel cell
	 * @param cell
	 * @return
	 */
	public  static String getExcelValue(HSSFCell cell){
		String value;
		switch (cell.getCellType()) {
	      case HSSFCell.CELL_TYPE_NUMERIC: // 数字
	          //if the content in time format
	          if (HSSFDateUtil.isCellDateFormatted(cell)) {      
	             //Note: The hour in the format format yyyy-MM-dd hh:mm:ss is a 12-hour format. If you want a 24-hour format, you can change the small h to H, yyyy-MM-dd HH:mm:ss
	             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
	             value=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();                                 
	               break;
	           } else {
	               value = new DecimalFormat("0").format(cell.getNumericCellValue());
	           }
	          break;
	      case HSSFCell.CELL_TYPE_STRING: // string
	          value = cell.getStringCellValue();
	          break;
	      case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
	          value = cell.getBooleanCellValue() + "";
	          break;
	      case HSSFCell.CELL_TYPE_FORMULA: // 公式
	          value = cell.getCellFormula() + "";
	          break;
	      case HSSFCell.CELL_TYPE_BLANK: // 空值
	          value = "";
	          break;
	      case HSSFCell.CELL_TYPE_ERROR: // 故障
	          value = "Illegal character";
	          break;
	      default:
	          value = "Unknown type";
	          break;
	 }
		return value;
	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326070129&siteId=291194637