Home » SQL & PL/SQL » SQL & PL/SQL » how to write an excel in PLSQL
how to write an excel in PLSQL [message #615014] Thu, 29 May 2014 08:42 Go to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
how to create an EXCEL in PLSQL
Re: how to write an excel in PLSQL [message #615016 is a reply to message #615014] Thu, 29 May 2014 08:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
evivekj wrote on Thu, 29 May 2014 06:42
how to create an EXCEL in PLSQL

You don't since Excel file is a proprietary binary format.
Re: how to write an excel in PLSQL [message #615017 is a reply to message #615016] Thu, 29 May 2014 08:49 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
but in java it using APACHE POI package we can create excel file .
and is it not possible to export the table data into excel ?

[Updated on: Thu, 29 May 2014 08:49]

Report message to a moderator

Re: how to write an excel in PLSQL [message #615018 is a reply to message #615017] Thu, 29 May 2014 08:53 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
evivekj wrote on Thu, 29 May 2014 06:49
but in java it using APACHE POI package we can create excel file .
and is it not possible to export the table data into excel ?


via ODBC from Excel you can issue SQL statements against Oracle database.

what problem are you really trying to solve?
How will you, I or anyone know when it has been solved?

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and please read http://www.orafaq.com/forum/t/174502/102589/
Re: how to write an excel in PLSQL [message #615021 is a reply to message #615018] Thu, 29 May 2014 09:11 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
i have a table x with all the columns CLOB type
which i have to export into an excel sheet with names of the columns
Re: how to write an excel in PLSQL [message #615022 is a reply to message #615014] Thu, 29 May 2014 09:13 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Its is a FAQ in most of the forums. Have a look https://community.oracle.com/message/11052667
Re: how to write an excel in PLSQL [message #615055 is a reply to message #615022] Thu, 29 May 2014 15:26 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
hi lalit i have tried the code written in the OTN community but it is not creating xls but xml
Re: how to write an excel in PLSQL [message #615057 is a reply to message #615055] Thu, 29 May 2014 15:40 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
I believe Excel now opens .XML files and properly puts everything in the correct columns. I think you must have the current version, though.
Re: how to write an excel in PLSQL [message #615060 is a reply to message #615057] Thu, 29 May 2014 16:04 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
no on opening the file with msexcel first a popup says file is corrupted even then when i opens a blank excel sheet is showing

here below the code which i have used

-----------------------------------
CREATE OR REPLACE DIRECTORY TEST_DIR AS 'c:\myfiles' /* directory on the Oracle database server */
/
GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
/



As myuser:

DECLARE
v_fh UTL_FILE.FILE_TYPE;
v_dir VARCHAR2(30) := 'TEST_DIR';
v_file VARCHAR2(30) := 'myfile.xls';
PROCEDURE run_query(p_sql IN VARCHAR2) IS
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
-- parse the SQL statement
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
-- start execution of the SQL statement
d := DBMS_SQL.EXECUTE(c);
-- get a description of the returned columns
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
-- bind variables to columns
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,4000);
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,4000);
END CASE;
END LOOP;
-- Output the column headers
UTL_FILE.PUT_LINE(v_fh,'<ss:Row>');
FOR j in 1..col_cnt
LOOP
UTL_FILE.PUT_LINE(v_fh,'<ss:Cell>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Data ss:Type="String">'||rec_tab(j).col_name||'</ss:Data>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Cell>');
END LOOP;
UTL_FILE.PUT_LINE(v_fh,'</ss:Row>');
-- Output the data
LOOP
v_ret := DBMS_SQL.FETCH_ROWS(c);
EXIT WHEN v_ret = 0;
UTL_FILE.PUT_LINE(v_fh,'<ss:Row>');
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
UTL_FILE.PUT_LINE(v_fh,'<ss:Cell>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Data ss:Type="String">'||v_v_val||'</ss:Data>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Cell>');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
UTL_FILE.PUT_LINE(v_fh,'<ss:Cell>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Data ss:Type="Number">'||to_char(v_n_val)||'</ss:Data>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Cell>');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
UTL_FILE.PUT_LINE(v_fh,'<ss:Cell ss:StyleID="OracleDate">');
UTL_FILE.PUT_LINE(v_fh,'<ss:Data ss:Type="DateTime">'||to_char(v_d_val,'YYYY-MM-DD"T"HH24:MI:SS')||'</ss:Data>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Cell>');
ELSE
DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
UTL_FILE.PUT_LINE(v_fh,'<ss:Cell>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Data ss:Type="String">'||v_v_val||'</ss:Data>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Cell>');
END CASE;
END LOOP;
UTL_FILE.PUT_LINE(v_fh,'</ss:Row>');
END LOOP;
DBMS_SQL.CLOSE_CURSOR(c);
END;
--
PROCEDURE start_workbook IS
BEGIN
UTL_FILE.PUT_LINE(v_fh,'<?xml version="1.0"?>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
END;
PROCEDURE end_workbook IS
BEGIN
UTL_FILE.PUT_LINE(v_fh,'</ss:Workbook>');
END;
--
PROCEDURE start_worksheet(p_sheetname IN VARCHAR2) IS
BEGIN
UTL_FILE.PUT_LINE(v_fh,'<ss:Worksheet ss:Name="'||p_sheetname||'">');
UTL_FILE.PUT_LINE(v_fh,'<ss:Table>');
END;
PROCEDURE end_worksheet IS
BEGIN
UTL_FILE.PUT_LINE(v_fh,'</ss:Table>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Worksheet>');
END;
--
PROCEDURE set_date_style IS
BEGIN
UTL_FILE.PUT_LINE(v_fh,'<ss:Styles>');
UTL_FILE.PUT_LINE(v_fh,'<ss:Style ss:ID="OracleDate">');
UTL_FILE.PUT_LINE(v_fh,'<ss:NumberFormat ss:Format="dd/mm/yyyy\ hh:mm:ss"/>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Style>');
UTL_FILE.PUT_LINE(v_fh,'</ss:Styles>');
END;
BEGIN
v_fh := UTL_FILE.FOPEN(upper(v_dir),v_file,'w',32767);
start_workbook;
set_date_style;
start_worksheet('EMP');
run_query('select * from emp');
end_worksheet;
start_worksheet('DEPT');
run_query('select * from dept');
end_worksheet;
end_workbook;
UTL_FILE.FCLOSE(v_fh);
END;

--------------------------------------------

but when i open the generated files with wordpad
it shows an xml file with all what utl_file.put_line
Re: how to write an excel in PLSQL [message #615062 is a reply to message #615060] Thu, 29 May 2014 17:37 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
<v_file VARCHAR2(30) := 'myfile.xls';
not as above but as below
v_file VARCHAR2(30) := 'myfile.xml';

If the resultant file really contains well formed XML, it can be opened by most any modern web browser.
Re: how to write an excel in PLSQL [message #615086 is a reply to message #615062] Fri, 30 May 2014 02:34 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
this code is written to create XLS file not xml and this is not working so do any one have any alternative to generate xls in plsql
Re: how to write an excel in PLSQL [message #615089 is a reply to message #615086] Fri, 30 May 2014 02:52 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
xls is a binary file. You are creating a plain text file, which is why excel thinks it's corrupted.
You can't create binary files with utl_file. you can create .xml or .csv
Re: how to write an excel in PLSQL [message #615097 is a reply to message #615089] Fri, 30 May 2014 03:49 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
it means we cant export data from a table into excel file through plsql
Re: how to write an excel in PLSQL [message #615101 is a reply to message #615097] Fri, 30 May 2014 04:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Depends on your definition of excel file. Excel can read and write csv and xml files as well as xls.
Re: how to write an excel in PLSQL [message #615122 is a reply to message #615101] Fri, 30 May 2014 07:59 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
yea. That's a real pain with Oracle database. All end users love Excel sheets and Oracle SQL*Plus can't export to native Excel format. We solved it with CSV files, but users like more native Excel files.
Re: how to write an excel in PLSQL [message #615126 is a reply to message #615122] Fri, 30 May 2014 08:10 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
and if any data is separated by by delimeter comma then data on the file remains for no use
Re: how to write an excel in PLSQL [message #615128 is a reply to message #615126] Fri, 30 May 2014 08:17 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
evivekj wrote on Fri, 30 May 2014 06:10
and if any data is separated by by delimeter comma then data on the file remains for no use

any character can be used as column delimiter; like pipe ("|")
Re: how to write an excel in PLSQL [message #615129 is a reply to message #615128] Fri, 30 May 2014 08:24 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Or if the data contains commas you can wrap it in double quotes.
Previous Topic: How to add partition by list clause on existing empty table
Next Topic: Replace all substrings by another strings
Goto Forum:
  


Current Time: Tue Apr 23 04:44:57 CDT 2024