python自动生成excel报表

1.将SQL语句查询的内容,直接写入到excel报表中,以下为全部脚本。
要求:此版本必须运维在windows平台,并且安装了excel程序,excel版本不限。
   python版本为2.7
   if b    判断b是否为空值

在execl中,列和行都是以0开始
【0】 0 1 2 3
【1】 0   1    2   3

sql语句要求,如果sql语句的条件需要外部传入进去,那么sql语句必须用""号括起来
# -*- coding:utf-8 -*-
from xlwt import *
import xlrd
import pymysql

#建立mysql连接 conn
= pymysql.connect(host='127.0.0.1',user='root', passwd='1234', db='test', charset='utf8') cur = conn.cursor() def SQL(cur,sql): cur.execute(sql); return (cur);
#执行sql语句 a
=SQL(cur, r"select * from test;")

#打开一个execle文档 w
= Workbook(encoding='utf-8') ws= w.add_sheet(u"xls")
i
=1 f = ['id', '名字']
#通过循环,将列明插入进去。 g
= 0 for x in f: fnt = Font() style = XFStyle() style.font = fnt ws.write(0, g, x) ws.row(i).set_style(style) g = g + 1 try:
   #遍历sql语句查询到的内容
for b in a: fnt = Font() style = XFStyle() style.font = fnt for f in range(0,len(b)): ws.write(i, f, '%s' %b[f]) ws.row(i).set_style(style) i = i + 1 if b: w.save(u"测试.xls") except Exception as e: print(e) cur.close() conn.close()
 
 

 

 

转载于:https://www.cnblogs.com/GXLo/p/6178938.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值