简体   繁体   English

如何将表导出为csv或excel格式

[英]How to export a table to csv or excel format

I need to export a oracle table to a csv/excel file format (along with the column headings). 我需要将oracle表导出为csv / excel文件格式(以及列标题)。 A solution through cx_oracle or through sqlplus welcome. 通过cx_oracle或通过sqlplus欢迎的解决方案。

Python code from the comment: 评论中的Python代码:

con = cx.connect() 
cur = con.cursor() 
printer = cur.execute(sqlcode) 
con.commit() 

perhaps use csv module (from standard library): 也许使用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())

If you have loads of data, unroll the fetchall() into a loop. 如果您有大量数据,请将fetchall()展开到循环中。

Happy trails! 快乐的小道!

要创建XLS文件,请使用python-excel项目中的xlwt模块。

对于非python解决方案,Tom Kyte有一些优秀的脚本, 可以使用PL / SQL(UTL_FILE),SQL * Plus和Pro * C 生成CSV文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM