from xlwt import *
'''对excel增加行'''
def writeRow(tableObj,rowNum,tmpList,xlsStyleDict={}):
for num,o in enumerate(tmpList):
if num in xlsStyleDict:
style = xlsStyleDict.get(num)
tableObj.write(rowNum,num,o,style)
else:
tableObj.write(rowNum,num,o)
'''@param fileName:文件名
@param dataDict:数据
dataDict格式 {"_sheetName":[u'a',u'b',u'c'],
"_sheetData":{"a":[['1','2','3'],['a','b','c']],
"b":[['11','22','33'],['aa','bb','cc']],
"c":[['111','222','333'],['aaa','bbb','ccc']]
},
"_sheetTitle":{"a":[[u'日期',u'visits数量',u'leads数量']]}
"_sheetStyle":{"a":{0:XFStyleobject,1:XFStyleobject}}
}
'''
def createExcelFile(fileName,dataDict):
if dataDict:
excelfile = Workbook()
sheetNameList = dataDict.get("_sheetName",[])
sheetDataDict = dataDict.get("_sheetData",{})
sheetTitleDict = dataDict.get("_sheetTitle",{})
sheetStyleDict = dataDict.get("_sheetStyle",{})
for sheetName in sheetNameList:
ws = excelfile.add_sheet(sheetName)
titleList = sheetTitleDict.get(sheetName,[])
styleDict = sheetStyleDict.get(sheetName,{})
dataList = sheetDataDict.get(sheetName,[])
rowIdx = 0
for title in titleList:
writeRow(ws,rowIdx,title)
rowIdx +=1
for data in dataList:
writeRow(ws,rowIdx,data,styleDict)
rowIdx +=1
excelfile.save(fileName)
阅读(6376) | 评论(0) | 转发(0) |