简体   繁体   English

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

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

I'm new to running tasks through batch files. 我是刚开始通过批处理文件运行任务。 I have a batch and sql script that outputs to csv, which works. 我有一个批处理和sql脚本,可以输出到csv,它可以工作。 But I want the output to be in Excel. 但我希望输出在Excel中。 I've edited these scripts so that it can output in Excel. 我已经编辑了这些脚本,以便可以在Excel中输出。 The proces does create an Excel file. 该过程会创建一个Excel文件。 However when I attempt to open the Excel file, I get an error that the file has not been found. 但是,当我尝试打开Excel文件时,出现错误消息,即找不到该文件。 What am I doing wrong? 我究竟做错了什么?

This is the SQL script. 这是SQL脚本。 Let's say this is stored in file.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

This is the batch script: 这是批处理脚本:

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

I've basically just changed the .csv file extension in the scripts to a .xlsx extension but clearly this isn't enough, because I can't open the file. 我基本上只是将脚本中的.csv文件扩展名更改为.xlsx扩展名,但显然这还不够,因为我无法打开文件。

Also, how do I just select all (select *) through this proces? 另外,如何通过此过程选择全部(选择*)? I've tried changing that too, but I just ended up with no data at all (not even a csv file) 我也尝试过更改,但最终却根本没有任何数据(甚至没有csv文件)

In Excel VBA: 在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

This assumes a reference to ado in excel 这假设在Excel中引用了ado

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

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