JAVA导入EXCEL文件:ReadExcelXlsxUtil.java解决对文件的兼容性处理(自主封装)

package AFT.utils;

import java.sql.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;

/**
 * 2003-2007Excel文件读取单元格数据校验
 * @author zwb
 *
 */
public class ReadExcelXlsxUtil {
	private static String STRCELL;
    /*
     * 需要存入的对象为String类型
     */
    public static String getStringCellvalue (Cell cell) {
    	STRCELL = null;
    	if (cell == null) {
            return null;
        } else {
    		switch (cell.getCellType()) {
    		case HSSFCell.CELL_TYPE_STRING:
    			STRCELL = cell.getStringCellValue();
    		     break;
    		case HSSFCell.CELL_TYPE_NUMERIC:
    			STRCELL = String.valueOf(cell.getNumericCellValue());
    		     break;
    		case HSSFCell.CELL_TYPE_BOOLEAN:
    			STRCELL = String.valueOf(cell.getBooleanCellValue());
    		     break;
    		case HSSFCell.CELL_TYPE_BLANK:
    			STRCELL = null;
    		     break;
    		default:
    			STRCELL = null;
    		     break;
    		}
    		if (StringUtils.isEmpty(STRCELL)) {
    			return null;
    		}
    		return STRCELL;
        }
    }
    
    
    /*
     * 需要存入的对象为Date类型
     */
    public static Date getDateCellValue (Cell cell) {
    	Date date = null;
    	int cellType;
    	if (cell == null) {
    		return null;
    	} else {
    		try {
                cellType = cell.getCellType();
                if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                    date = new Date(cell.getDateCellValue().getTime());
                } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
                    date = Date.valueOf(getStringCellvalue(cell));
                } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
                	date = null;
                }
            } catch (Exception e) {
                System.out.println("单元格输入的内容不正确!");
                e.printStackTrace();
            }
            return date;
    	}
    }
    
    
    /*
     * 需要存入的类型为Double类型
     */
//    public static Double getDoubleCellValue(HSSFCell cell) {
//    	Double value;
//    	if (cell == null) {
//    		return null;
//    	} else {
//    		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
//    		value = Double.parseDouble(cell.getStringCellValue());
//    		try {
//	    		int cellType = cell.getCellType();
//	            if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
//	            	value = null;
//	            } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
//	            	value = Double.parseDouble(cell.getStringCellValue());
//	            	return null;
//	            } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
//	            	value = null;
//	            }
//    		} catch (Exception e) {
//    			System.out.println("单元格输入的内容不正确!");
//    			e.printStackTrace();
//    		}
//            return null;
//    	}
//    	return value;
//    	
//    }
    
    /*
     * 需要存入的类型为Integer类型
     */
    public static Integer getIntegerCellValue (Cell cell) {
    	Integer value = null;
    	if (cell == null) {
    		value = null;
    	} else {
    		try {
    			int cellType = cell.getCellType();
	            if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
	            	value = null;
	            } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
	            	value = Integer.parseInt(getStringCellvalue(cell));
	            } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
	            	value = null;
	            }
    			return value;
    		} catch (Exception e) {
    			System.out.println("单元格输入的内容不正确!");
    			e.printStackTrace();
    		}
    	}
    	return value;
    }
    
    /*
     * 需要存入的对象为String类型
     */
    public static String getStringCellvalue (XSSFCell cell) {
    	String strCell = "";
    	if (cell == null) {
            return "";
        } else {
    		switch (cell.getCellType()) {
    		case XSSFCell.CELL_TYPE_STRING:
    		     strCell = cell.getStringCellValue();
    		     break;
    		case XSSFCell.CELL_TYPE_NUMERIC:
    		     strCell = String.valueOf(cell.getNumericCellValue());
    		     break;
    		case XSSFCell.CELL_TYPE_BOOLEAN:
    		     strCell = String.valueOf(cell.getBooleanCellValue());
    		     break;
    		case XSSFCell.CELL_TYPE_BLANK:
    		     strCell = "";
    		     break;
    		default:
    		     strCell = "";
    		     break;
    		}
    		if (StringUtils.isEmpty(strCell)) {
    			return "";
    		}
    		return strCell;
        }
    }
    
    
    /*
     * 需要存入的对象为Date类型
     */
    public static Date getDateCellValue (XSSFCell cell) {
    	Date date = null;
    	if (cell == null) {
    		return null;
    	} else {
    		try {
    			
                int cellType = cell.getCellType();
                if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {
                    date = new Date(cell.getDateCellValue().getTime());
                } else if (cellType == XSSFCell.CELL_TYPE_STRING) {
                    date = Date.valueOf(getStringCellvalue(cell));
                } else if (cellType == XSSFCell.CELL_TYPE_BLANK) {
                	date = null;
                }
            } catch (Exception e) {
                System.out.println("单元格输入的内容不正确!");
                e.printStackTrace();
            }
            return date;
    	}
    }
    
    
    /*
     * 需要存入的类型为Double类型
     */
//    public static Double getDoubleCellValue(HSSFCell cell) {
//    	Double value;
//    	if (cell == null) {
//    		return null;
//    	} else {
//    		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
//    		value = Double.parseDouble(cell.getStringCellValue());
//    		try {
//	    		int cellType = cell.getCellType();
//	            if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
//	            	value = null;
//	            } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
//	            	value = Double.parseDouble(cell.getStringCellValue());
//	            	return null;
//	            } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
//	            	value = null;
//	            }
//    		} catch (Exception e) {
//    			System.out.println("单元格输入的内容不正确!");
//    			e.printStackTrace();
//    		}
//            return null;
//    	}
//    	return value;
//    	
//    }
    
    /*
     * 需要存入的类型为Integer类型
     */
    public static Integer getIntegerCellValue (XSSFCell cell) {
    	Integer value = null;
    	if (cell == null) {
    		value = null;
    	} else {
    		try {
    			int cellType = cell.getCellType();
	            if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {
	            	value = null;
	            } else if (cellType == XSSFCell.CELL_TYPE_STRING) {
	            	value = Integer.parseInt(getStringCellvalue(cell));
	            } else if (cellType == XSSFCell.CELL_TYPE_BLANK) {
	            	value = null;
	            }
    			return value;
    		} catch (Exception e) {
    			System.out.println("单元格输入的内容不正确!");
    			e.printStackTrace();
    		}
    	}
    	return value;
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度没有我的爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值