java使用poi对excel类型进行处理

版权声明:本文为博主原创文章,未经博主允许不得转载! https://blog.csdn.net/bojinyanfeng/article/details/85598760
POI 中的CellType类型取值说明
CellType常量 类型
CELL_TYPE_NUMERIC 数值型 0
CELL_TYPE_STRING 字符串型 1
CELL_TYPE_FORMULA 公式型 2
CELL_TYPE_BLANK 空值 3
CELL_TYPE_BOOLEAN 布尔型 4
CELL_TYPE_ERROR 错误 5

根据上述poi中的CellType类型进行单元格数值转换为java数值

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;
	}
}

猜你喜欢

转载自blog.csdn.net/bojinyanfeng/article/details/85598760