NutzCN Logo
短点 poi date_43f29065
发布于 2965天前 作者 明天会吹什么风 1312 次浏览 复制 上一个帖子 下一个帖子
标签:

查看完整内容

```public static String readCellValues(HSSFCell cell) throws Exception {
// 用于返回结果
String result = new String();

try {
    // 如果单元格为空,返回null
    if (cell == null) {
        result = "null";
    }
    else {
        // 判断单元格类型
        switch (cell.getCellType()) { 
        // 数字类型
        case HSSFCell.CELL_TYPE_NUMERIC:  
            // 处理日期格式、时间格式
            if (HSSFDateUtil.isCellDateFormatted(cell)) {  
                SimpleDateFormat sdf = null;  
                if (cell.getCellStyle().getDataFormat() == HSSFDataFormat  
                        .getBuiltinFormat("h:mm")) {  
                    sdf = new SimpleDateFormat("HH:mm");  
                } else {// 日期  
                    sdf = new SimpleDateFormat("yyyy-MM-dd");  
                }  
                Date date = cell.getDateCellValue();  
                result = sdf.format(date);  
            } else if (cell.getCellStyle().getDataFormat() == 58) {  
                // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)  
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                double value = cell.getNumericCellValue();  
                Date date = org.apache.poi.ss.usermodel.DateUtil  
                        .getJavaDate(value);  
                result = sdf.format(date);  
            } else {  
                double value = cell.getNumericCellValue();  
                CellStyle style = cell.getCellStyle();  
                DecimalFormat format = new DecimalFormat();  
                String temp = style.getDataFormatString();  
                // 单元格设置成常规  
                if (temp.equals("General")) {  
                    format.applyPattern("#");  
                }  
                result = format.format(value);  
            }  
            break;  
        case HSSFCell.CELL_TYPE_STRING:// String类型  
            result = cell.getStringCellValue();  
            break;  
        case HSSFCell.CELL_TYPE_BLANK:  
            result = "";  
        default:  
            result = "";  
            break;  
        }  
    }
}
catch(Exception e) {
    e.printStackTrace();
}

return result;

}```

0 回复
添加回复
请先登陆
回到顶部