如何将表格导出为CSV或Excel格式
我需要把一个Oracle数据库里的表导出成CSV或Excel文件格式,并且要包含列标题。通过cx_oracle或者sqlplus的解决方案都可以。
评论中的Python代码:
con = cx.connect()
cur = con.cursor()
printer = cur.execute(sqlcode)
con.commit()
3 个回答
0
如果你不想用Python,Tom Kyte有一些很棒的脚本,可以帮助你用PL/SQL(UTL_FILE)、SQL*Plus和Pro*C来生成CSV文件。你可以在他的博客上找到这些脚本,链接在这里:生成CSV文件。
2
要创建XLS文件,可以使用来自 python-excel 项目的 xlwt 模块。
14
可以考虑使用csv模块(这是标准库里的一个工具):
import csv
cursor = connection.cursor() # assuming you know how to connect to your oracle db
cursor.execute('select * from table_you_want_to_turn_to_csv')
with open('output_file.csv', 'wb') as fout:
writer = csv.writer(fout)
writer.writerow([ i[0] for i in cursor.description ]) # heading row
writer.writerows(cursor.fetchall())
如果你有很多数据,可以把fetchall()的结果放到一个循环里来处理。
祝你好运!