import logging,os
from sqlserver import sqlserver
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.utils import get_column_letter
from openpyxl.reader.excel import load_workbook

class HandleExcel():
    def __init__(self):
        self.head_row_labels = ['序号', '统一社会信用代码', '企业名称', '公司地址', '企业法人代表']

    def write_to_excel_with_openpyxl(self, records, head_row, save_excel_name="save.xlsx"):
        # 新建一个workbook
        wb = Workbook()
        # 新建一个excelWriter
        #ew = ExcelWriter(workbook=wb)
        # 设置文件输出路径与名称
        dest_filename = save_excel_name
        # 第一个sheet是ws
        ws = wb.worksheets[0]
        # 设置ws的名称
        ws.title = "企业"
        # 写第一行,标题行
        for h_x in range(1, len(head_row) + 1):
            ws.cell(1,h_x,head_row[h_x - 1])
        for row in range(2,len(records)+2):
            for col in range(1, len(head_row) + 1):
                ws.cell(row, col, records[row-2][col-1])
        # 写文件
        wb.save(filename=dest_filename)

 

posted on 2020-07-03 13:35  二豆  阅读(870)  评论(0编辑  收藏  举报