码迷,mamicode.com
首页 > 编程语言 > 详细

python xlwt 写excel文件

时间:2016-09-08 13:00:36      阅读:864      评论:0      收藏:0      [点我收藏+]

标签:

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 = 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文件

标签:

原文地址:http://www.cnblogs.com/shaosks/p/5852413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!