查询不同sql,插入同一个sheet
import pymysql,xlwt,os
sql1='''SELECT
type,
count( * ) AS 激活数量,
sum( IF ( severity = 1, 1, 0 ) ) AS 1级数量,
sum( IF ( severity = 2, 1, 0 ) ) AS 2级数量,
sum( IF ( severity = 3, 1, 0 ) ) AS 3级数量,
sum( IF ( severity = 4, 1, 0 ) ) AS 4级数量
FROM
zt_bug
WHERE
product = '189'
AND STATUS = 'active'
GROUP BY
type'''
sql2='''SELECT
sum( IF ( date_format( openedDate, '%Y-%m-%d' ) = date_format( NOW( ), '%Y-%m-%d' ), 1, 0 ) ) AS 今天创建,
sum( IF ( date_format( resolvedDate, '%Y-%m-%d' ) = date_format( NOW( ), '%Y-%m-%d' ), 1, 0 ) ) AS 今天解决,
sum( IF ( date_format( closedDate, '%Y-%m-%d' ) = date_format( NOW( ), '%Y-%m-%d' ), 1, 0 ) ) AS 今天关闭
FROM
zt_bug
WHERE
product = '189'
'''
def export_excel(sql):
host,user,passwd,db='192.168.1.10','root','zentao_123','zentao'
coon=pymysql.connect(host=host,user=user,passwd=passwd,db=db,port=3306,charset='utf8')
cur=coon.cursor() #建立游标
#cur.execute(sql1)#执行sql1
cur.execute(sql)#执行sql2
fileds=[filed[0] for filed in cur.description]#所有字段
all_data=cur.fetchall()#所有值
return all_data
def write(table_name,fileds,all_data,fileds2,all_data2):
book=xlwt.Workbook()
sheet=book.add_sheet('sheet1')
for col,filed in enumerate(fileds):
sheet.write(0,col,filed)
row = 1
for data in all_data:
for index, datacol in enumerate(data): # 控制列
sheet.write(row, index, datacol)
row = row+ 1
row = row + 5
for col,filed in enumerate(fileds2):
sheet.write(row,col,filed)
for data in all_data2:
for index, datacol in enumerate(data): # 控制列
sheet.write(row+1, index, datacol)
row = row+ 1
book.save('%s.xls' %table_name)
# export_excel('zentao.zt_bug') # 导出excel
if __name__ == '__main__':
path=os.path.abspath(__name__)
print(path)
fileds=['type','激活数量','1级BUG数量','2级BUG数量','3级BUG数量','4级BUG数量']
fileds2 = ['当日创建', '当日解决', '当日关闭']
all_data=export_excel(sql1)
all_data2=export_excel(sql2)
write(1111,fileds,all_data,fileds2,all_data2)
# export_excel('zentao.zt_bug')
查询不同sql,插入同一个sheet
浙公网安备 33010602011771号