Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4953274
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: Python/Ruby

2010-07-29 13:39:42

Python代码
  1. #!/usr/bin/env python  
  2. # -*- coding:utf8  -*-  
  3. import xlrd   
  4.   
  5.   
  6. class WorkBook():  
  7.     def __init__(self):  
  8.         pass  
  9.     def __del__(self):  
  10.         pass  
  11.     def readExcel(self,excelFile):  
  12.         book = xlrd.open_workbook(excelFile)  
  13.         return book  
  14.     def readSheet(self,sheet):          
  15.         lifiled = sheet.col(2)[5:]  #字段  
  16.         litype = sheet.col(4)[5:]   #类型  
  17.         lilength = sheet.col(5)[5:] #长度  
  18.         linull = sheet.col(6)[5:]   #是否为空  
  19.         #print "++++++++++++++++++++++++++++++++++++"  
  20.          
  21.         print "CREATE TABLE DamsDB.dbo.%s" %(sheet.name,)  
  22.       
  23.         print "("  
  24.         for i in range(0,(sheet.nrows - 5)):  
  25.             if lilength[i].value != "":  
  26.                 if linull[i].value == "":  
  27.                     print "    %s %s (%s)," %(lifiled[i].value,litype[i].value,lilength[i].value)  
  28.                 else:  
  29.                      print "    %s %s (%s) %s," %(lifiled[i].value,litype[i].value,lilength[i].value,"NOT NULL")  
  30.             else:  
  31.                 if linull[i].value != "":  
  32.                     print "    %s %s %s," %(lifiled[i].value,litype[i].value,linull[i].value)  
  33.                 else:  
  34.                      print "    %s %s," %(lifiled[i].value,litype[i].value)  
  35.         print ")"  
  36.         print "GO"  
  37.         print "\n"  
  38.         #print "++++++++++++++++++++++++++++++++++++"  
  39.   
  40.   
  41. if __name__ == "__main__":  
  42.     workbook = WorkBook()  
  43.     book = workbook.readExcel("d:\\a.xls")  
  44.     print "The number of worksheets is", book.nsheets  
  45.       
  46.     for i in range(3, book.nsheets):  
  47.         #print "+++++++++++ i ==",i  
  48.         workbook.readSheet(book.sheet_by_index(i))  

  • (181.6 KB)
  • 下载次数: 47
阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~