Python 导入文件夹中所有excel并按列名读取插入数据库

来源:互联网 发布:网络推广平台公司 编辑:程序博客网 时间:2024/06/03 21:46

#-*- coding: utf-8 -*-# python 3.5.0__author__ = 'HZC'import osimport xlrdimport pymssqlimport datetimeclass BUSINESS:def __init__(self):self.host = "HZC"self.user = "kk"self.pwd = "kk"self.db = "business"self._conn = self.GetConnect()if(self._conn):self._cur = self._conn.cursor()#连接数据库def GetConnect(self):conn = Falsetry:conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database =self.db)except Exception as err:print("连接数据库失败, %s" % err)else:return conn#执行非查询类语句def ExecNonQuery(self, sql):flag = Falsetry:self._cur.execute(sql)self._conn.commit()flag = Trueexcept Exception as err:flag = Falseself._conn.rollback()print("执行失败, %s" % err)else:return flag#excel 中的日期格式化(否则为浮点数值)def FormatDatetimeValue(self,book, sheet,ind_row,ind_col):cell = sheet.cell(ind_row, ind_col)if cell.ctype == 3: # 3 means 'xldate' , 1 means 'textyear, month, day, hour, minute, second = xlrd.xldate_as_tuple(cell.value, book.datemode)py_date = datetime.datetime(year, month, day, hour, minute, second)return py_date.strftime('%Y-%m-%d %H:%M:%S')#读取表格第一行作为列名。避免重复读取,名称和位置存储为字典(如:{"name":0, "mobile":1})def GetColumnTitle(self, sheet):col_dict = {}  for i in range(sheet.ncols):col_dict[sheet.cell_value(0, i).strip()] = ireturn col_dict#遍历文件夹,读取所有【xls】文件名称def GetFolderFile(self,dir):for directory, subdirectories, files in os.walk(dir):#files = [s for s in files if ".xls" in s]for file in files:if os.path.splitext(file)[1][1:] != "xls":continuefilepath = os.path.join(directory, file)self.SaveOneFile(filepath)#读取一个 xls 文件到数据库(第一个 sheet$)def SaveOneFile(self,filepath):print(filepath)workbook = xlrd.open_workbook(filepath)sheet = workbook.sheet_by_index(0) #只读第一个$sheet,没遍历所有!#如第一行有列名,则按列名取数col_dict = self.GetColumnTitle(sheet)if bool(col_dict):number_of_rows = sheet.nrowsfor row in range(1, number_of_rows):sql = """INSERT INTO tab(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col2)VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %s, '%s', '%s')""" % \(self.FormatDatetimeValue(workbook,sheet,row,col_dict["col1"]),self.FormatDatetimeValue(workbook,sheet,row,col_dict["col2"]),self.FormatDatetimeValue(workbook,sheet,row,col_dict["col3"]),sheet.cell(row,col_dict["col4"]).value,sheet.cell(row,col_dict["col5"]).value,sheet.cell(row,col_dict["col6"]).value,sheet.cell(row,col_dict["col7"]).value,sheet.cell(row,col_dict["col8"]).value,sheet.cell(row,col_dict["col9"]).value,sheet.cell(row,col_dict["col10"]).value,sheet.cell(row,col_dict["col11"]).value,sheet.cell(row,col_dict["col12"]).value#datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))self.ExecNonQuery(sql)else:passif __name__ == "__main__":bs = BUSINESS()#整个文件夹bs.GetFolderFile(r'C:/Users/huangzecheng/Desktop/11月/')#一个文件#bs.SaveOneFile(r'C:/Users/huangzecheng/Desktop/11月/py20171101.xls')









阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 优泽倍润修护霜 优倍通 光明优倍你敢喝吗 倍优诺 优倍康微囊粉多钱一盒 学而思优倍 优倍视全焦数控恢复镜 光明优倍 优倍 倍优 优智借app 优借 优借记 趣优借靠谱吗 优借app 小优借 橄榄优借 优借上征信吗 优借可靠吗 优借贷款怎么样 优借可以贷款吗 橄榄优借app 优借能下款吗 优易借 优借贷款 优先股 优先股股利 光圈优先 优先权 快门优先 优先购买权 优先 女士优先 宁波人流优先新东方 宁波妇科优先新东方 宁波男科优先新东方 重庆妇科优先阳光 东莞人流优先深圳怡康 深圳妇科优先怡康 宁波做人流优先新东方 宁波看妇科优先新东方