作者: shaoliang 2020-06-04 04:30   访问数:1110
近期接到任务,完成山东省胃癌上报
两个不关联的excel表格
首先用phymyadmin 或者mysqlfront 导入到mysql数据库
使用pycharm 进行操作
先安装xlwt见下
贴代码
import pymysql import xlwt conn = pymysql.connect("localhost", "root", "root", "digestive", charset='utf8') # 使用cursor()方法获取操作游标 cursor = conn.cursor() # cursor.execute("SELECT * FROM `gastroscope` LEFT JOIN pathology ON gastroscope.病人号=pathology.病人编号 WHERE gastroscope.送病理='是' ") cursor.execute("SELECT * FROM `gastroscope` LEFT JOIN pathology ON gastroscope.病人号=pathology.病人编号") # cursor.execute("SELECT gastroscope.姓名 FROM `gastroscope` LEFT JOIN pathology ON gastroscope.病人号=pathology.病人编号 WHERE gastroscope.送病理='是'") # cursor.execute("SELECT * FROM `gastroscope`") result = cursor.fetchall() # 获取字段 fields=cursor.description # 显示字段 print(fields) # 关闭游标 cursor.close() # 关闭数据 conn.close() #将查询结果写入到excel workbook = xlwt.Workbook() #创建一个新的sheet sheet=workbook.add_sheet('sheet1',cell_overwrite_ok=True) #将表的字段名写入excel 这个地方比较懵逼 for field in range(len(fields)): sheet.write(0,field,fields[field][0]) #结果写入excle for row in range(1,len(result)+1): for col in range(len(fields)): sheet.write(row,col,result[row-1][col]) #excel保存为文件 workbook.save(r'd:\3.xls') # python牛的一笔