python模块-操作excel(xlsxwriter)

xlsxwriter模块 主要用来生成excel表格,插入数据
每一个Excel数据文件从上至下分为三个层级的对象:workbook(一个excel文件)-sheet-cell(单元格)
# coding:utf-8
'''
@Created on 2019年5月28日
'''

import xlsxwriter
import datetime

curdate = datetime.datetime.now().strftime("%Y%m%d%H%M%S")


class getTestResult:
    # 获取创建excel标名所需参数
    def __init__(self, reportName):
        self.reportName = reportName
        self.date = curdate

    # 创建工作簿
    def createXlsx(self):
        self.xlsxname = './result/{}-{}.xlsx'.format(self.reportName, curdate)
        self.workbook = xlsxwriter.Workbook(self.xlsxname)

    # 创建工作表并添加固定表头
    def createSheet(self, worksheetname):
        self.worksheet = self.workbook.add_worksheet(worksheetname)
        self.worksheet.set_column('A:A', 50)  # 设置第一列宽度为50像素
        self.worksheet.set_column('B:D', 30)
        self.worksheet.write(0, 0, "统计名称")
        self.worksheet.write(0, 1, "页面展示值")
        self.worksheet.write(0, 2, "SQL统计值")
        self.worksheet.write(0, 3, "测试结果")

    # 写数据到worksheet里
    def writeData(self, itemname, displayvalue, sqlvalue, result, count):
        self.worksheet.write(count, 0, itemname)
        self.worksheet.write(count, 1, displayvalue)
        self.worksheet.write(count, 2, sqlvalue)
        self.worksheet.write(count, 3, result)

    def closexlsx(self):
        self.workbook.close()


if __name__ == "__main__":
    getTR = getTestResult('test')
    getTR.createXlsx()
    getTR.createSheet('按机构统计')
    getTR.writeData("机构患者就诊总人次", "es", "sql", "pass", 1)
    getTR.createSheet('按药师统计')
    getTR.writeData("药师患者就诊总人次", "es", "sql", "pass", 1)
    getTR.closexlsx()

 

posted @ 2019-08-21 17:59  爱打盹的猫猫  阅读(407)  评论(0编辑  收藏  举报