首页 > 代码库 > python xlwt 写excel文件
python xlwt 写excel文件
python 写excel文件,需要xlwt库。下载地址:https://pypi.python.org/pypi/xlwt/1.1.2
下载后修改扩展名为rar, 解压后安装:
安装成功后就可以引用了。如下代码:
# -*- coding: utf-8 -*-
import xlwt
‘‘‘往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引‘‘‘
def WriteSheetRow(sheet,rowValueList,rowIndex):
i = 0
for svalue in rowValueList:
strValue = http://www.mamicode.com/unicode(str(svalue),‘utf-8‘)
sheet.write(rowIndex,i,strValue)
i = i + 1
‘‘‘写excel文件‘‘‘
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet(‘sheet1‘)
headList = [‘标题1‘,‘标题2‘,‘标题3‘,‘标题4‘]
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex)
for i in xrange(1,11):
rowIndex = rowIndex + 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex)
wbk.save(excelFile)
#测试
save_Excel("D:\\test.xlsx")
结果如下:
python xlwt 写excel文件