对execl的读写(批量创建execl文件)

# -*-coding:utf-8-*-
# Time:2017/9/21 19:02
# Author:YangYangJun


import os
import time

from openpyxl import Workbook


def writeExcel(tableValues, i):
    # 获取文件路径
    excelPath = os.path.join(os.getcwd(), 'ExcelData')
    if not os.path.exists(excelPath):
        os.makedirs(excelPath)
    nameTime = time.strftime('%Y-%m-%d_%H-%M-%S')
    excelName = 'Excel' + nameTime + str(i) + '.xlsx'
    ExcelFullName = os.path.join(excelPath, excelName)
    wb = Workbook()
    ws = wb.active
    tableTitle = ['userName', 'Phone', 'age', 'Remark']
    for col in range(len(tableTitle)):
        c = col + 1
        ws.cell(row=1, column=c).value = tableTitle[col]
    for row in range(len(tableValues)):
        ws.append(tableValues[row])
    # wb.save(ExcelFullName)
    wb.save(filename=ExcelFullName)
    return ExcelFullName


def createfile(num):
    temp = []
    for i in range(int(num)):
        tableValues = [['test', "test", str(i), '测试数据!']]
        temp.append(tableValues[0])
        ExcelFullName = writeExcel(temp, i)


if __name__ == '__main__':
    import datetime

    starttime = datetime.datetime.now()
    createfile(100)
    endtime = datetime.datetime.now()
    print(endtime - starttime)

 

posted @ 2018-06-25 16:59  银鑫  阅读(78)  评论(0编辑  收藏  举报