break用法:循环的时候,循环一次即停止
import pymysql,xlwt FI = pymysql.connect( host='*',user='root',passwd='*', port=*,db='z*',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 ('*') ORDER BY d.name,a.date''' cur.execute(sql) book=xlwt.Workbook() sheet = 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) # book.save('./user_info.xls') n=1 for i in res: bm=i[0] bm=bm[0:4] if bm=='*': for j in range(len(i)): sheet.write(n, j, i[j]) print(n,j,i[j]) # break n+=1 book.save(r'C:UserszzDesktop11.xls')