場景:資料夾下有n多excel,每個excel中有1個sheet,sheet頁中首行為**的列頭名
實現:將以上所有**匯入到mysql中去,以檔名命名為表名,以列頭名命名為欄位名
細節:將所有字段處理成字串,其中半形"處理成了全形」 等
不足:……
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xlrd
import string
import os
import mysqldb
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
def issubstring(substrlist,str):
'''''
#判斷字串str是否包含序列substrlist中的每乙個子字串
#>>>substrlist=['f','ems','txt']
#>>>str='f06925ems91.txt'
#>>>issubstring(substrlist,str)#return true (or false)
'''
flag=true
for substr in substrlist:
if not(substr in str):
flag=false
return flag
def fn_get_filelist(findpath,flagstr=):
'''''
#獲取目錄中指定的檔名
#>>>flagstr=['f','ems','txt'] #要求檔名稱中包含這些字元
#>>>filelist=getfilelist(findpath,flagstr) #
'''
import os
filelist=
filenames=os.listdir(findpath)
if (len(filenames)>0):
for fn in filenames:
if (len(flagstr)>0):
#返回指定型別的檔名
if (issubstring(flagstr,fn)):
fullfilename=os.path.join(findpath,fn)
else:
#預設直接返回所有檔名
fullfilename=os.path.join(findpath,fn)
#對檔名排序
if (len(filelist)>0):
filelist.sort()
for i in range(len(filelist)):
print filelist[i]
return filelist
def fn_create(f):
book = xlrd.open_workbook(f)
sheet = book.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
c = ""
for ic in range(cols):
arr_v = sheet.col_values(ic)
h = fn_arr_maxlen(arr_v)
c = c + '`' + sheet.cell(0,ic).value + '` varchar(' + str(h) + ') null comment \"' + sheet.cell(0,ic).value + "\"," + "\n\r"
t = unicode(f.split("/")[-1].split(".")[0],"utf-8")# f.replace('/tmp/excel/','')
t1 = ' create table if not exists `suyang_'
t2 = ' id int auto_increment ,primary key (id)) engine=innodb default charset=utf8;'
t = t1 + t + '` (' + c + t2
#print t
return t
def fn_insert(f):
book = xlrd.open_workbook(f)
sheet = book.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
c = ''
for ic in range(cols):
c = c + '`' + sheet.cell(0,ic).value + '`,'
t = f.split("/")[-1].split(".")[0] # f.replace('/tmp/excel/','')
print t
insertsql = ' insert into `suyang_' + t + '` (' + c.rstrip(',') + ' ) values ( '
for ir in range(1,rows):
row_values = ''
insertsql = ' insert into `suyang_' + t + '` (' + c.rstrip(',') + ' ) values ( '
for icc in range(cols):
if isinstance(sheet.cell(ir,icc).value,int) or isinstance(sheet.cell(ir,icc).value,float):
vs = repr(sheet.cell(ir,icc).value).split(".")[0]
else:
vs = str(sheet.cell(ir,icc).value)
insertsql = insertsql + row_values + '"' + str(vs).replace('"','」') + '",'
insertsql = insertsql.rstrip(',')+');' #sheet.cell(ir,icc)
yield insertsql
#print t
def fn_arr_maxlen(arr):
o = 0
for v in arr:
if (o < len(str(v).strip())):
o = len(str(v).strip())
return o
if __name__ == '__main__':
'''''cs=fn_create('/tmp/excel/登出資訊.xlsx')
print cs'''
mycn = mysqldb.connect("10.18.141.52","dba","***************x","ys" )
mycn.set_character_set('utf8')
mycursor = mycn.cursor()
mycursor.execute('set character set utf8;')
mycursor.execute('set names utf8;')
mycursor.execute('set character_set_connection=utf8;')
fs=fn_get_filelist("/tmp/excel",['xlsx'])
for ii in range(len(fs)):
tcreatesql = fn_create(fs[ii])
print tcreatesql
try:
mycursor.execute(tcreatesql)
for isql in fn_insert(fs[ii]):
#print isql
mycursor.execute(isql)
mycn.commit()
except exception , e:
print e
mycursor.close()
mycn.close()
python實現通用excel匯入到mysql
file 要處理的 檔案 startrow 從第幾行開始匯入 table 匯入到哪個表 comitcount 多少筆一提交 由於資料庫對sql長度是有限制的,不建議設定太大 map 表字段與excel列的對映關係 import math import xlrd import json from db...
Excel資料匯入到Grid
方法一 string strcon provider microsoft.jet.oledb.4.0 data source strsource extended properties excel 8.0 string query select from sheet1 sheet1表示表 oledb...
Excel資料匯入到Access
下面是asp的全部程式,需要做的是建乙個test.mdb資料庫和乙個test.xls的excel 查詢excel準備匯入到access sql select from sheet1 要匯入的excel資料裡面的表的名稱,後面一定要加 set rs conn2.execute sql while no...