#-*- utf:8 -*-
import xlsxwriter
# 新建一个Excel文件
workbook = xlsxwriter.Workbook(u'd:\interfaceresult.xlsx')
# 新建一个worksheet
worksheet = workbook.add_worksheet()
title = [u'测试用例',u'测试结果']
# 向'A1'单元格写入title列表,列表中每一个字段对应一列内容
worksheet.write_row(0,0,title)
t =[u'数据1',u'数据2']
worksheet.write_row(1,0,t) #write_row(行,列,t)
workbook.close()
封装代码:
import xlsxwriter
class SaveFile(object):
def __init__(self,sheetname): #sheetname 页签名称
self.sheetname = sheetname
self.workbook = None
self.worksheet = None
def create(self):
workbook = xlsxwriter.Workbook(u'd:\interfaceresult.xlsx')
worksheet = workbook.add_worksheet(self.sheetname)
self.workbook = workbook
self.worksheet = worksheet
title = [u'测试用例',u'测试结果']
worksheet.write_row(0,0,title)
return True
def write_info(self,row,col,t):
self.worksheet.write_row(row,col,t)
print(self.worksheet,t)
return True
def bk_close(self):
self.workbook.close()
return True
if __name__ == '__main__':
s = SaveFile('case')
s.create()
d = {"空['test1', 'test2', 'test3', 'test4']":1}
col = 0 #列
row = 1 #行
for k,v in d.items():
s.write_info(row,col,[k, v])
row +=1
s.bk_close()