该文章主要使用Python的cx_Oracle库和openpyxl库把Oracle数据库表中的数据写入到Excel中。
cx_Oracle:提供了连接Oracle数据库和操作数据库的方法。
openpyxl:用于操作Excel文件,包含Excel的各种操作方法。
代码如下:
import cx_Oracle
import openpyxl
代码如下:
#连接Oracle参数:user/password@host:端口/库名
dbname = {
"user":"SCOTT",
"pwd":"****",
"dsn":"127.0.0.1:1521/ORCL"
}
#创建类
class OracleTable():
#初始连接数据库
def __init__(self):
"""连接池方式"""
self.db_info = dbname
self.conn = OracleTable.__getConnect(self.db_info)
#获取数据库连接对象
@staticmethod
def __getConnect(db_info):
try:
con = cx_Oracle.connect(db_info['user'],db_info['pwd'],db_info['dsn'])
return con
except Exception as e:
print("数据库连接异常:%s" %e)
#查询数据库表数据操作
def oracle_getrow(self,sql):
#执行SQL
try:
cursor = self.conn.cursor() #建立游标
try:
cursor.execute(sql) #执行SQL
#获取表字段用于Excel的表头
title = [i[0] for i in cursor.description]
rows = cursor.fetchall() #获取查询数据
return title,rows
except Exception as e:
print("执行SQL出现异常:%s" %e)
finally:
cursor.close #关闭游标
except Exception as e:
print("数据库连接异常:%s" %e)
#关闭Oracle数据库连接
def oracle_close(self):
try:
self.conn.close()
except Exception as e:
print("数据库关闭异常:%s" %e)
代码如下:
#第一个参数是表头,第二个是表数据,第三个是Excel文件名称
def data_xls(title,rows,xlname):
try:
xls = openpyxl.Workbook() #打开Excel工作表格
sheet = xls.create_sheet(title='mysheet') #创建sheet页
#写入表头
for t,d in enumerate(title):
sheet.cell(row=1,column=1+t,value=d)
#写入数据
for s,i in enumerate(rows): #列表中获取每个元组
for n,c in enumerate(i): #元组中拿出每个内容
sheet.cell(row=s+2,column=n+1,value=c)
xls.save(xlname) #保存数据到Excel
except:
raise
代码如下:
if __name__ == "__main__":
print("开始执行")
oracle = OracleTable() #调用对象
sql = input("请输入要查询的SQL:")
print(sql)
#通过对象调用执行SQL方法,返回查询的数据列表
rs = oracle.oracle_getrow(sql)
print(rs[0])
xlname = input("请输入文件名:")
#调用操作Excel方法,传参
data_xls(rs[0],rs[1],xlname)
print("执行结束!")
代码如下:
import cx_Oracle
import openpyxl
#连接Oracle参数:user/password@host:端口/库名
dbname = {
"user":"SCOTT",
"pwd":"scott",
"dsn":"127.0.0.1:1521/ORCL"
}
#创建类
class OracleTable():
#初始连接数据库
def __init__(self):
"""连接池方式"""
self.db_info = dbname
self.conn = OracleTable.__getConnect(self.db_info)
#获取数据库连接对象
@staticmethod
def __getConnect(db_info):
try:
con = cx_Oracle.connect(db_info['user'],db_info['pwd'],db_info['dsn'])
return con
except Exception as e:
print("数据库连接异常:%s" %e)
#查询数据库表数据操作
def oracle_getrow(self,sql):
#执行SQL
try:
cursor = self.conn.cursor() #建立游标
try:
cursor.execute(sql) #执行SQL
#获取表字段用于Excel的表头
title = [i[0] for i in cursor.description]
rows = cursor.fetchall() #获取查询数据
return title,rows
except Exception as e:
print("执行SQL出现异常:%s" %e)
finally:
cursor.close #关闭游标
except Exception as e:
print("数据库连接异常:%s" %e)
#关闭Oracle数据库连接
def oracle_close(self):
try:
self.conn.close()
except Exception as e:
print("数据库关闭异常:%s" %e)
#第一个参数是表头,第二个是表数据,第三个是Excel文件名称
def data_xls(title,rows,xlname):
try:
xls = openpyxl.Workbook() #打开Excel工作表格
sheet = xls.create_sheet(title='mysheet') #创建sheet页
#写入表头
for t,d in enumerate(title):
sheet.cell(row=1,column=1+t,value=d)
#写入数据
for s,i in enumerate(rows): #列表中获取每个元组
for n,c in enumerate(i): #元组中拿出每个内容
sheet.cell(row=s+2,column=n+1,value=c)
xls.save(xlname) #保存数据到Excel
except:
raise
if __name__ == "__main__":
print("开始执行")
oracle = OracleTable() #调用对象
sql = input("请输入要查询的SQL:")
print(sql)
#通过对象调用执行SQL方法,返回查询的数据列表
rs = oracle.oracle_getrow(sql)
print(rs[0])
xlname = input("请输入文件名:")
#调用操作Excel方法,传参
data_xls(rs[0],rs[1],xlname)
print("执行结束!")
以上就是今天要说的内容,本文简单介绍了cx_Oracle和openpyxl的使用,通过本案例可以方便的将Oracle数据库表的数据导入到Excel文件中。