oracle query output in excel file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting oracle query output in excel file
# 1  
Old 04-28-2009
oracle query output in excel file

Hi All,
I have seen many examples of spooling query output in csv file.
But I want to spool the query results in proper excel file.
Can you guys help me on this?
# 2  
Old 04-28-2009
Quote:
Originally Posted by yabhi_22
Hi All,
I have seen many examples of spooling query output in csv file.
But I want to spool the query results in proper excel file.
Can you guys help me on this?
Something like this:

Code:
SQL> spool emp.xls
SQL> select empno||chr(9)||ename||chr(9)||job||chr(9)||mgr from emp;
SQL> spool off

chr(9) is the tab character, so essentially you are creating a "tsv" (tab-separated values) file that has the extension "xls".

Thereafter you can simply double-click on it and MS Excel, or an equivalent program, should be able to do the rest.

Hope that helps,
tyler_durden

_______________________________________
"Only after disaster can we be resurrected."
# 3  
Old 04-28-2009
Try this:

Code:
SET MARKUP HTML ON ENTMAP ON SPOOL ON PREFORMAT OFF ;
SPOOL MY_TEST_FILE.xls;
SELECT * FROM EMP;
SPOOL OFF;

regards,
Arun.
# 4  
Old 04-29-2009
Both worked

Thank you Guys.
Both your solutions worked perfectly fine.

I tried set heading on to display column names in the excel but it didn't work.
Did I missed something?
# 5  
Old 04-29-2009
Quote:
Originally Posted by yabhi_22
...
I tried set heading on to display column names in the excel but it didn't work.
Did I missed something?
Put the following commands in a script, say, C:\test.sql

Code:
set heading off pages 0 trimspool on feedback off echo off
spool c:\emp.xls
select 'EMPNO'||chr(9)||'ENAME'||chr(9)||'JOB'||chr(9)||'MGR' from dual
union all
select empno||chr(9)||ename||chr(9)||job||chr(9)||mgr from emp;
spool off

Then, from the SQL prompt:

Code:
test@ORA10G>
test@ORA10G> host type c:\test.sql
set heading off pages 0 trimspool on feedback off echo off
spool c:\emp.xls
select 'EMPNO'||chr(9)||'ENAME'||chr(9)||'JOB'||chr(9)||'MGR' from dual
union all
select empno||chr(9)||ename||chr(9)||job||chr(9)||mgr from emp;
spool off
test@ORA10G>
test@ORA10G> @c:\test.sql
EMPNO   ENAME   JOB     MGR
7369    SMITH   CLERK   7902
7499    ALLEN   SALESMAN        7698
7521    WARD    SALESMAN        7698
7566    JONES   MANAGER 7839
7654    MARTIN  SALESMAN        7698
7698    BLAKE   MANAGER 7839
7782    CLARK   MANAGER 7839
7788    SCOTT   ANALYST 7566
7839    KING    PRESIDENT
7844    TURNER  SALESMAN        7698
7876    ADAMS   CLERK   7788
7900    JAMES   CLERK   7698
7902    FORD    ANALYST 7566
7934    MILLER  CLERK   7782
test@ORA10G>

Hope that helps,
tyler_durden

______________________________________________
"Only after disaster can we be resurrected."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop output to excel file

Hi, I have a shell script which analyses the log folder for a specific string and throws me the output. I have used for loop since it does this in multiple servers. Now I want to save the output in a excel in the below format. Can someone please help? The output which I get Server1 : count... (1 Reply)
Discussion started by: srilaxman
1 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. UNIX for Dummies Questions & Answers

How to extract UNIX output to excel CSV file?

Hi guys - I have a file and output like this below: File: myfile.dat File has content like this: ABCD, 34, 456, 2324 I would like excel to have this: Name (ABCD) Quantity (34), Type (456), Status (2324) (Name, Quantity, Type and Status are columns) I would like to export... (4 Replies)
Discussion started by: DallasT
4 Replies

4. UNIX for Dummies Questions & Answers

Removing unnecessary eol ($) character from Oracle sql query output

Hi All, I am fetching oracle query result in shell variable. As columns numbers are more the output wraps in unix terminal .i.e one complete record in db gets store in multiple lines. with each line ends with $ character. I want to remove these unnecessary $ character but to keep required $... (8 Replies)
Discussion started by: Harshal22
8 Replies

5. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

6. Shell Programming and Scripting

Problems with storing oracle sqlplus query output shell script

Hello everyone, I have a RHEL 5 system and have been trying to get a batch of 3-4 scripts each in a separate variables and they are not working as expected. I tried using following syntax which I saw a lot of people on this site use and should really work, though for some reason it doesn't... (3 Replies)
Discussion started by: rockf1bull
3 Replies

7. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

8. Shell Programming and Scripting

redirecting oracle sqlplus select query into file

So, I would like to run differen select queries on multiple databases.. I made a script wich I thought to be called something like.. ./script.sh sql_file_name out.log or to enter select statement in a command line.. (aix) and I did created some shell script wich is not working.. it... (6 Replies)
Discussion started by: bongo
6 Replies

9. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

10. Shell Programming and Scripting

shellscript.query Oracle table..populate in a text file

Hi Guys, I'm new to this forum as well as to UNIX shell scripting. I'm looking for a shellscript to query an Oracle database table and populate the result set of the query in a text file. Could you someone help me out with a sample code? Thanks, Bhagat (7 Replies)
Discussion started by: bhagat.singh-j
7 Replies
Login or Register to Ask a Question