全部博文(2065)
分类: Python/Ruby
2010-08-13 11:20:25
Python操作excel方法
时间:
下载地址:
这里提一个小例子:
from pyExcelerator import *
w = Workbook() #创建一个工作簿,相当于是建立一个库
ws = w.add_sheet('Hey, Hades') #创建一个工作表
ws.write(0,0,'bit') #在1行1列写入bit
ws.write(0,1,'huang') #在1行2列写入huang
ws.write(1,0,'xuan') #在2行1列写入xuan
w.save('mini.xls') #保存
2.如何把一段SQL的结果集保存到EXCEL中呢?
w = Workbook()
ws = w.add_sheet('serverinfo')
conn_local = MySQLdb.connect("xxxxxxxxx","xxx","xxx","xxx",port=3306,connect_timeout=10,compress=True)
cur_local = conn_local.cursor()
cur_local.execute("""select
b.years,b.months,b.weeks from serverweek
b where b.years='2010' and b.months='08' and b.weeks='1' order by
b.type""")
row = cur_local.fetchall()
ws.write(0,0,u'年份')
ws.write(0,1,u'月份')
ws.write(0,2,u'第几周')
for i in range(len(row)):
years = row[i][0]
months = row[i][1]
weeks = row[i][2]
ws.write(i+1, 0, str(years))
ws.write(i+1, 1, str(months))
ws.write(i+1, 2, str(weeks))
w.save('mini.xls')
3. 如何插入单元格?
w = Workbook()
ws = w.add_sheet(u’这是中文’) 创建好一个工作表对象
ws.write(i, j, value) 往这个单元格中写入数据
4. 如何设置单元格格式
font0 = Font()
font0.name = '宋体'
font0.struck_out = False
font0.bold = True
style0 = XFStyle()
style0.font = font0
ws.write(0,0,u'年份',style0) 赋上样式进来