python之excel导出

excel操作导出excelManager.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlwt
def generate_excel(rows,titleRow=[],filename='1.xls',sheetName="基本信息"):

    wbk = xlwt.Workbook(encoding='utf-8')
    sheet = wbk.add_sheet(sheetname=sheetName)
    i = 0;
    titleRow_len =len(titleRow);
    if titleRow_len <= 0:
        for key in rows[0]:
            titleRow.append({key:key});
    for col in range(titleRow_len):
        sheet.write(i,col,titleRow[col].values()[0])
    for row in rows:
        i=i+1;
        for col in range(titleRow_len):
           value = row[titleRow[col].keys()[0]]
           if value is None:
               value = '';
           sheet.write(i, col,value )
    wbk.save(unicode(filename, 'utf-8'));
    print "生成excel文件:",filename

相关推荐