python 生成Excel文件
最近用python做定时发送统计报表的功能,其中用到了生成excel文件,生成简单的excel文件代码如下。#!/usr/bin/python# coding:utf8import xlsxwriterimport sys# python3import importlibimportlib.reload(sys)# python2# reload(sys)# sys.set...
·
最近用python做定时发送统计报表的功能,其中用到了生成excel文件,生成简单的excel文件代码如下。
#!/usr/bin/python
# coding:utf8
import xlsxwriter
import sys
# python3
import importlib
importlib.reload(sys)
# python2
# reload(sys)
# sys.setdefaultencoding("utf-8")
class Excel(object):
# 初始化,设置文件名
def __init__(self, name):
self.book = xlsxwriter.Workbook(name)
self.sheet = self.book.add_worksheet()
# 写入列名
def write_colume_name(self, colums_name):
for i in range(0, len(colums_name)):
self.sheet.write(0, i, colums_name[i])
# 写入数据
def write_content(self, row_num, data):
for i in range(0, len(data)):
self.sheet.write(row_num, i, data[i])
# 关闭文件
def close(self):
self.book.close()
if __name__ == '__main__':
filename = 'test_order.xlsx'
colums_name = ['学号', '姓名', '性别']
book = Excel(filename)
book.write_colume_name(colums_name)
data = ['20180228', 'Mike', '男']
book.write_content(1, data)
book.close()
print('finish')
更多推荐
已为社区贡献2条内容
所有评论(0)