import pymysql,xlwt
FI = pymysql.connect(
host=‘192.168.1.10‘,user=‘root‘,passwd=‘zentao_123‘,
port=3306,db=‘zentao‘,charset=‘utf8‘
#port必须写int类型,
#charset这里必须写utf8
)
cur = FI.cursor() #建立游标
sql = ‘‘‘select
case left(d.name,2) when ‘DD‘ then ‘调度‘ when ‘DW‘ then ‘电网‘ when ‘ZN‘ then ‘智能设备‘ when ‘DL‘ then ‘智能设备‘ WHEN ‘HB‘ then ‘华北‘ when ‘ZH‘ then‘综合能源‘ when ‘DS‘ then ‘大数据‘ when ‘XS‘ then ‘智能线损‘ else ‘研发中心‘ end as 所属部门,
d.name as 项目,
b.realname,
a.date,
a.consumed as 工时,
a.work
FROM zt_effort a
left join zt_user b on a.account=b.account
left join zt_dept c on b.dept=c.id
left join zt_project d on a.project=d.id
left join zt_task e on a.objectID=e.id
where c.id in (35,22,1)
and a.date >=‘20201226‘ and a.date <=‘20210625‘ and c.name=‘质量保障巴‘ and a.consumed>0
and b.realname in (‘赵博‘,‘郝楠‘,‘李青云‘,‘张红运‘,‘刘通慧‘,‘周蕊‘,‘于萍‘,‘郝楠‘,‘赵康‘)
and d.name not in (‘YFZX-技术研发中心公共管理工作‘,‘自动化测试脚本开发‘)
ORDER BY d.name,a.date‘‘‘
cur.execute(sql)
style = xlwt.XFStyle()
style.alignment.wrap = 1 #设置自动换行
book=xlwt.Workbook(encoding=‘utf-8‘)
sheet = book.add_sheet(‘调度‘)
sheet1=book.add_sheet(‘智能设备‘,cell_overwrite_ok=True)
font=xlwt.Font()
font.name=‘微软雅黑‘
font.height=22*11
sheet2=book.add_sheet(‘综合能源‘)
sheet3=book.add_sheet(‘智能线损‘)
sheet4=book.add_sheet(‘华北‘)
sheet5=book.add_sheet(‘大数据‘)
sheet6=book.add_sheet(‘研发中心‘)
fileds=[filed[0] for filed in cur.description]#获取字段表头
res = cur.fetchall() #获取所有返回的结果
for col, filed in enumerate(fileds):
# print(col, filed)
sheet.write(0, col, filed)
sheet1.write(0,col,filed)
sheet2.write(0,col,filed)
sheet3.write(0,col,filed)
sheet4.write(0,col,filed)
sheet5.write(0,col,filed)
sheet6.write(0,col,filed)
# book.savsheet1.write(0, col, filed)e(‘./user_info.xls‘)
dd=1
sb=1
zh=1
xs=1
hb=1
dsj=1
yf=1
for i in res:
bm=i[0]
bm=bm[0:4]
if bm==‘调度‘:
for j in range(len(i)):
sheet.write(dd, j, i[j])
dd+=1
if bm==‘智能设备‘:
for j in range(len(i)):
sheet1.write(sb,j,i[j])
import pymysql,xlwt FI = pymysql.connect( host=‘*‘,user=‘root‘,passwd=‘*‘, port=*,db=‘zentao‘,charset=‘utf8‘ #port必须写int类型, #charset这里必须写utf8 ) cur = FI.cursor() #建立游标 sql = ‘‘‘select case left(d.name,2) when ‘DD‘ then ‘调度‘ when ‘DW‘ then ‘电网‘ when ‘ZN‘ then ‘智能设备‘ when ‘DL‘ then ‘智能设备‘ WHEN ‘HB‘ then ‘华北‘ when ‘ZH‘ then‘综合能源‘ when ‘DS‘ then ‘大数据‘ when ‘XS‘ then ‘智能线损‘ else ‘研发中心‘ end as 所属部门, d.name as 项目, b.realname, a.date, a.consumed as 工时, a.work FROM zt_effort a left join zt_user b on a.account=b.account left join zt_dept c on b.dept=c.id left join zt_project d on a.project=d.id left join zt_task e on a.objectID=e.id where c.id in (35,22,1) and a.date >=‘20201226‘ and a.date <=‘20210625‘ and c.name=‘*‘ and a.consumed>0 and b.realname in (‘*‘) and d.name not in (‘*)‘‘‘ cur.execute(sql) style = xlwt.XFStyle() style.alignment.wrap = 1 #设置自动换行 book=xlwt.Workbook(encoding=‘utf-8‘) sheet = book.add_sheet(‘调度‘) sheet1=book.add_sheet(‘智能设备‘,cell_overwrite_ok=True) font=xlwt.Font() font.name=‘微软雅黑‘ font.height=22*11 sheet2=book.add_sheet(‘综合能源‘) sheet3=book.add_sheet(‘智能线损‘) sheet4=book.add_sheet(‘华北‘) sheet5=book.add_sheet(‘大数据‘) sheet6=book.add_sheet(‘研发中心‘) fileds=[filed[0] for filed in cur.description]#获取字段表头 res = cur.fetchall() #获取所有返回的结果 for col, filed in enumerate(fileds): # print(col, filed) sheet.write(0, col, filed) sheet1.write(0,col,filed) sheet2.write(0,col,filed) sheet3.write(0,col,filed) sheet4.write(0,col,filed) sheet5.write(0,col,filed) sheet6.write(0,col,filed) # book.savsheet1.write(0, col, filed)e(‘./user_info.xls‘) dd=1 sb=1 zh=1 xs=1 hb=1 dsj=1 yf=1 for i in res: bm=i[0] bm=bm[0:4] if bm==‘调度‘: for j in range(len(i)): sheet.write(dd, j, i[j]) dd+=1 if bm==‘智能设备‘: for j in range(len(i)): sheet1.write(sb,j,i[j]) sb+=1 if bm==‘综合能源‘: for j in range(len(i)): sheet2.write(zh,j,i[j]) zh+=1 if bm==‘智能线损‘: for j in range(len(i)): sheet3.write(xs,j,i[j]) xs+=1 if bm==‘华北‘: for j in range(len(i)): sheet4.write(hb,j,i[j]) hb+=1 if bm==‘大数据‘: for j in range(len(i)): sheet5.write(dsj,j,i[j]) dsj+=1 if bm==‘研发中心‘: for j in range(len(i)): sheet6.write(yf,j,i[j]) yf+=1 print(i[j]) book.save(r‘C:\Users\zr\Desktop\12.xls‘)
sb+=1
if bm==‘综合能源‘:
for j in range(len(i)):
sheet2.write(zh,j,i[j])
zh+=1
if bm==‘智能线损‘:
for j in range(len(i)):
sheet3.write(xs,j,i[j])
xs+=1
if bm==‘华北‘:
for j in range(len(i)):
sheet4.write(hb,j,i[j])
hb+=1
if bm==‘大数据‘:
for j in range(len(i)):
sheet5.write(dsj,j,i[j])
dsj+=1
if bm==‘研发中心‘:
for j in range(len(i)):
sheet6.write(yf,j,i[j])
yf+=1
print(i[j])
book.save(r‘C:\Users\zr\Desktop\12.xls‘)