将数据从Oracle SQL Developer导出到Excel .xlsx

时间:2017-06-23 15:22:58

标签: sql excel oracle xlsx

我有一个小项目,需要将Oracel SLQ Developer的数据导出到Excel(使用命令而不是SLQ Developer中的工具),然后创建图形。 使用“spool”我可以导出到csv罚款(但不能在csv中生成图表)但是当我尝试导出到xlsx时它会破坏整个excel表格

"Excel cannot open the file "ExcelFile.xlsx" because the file format or file extention 
       is not valid. Verify that the file has not been corrupted and that the 
       file extension mathces the format of the file."

这是我在SQL Developer中使用的代码。

spool FileLocation\ExcelFile.xlsm
SELECT * FROM Table;
spool off;

有什么方法可以阻止数据损坏,还是有其他方法可以将数据导出到.xlsx文件?

3 个答案:

答案 0 :(得分:2)

你知道导出的sql开发人员是使用sqlplus(代码是一样的)所以perhabs有其他方法,但这个应该足够好

我会尝试将第一行更改为:

spool ExcelFile.xls

可能你还需要打开

SET MARKUP HTML ON

http://www.orahow.com/2015/09/spool-sqlplus-output-to-excel-format.html

无论如何都有解决方法 - 您只需生成.CSV文件,然后在excel中打开它并保存为.xlsx文件

答案 1 :(得分:2)

Nooooooo。

set sqlformat csv
spool c:\file.sql
select * from table;
spool off;

然后在excel中打开文件。

OR

以交互方式运行查询。

右键点击网格,导出> XLSX。打开文件。

假脱机只将查询输出写入文件,它不查看文件扩展名,并弄清楚如何在该点写入输出。

所以你要么必须通过查询自己编码,要么使用我们支持的格式输出之一

SET SQLFORMAT
  CSV
  JSON
  DELIMITED
  XML
  HTML
  INSERT
  LOADER

使用'帮助设置sqlformat'求助。

答案 2 :(得分:0)

我也遇到了同样的问题,然后将其应用到代码下面,并成功导出了。

import xlsxwriter
from xlsxwriter import Workbook
import cx_Oracle
import datetime
from datetime import date

dsn_tns = cx_Oracle.makedsn('HOST', 'PORTNO', sid='BGRDB') 
    
db = cx_Oracle.connect(user=r'username', password='password', dsn=dsn_tns)
cursor = db.cursor()

workbook = xlsxwriter.Workbook('C:/Path/outfile.xlsx')
sheet = workbook.add_worksheet()


cursor.execute("select * from TABLENAME")
for r, row in enumerate(cursor.fetchall()):
         for c, col in enumerate(row):
                sheet.write(r, c, col)

workbook.close()
cursor.close()