需要软件
1. xlrd
官方资料:
下载地址:http://pypi.python.org/pypi/xlrd
官网地址:http://www.python-excel.org/
文档地址:https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html
文档pdf下载:http://www.simplistix.co.uk/presentations/python-excel.pdf
2. mysqldb
示例代码:
#encoding: utf-8
'''
Created on 2012-9-4
将Excel导入到数据库
@author: 蓝桥书生
'''
import MySQLdb
import xlrd
import datetime
def insert(cursor,sql):
print cursor.execute(sql)
if __name__ == '__main__':
db = MySQLdb.connect(user='root', db='tinnonjscm', passwd='root', host='localhost')
cursor = db.cursor()
xlsfile = r'E:\tmp\projectMgr_project.xls'
book = xlrd.open_workbook(xlsfile)
sheet1 = book.sheet_by_index(0)
nrows = sheet1.nrows #行总数
for row in range(nrows):
sql = "insert into projectmgr_project (name) VALUES('"+sheet1.cell_value(row,1)+"')";
print sql
insert(cursor,sql)
db.commit()
cursor.close()
db.close()