繁体   English   中英

尝试通过运行批处理文件将SQL Developer导出到Excel

[英]Attempting to output a SQL developer export to Excel by running a batch file

我是刚开始通过批处理文件运行任务。 我有一个批处理和sql脚本,可以输出到csv,它可以工作。 但我希望输出在Excel中。 我已经编辑了这些脚本,以便可以在Excel中输出。 该过程会创建一个Excel文件。 但是,当我尝试打开Excel文件时,出现错误消息,即找不到该文件。 我究竟做错了什么?

这是SQL脚本。 假设这存储在file.sql中:

set verify off
set define off
set termout off
set heading off
set pages 50000
set feedback off
set newpage none
set trimspool on
set linesize 1000
set decimal 


whenever sqlerror exit


spool C:\pathname\filename.xlsx append

select
'' 
||COLUMN_1||
';'||COLUMN_2||
';'||COLUMN_3||
''
FROM blahblah.tablename;

spool off
exit

这是批处理脚本:

echo off

if exist C:\pathname\filename.xlsx del C:\pathname\filename.xlsx

:: creating a header

echo COLUMN_1;COLUMN_2;COLUMN_3;

sqlplus blahblah/password@databasename @C:\pathname\file.sql

我基本上只是将脚本中的.csv文件扩展名更改为.xlsx扩展名,但显然这还不够,因为我无法打开文件。

另外,如何通过此过程选择全部(选择*)? 我也尝试过更改,但最终却根本没有任何数据(甚至没有csv文件)

在Excel VBA中:

 Sub ImportData()
 Dim Conn as New Connection
 Conn.Connectionstring = ' get details from here https://www.connectionstrings.com/sql-server/   depending on version of sql you are using
 conn.open
 dim rs as new recordset
 rs.open "Select * from BlahBlahTablename",conn
 if not rs.eof then ' if we have data
     range("a2").copyfromrecordset rs
 end if
 Dim f as field
 dim r as range
 for each f in rs.fields
   r = f.name
   set r = r.offset(0,1)
  next f
 rs.close
 set rs = nothing
 conn.close
 End Sub

这假设在Excel中引用了ado

暂无
暂无

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

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