python处理excel的时间格式_python 读取execl的时如何还原日期格式?!excle读取日期格式...

python 读取execl的时如何还原日期格式?

用pyExcelerator还是xlrd读的excel?如果是xlrd那它自带一个xldate_as_tuple,可以转成datetime,比如:

from datetime import datetime

from xlrd import xldate_as_tuple

d=datetime(*xldate_as_tuple(x,0))#x你那串数字的变量

如果是用pyExcelerator,那好象它没似的函数,可以用下面这个函数:

def xldate_as_datetime(xldate, datemode=0):

if datemode not in (0, 1):

raise XLDateBadDatemode(datemode)

if xldate == 0.00:

return datetime.time(0, 0, 0)

if xldate < 0.00:

raise XLDateNegative(xldate)

xldays = int(xldate)

frac = xldate - xldays

seconds = int(round(frac * 86400.0))

assert 0 <= seconds <= 86400

if seconds == 86400:

seconds = 0

xldays = 1

#if xldays >= _XLDAYS_TOO_LARGE[datemode]:

# raise XLDateTooLarge(xldate)

if xldays == 0:

# second = seconds % 60; minutes = seconds // 60

minutes, second = divmod(seconds, 60)

# minute = minutes % 60; hour = minutes // 60

hour, minute = divmod(minutes, 60)

return datetime.time(hour, minute, second)

if xldays < 61 and datemode == 0:

raise XLDateAmbiguous(xldate)

return (

datetime.datetime.fromordinal(xldays 693594 1462 * datemode)

datetime.timedelta(seconds=seconds)

)

用法:

d=xldate_as_datetime(x)#x就是你那串数字的变量

java读取excel时间格式出现数字怎么处理

java读取excel间格式出现数字的处理方法:

Excel存储日期、时间均以数型进行存储,读POI先判断是是否是数值类型,再进行判断转化

1、数值格式(CELL_TYPE_NUMERIC):

1.纯数值格式:getNumericCellValue() 直接获取数据

2.日期格式:处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式

1).判断是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)

2).判断是日期或者时间

cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")

OR:cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")

3.自定义日期格式:处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式

判断cell.getCellStyle().getDataFormat()值,解析数值格式

yyyy年m月d日----->31

m月d日---->58

h时mm分--->32

举例说明:

private String parseExcel(Cell cell) {

String result = new String();

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.getRichStringCellValue().toString();

break;

case HSSFCell.CELL_TYPE_BLANK:

result = "";

default:

result = "";

break;

}

return result;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值