这里是封装成了一个函数
def calculate(date1, date2):
detal = date2-date1
#这里的时间为x day,xx:xx:xx的格式
detal = str(detal)
#拆分出天,其他为(xx:xx:xx)
if len(detal.split(","))<=1:
return str(detal)
day,other = detal.split(",")
#拆分出真正的天
day = day.split(",")[0]
#拆分出时分秒
other = other.split(":")
# 进行转换,将天转换为小时,再将之前的小时加过来
hours = int (other[0]) + int(day) *24
#最后替换成小时
other[0] = str(hours)
#返回最终结果
return “:” .join(other)
封装sql那一段我就不写了,我的第一个博客里有
需要导入的模块里也有
..........
heards = (xxxxxx,xxxxx,xxx)
#data也就是sql语句
data =query ("select * form xxxxxxx")
genexcel = tablib.Dataset(*data, headers=headers)
datajson = json.loads(genexcel.json)
xlsx = xlwt.Workbook(encoding='utf-8')
sheet = xlsx.add_sheet('sheet1', True)
index = 1
for row in data:
#自己根据自己的sql的时间的列数,我的这边时间在3,4列,字段名叫created,date
created,date = row[3],row[4]
sheet.write(index,0,1,label = calculate(date1, date2) )
index +=1
xlsx.save("1.xls")
基本上就是这了