python写入excel(方式1)

import xlsxwriter


li=["张三","李四","王五","周六","王琪","谈吧",5,6,3,4]
# todo 创建excel文件
xl = xlsxwriter.Workbook(r'test.xlsx')
# todo 添加sheet
sheet = xl.add_worksheet('sheet1')
sheet.write_string("A1","编号")#写入开头行
sheet.write_string("B1","姓名")#写入开头列
x=1
for i in range(len(li)):
x+=1#A的序号是从X+1开始进行编号
y=x-1#A的内容编号从1开始,所以需要减1,另外记住x是增加了1的
sheet.write_string("A%d" % x,"%d" % y)
sheet.write_string("B%d" % x, "%s" % li[i])
# # todo 设置单元格宽度大小
sheet.set_column('A:B', 50)
# todo 关闭文件
xl.close()

 

猜你喜欢

转载自www.cnblogs.com/wyx1990/p/12014457.html
今日推荐