Создание файла Excel средствами PL/SQL

На просторах сети нашел очень интересный PL/SQL пакет, который позволяет легко формировать Excel файлы в формате xlsx на основе запросов к базе данных. Такой пакет очень даже может пригодиться.

Для формирования Excel файла необходимо создать объет DIRECTORY на сервере базы данных. Сам код PL/SQL пакета:

CREATE OR REPLACE PACKAGE as_xlsx
IS
/**********************************************
**
** Author: Anton Scheffer
** Date: 19-02-2011
** Website: http://technology.amis.nl/blog
** See also: http://technology.amis.nl/blog/?p=10995
**
** Changelog:
** Date: 21-02-2011
** Added Aligment, horizontal, vertical, wrapText
** Date: 06-03-2011
** Added Comments, MergeCells, fixed bug for dependency on NLS-settings
** Date: 16-03-2011
** Added bold and italic fonts
** Date: 22-03-2011
** Fixed issue with timezone's set to a region(name) instead of a offset
** Date: 08-04-2011
** Fixed issue with XML-escaping from text
** Date: 27-05-2011
** Added MIT-license
** Date: 11-08-2011
** Fixed NLS-issue with column width
** Date: 29-09-2011
** Added font color
** Date: 16-10-2011
** fixed bug in add_string
** Date: 26-04-2012
** Fixed set_autofilter (only one autofilter per sheet, added _xlnm._FilterDatabase)
** Added list_validation = drop-down
**
******************************************************************************
******************************************************************************
Copyright (C) 2011, 2012 by Anton Scheffer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

******************************************************************************
******************************************** */
--
TYPE tp_alignment IS RECORD
( vertical VARCHAR2(11)
, horizontal VARCHAR2(16)
, wrapText BOOLEAN
);
--
PROCEDURE clear_workbook;
--
PROCEDURE new_sheet( p_sheetname VARCHAR2 := NULL );
--
FUNCTION OraFmt2Excel( p_format VARCHAR2 := NULL )
RETURN VARCHAR2;
--
FUNCTION get_numFmt( p_format VARCHAR2 := NULL )
RETURN PLS_INTEGER;
--
FUNCTION get_font
( p_name VARCHAR2
, p_family PLS_INTEGER := 2
, p_fontsize NUMBER := 11
, p_theme PLS_INTEGER := 1
, p_underline BOOLEAN := FALSE
, p_italic BOOLEAN := FALSE
, p_bold BOOLEAN := FALSE
, p_rgb VARCHAR2 := NULL -- this is a hex ALPHA Red Green Blue value
)
RETURN PLS_INTEGER;
--
FUNCTION get_fill
( p_patternType VARCHAR2
, p_fgRGB VARCHAR2 := NULL -- this is a hex ALPHA Red Green Blue value
)
RETURN PLS_INTEGER;
--
FUNCTION get_border
( p_top VARCHAR2 := 'thin'
, p_bottom VARCHAR2 := 'thin'
, p_left VARCHAR2 := 'thin'
, p_right VARCHAR2 := 'thin'
)
/*
none
thin
medium
dashed
dotted
thick
double
hair
mediumDashed
dashDot
mediumDashDot
dashDotDot
mediumDashDotDot
slantDashDot
*/
RETURN PLS_INTEGER;
--
FUNCTION get_alignment
( p_vertical VARCHAR2 := NULL
, p_horizontal VARCHAR2 := NULL
, p_wrapText BOOLEAN := NULL
)
/* horizontal
center
centerContinuous
distributed
fill
general
justify
left
right
*/
/* vertical
bottom
center
distributed
justify
top
*/
RETURN tp_alignment;
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value NUMBER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value VARCHAR2
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value DATE
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE hyperlink
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_url VARCHAR2
, p_value VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE COMMENT
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_text VARCHAR2
, p_author VARCHAR2 := NULL
, p_width PLS_INTEGER := 150 -- pixels
, p_height PLS_INTEGER := 100 -- pixels
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE mergecells
( p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE list_validation
( p_sqref_col PLS_INTEGER
, p_sqref_row PLS_INTEGER
, p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_style VARCHAR2 := 'stop' -- stop, warning, information
, p_title VARCHAR2 := NULL
, p_prompt VARCHAR := NULL
, p_show_error BOOLEAN := FALSE
, p_error_title VARCHAR2 := NULL
, p_error_txt VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE list_validation
( p_sqref_col PLS_INTEGER
, p_sqref_row PLS_INTEGER
, p_defined_name VARCHAR2
, p_style VARCHAR2 := 'stop' -- stop, warning, information
, p_title VARCHAR2 := NULL
, p_prompt VARCHAR := NULL
, p_show_error BOOLEAN := FALSE
, p_error_title VARCHAR2 := NULL
, p_error_txt VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE defined_name
( p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_name VARCHAR2
, p_sheet PLS_INTEGER := NULL
, p_localsheet PLS_INTEGER := NULL
);
--
PROCEDURE set_column_width
( p_col PLS_INTEGER
, p_width NUMBER
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE set_column
( p_col PLS_INTEGER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE set_row
( p_row PLS_INTEGER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE freeze_rows
( p_nr_rows PLS_INTEGER := 1
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE freeze_cols
( p_nr_cols PLS_INTEGER := 1
, p_sheet PLS_INTEGER := NULL
);
--
PROCEDURE set_autofilter
( p_column_start PLS_INTEGER := NULL
, p_column_end PLS_INTEGER := NULL
, p_row_start PLS_INTEGER := NULL
, p_row_end PLS_INTEGER := NULL
, p_sheet PLS_INTEGER := NULL
);
--
FUNCTION finish
RETURN blob;
--
PROCEDURE save
( p_directory VARCHAR2
, p_filename VARCHAR2
);
--
PROCEDURE query2sheet
( p_sql VARCHAR2
, p_column_headers BOOLEAN := TRUE
, p_directory VARCHAR2 := NULL
, p_filename VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
);
--
/* Example
begin
as_xlsx.clear_workbook;
as_xlsx.new_sheet;
as_xlsx.cell( 5, 1, 5 );
as_xlsx.cell( 3, 1, 3 );
as_xlsx.cell( 2, 2, 45 );
as_xlsx.cell( 3, 2, 'Anton Scheffer', p_alignment => as_xlsx.get_alignment( p_wraptext => true ) );
as_xlsx.cell( 1, 4, sysdate, p_fontId => as_xlsx.get_font( 'Calibri', p_rgb => 'FFFF0000' ) );
as_xlsx.cell( 2, 4, sysdate, p_numFmtId => as_xlsx.get_numFmt( 'dd/mm/yyyy h:mm' ) );
as_xlsx.cell( 3, 4, sysdate, p_numFmtId => as_xlsx.get_numFmt( as_xlsx.orafmt2excel( 'dd/mon/yyyy' ) ) );
as_xlsx.cell( 5, 5, 75, p_borderId => as_xlsx.get_border( 'double', 'double', 'double', 'double' ) );
as_xlsx.cell( 2, 3, 33 );
as_xlsx.hyperlink( 1, 6, 'http://www.amis.nl', 'Amis site' );
as_xlsx.cell( 1, 7, 'Some merged cells', p_alignment => as_xlsx.get_alignment( p_horizontal => 'center' ) );
as_xlsx.mergecells( 1, 7, 3, 7 );
for i in 1 .. 5
loop
as_xlsx.comment( 3, i + 3, 'Row ' || (i+3), 'Anton' );
end loop;
as_xlsx.new_sheet;
as_xlsx.set_row( 1, p_fillId => as_xlsx.get_fill( 'solid', 'FFFF0000' ) ) ;
for i in 1 .. 5
loop
as_xlsx.cell( 1, i, i );
as_xlsx.cell( 2, i, i * 3 );
as_xlsx.cell( 3, i, 'x ' || i * 3 );
end loop;
as_xlsx.query2sheet( 'select rownum, x.*
, case when mod( rownum, 2 ) = 0 then rownum * 3 end demo
, case when mod( rownum, 2 ) = 1 then ''demo '' || rownum end demo2 from dual x connect by rownum <= 5' );
as_xlsx.save( 'MY_DIR', 'my.xlsx' );
end;
--
begin
as_xlsx.clear_workbook;
as_xlsx.new_sheet;
as_xlsx.cell( 1, 6, 5 );
as_xlsx.cell( 1, 7, 3 );
as_xlsx.cell( 1, 8, 7 );
as_xlsx.new_sheet;
as_xlsx.cell( 2, 6, 15, p_sheet => 2 );
as_xlsx.cell( 2, 7, 13, p_sheet => 2 );
as_xlsx.cell( 2, 8, 17, p_sheet => 2 );
as_xlsx.list_validation( 6, 3, 1, 6, 1, 8, p_show_error => true, p_sheet => 1 );
as_xlsx.defined_name( 2, 6, 2, 8, 'Anton', 2 );
as_xlsx.list_validation
( 6, 1, 'Anton'
, p_style => 'information'
, p_title => 'valid values are'
, p_prompt => '13, 15 and 17'
, p_show_error => true
, p_error_title => 'Are you sure?'
, p_error_txt => 'Valid values are: 13, 15 and 17'
, p_sheet => 1 );
as_xlsx.save( 'MY_DIR', 'my.xlsx' );
end;
--
begin
as_xlsx.clear_workbook;
as_xlsx.new_sheet;
as_xlsx.cell( 1, 6, 5 );
as_xlsx.cell( 1, 7, 3 );
as_xlsx.cell( 1, 8, 7 );
as_xlsx.set_autofilter( 1,1, p_row_start => 5, p_row_end => 8 );
as_xlsx.new_sheet;
as_xlsx.cell( 2, 6, 5 );
as_xlsx.cell( 2, 7, 3 );
as_xlsx.cell( 2, 8, 7 );
as_xlsx.set_autofilter( 2,2, p_row_start => 5, p_row_end => 8 );
as_xlsx.save( 'MY_DIR', 'my.xlsx' );
end;
*/
END;
/

CREATE OR REPLACE PACKAGE BODY as_xlsx
IS
--
c_LOCAL_FILE_HEADER CONSTANT RAW(4) := HEXTORAW( '504B0304' ); -- Local file header signature
c_END_OF_CENTRAL_DIRECTORY CONSTANT RAW(4) := HEXTORAW( '504B0506' ); -- End of central directory signature
--
TYPE tp_XF_fmt IS RECORD
( numFmtId PLS_INTEGER
, fontId PLS_INTEGER
, fillId PLS_INTEGER
, borderId PLS_INTEGER
, alignment tp_alignment
);
TYPE tp_col_fmts IS TABLE OF tp_XF_fmt INDEX BY PLS_INTEGER;
TYPE tp_row_fmts IS TABLE OF tp_XF_fmt INDEX BY PLS_INTEGER;
TYPE tp_widths IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
TYPE tp_cell IS RECORD
( VALUE NUMBER
, style VARCHAR2(50)
);
TYPE tp_cells IS TABLE OF tp_cell INDEX BY PLS_INTEGER;
TYPE tp_rows IS TABLE OF tp_cells INDEX BY PLS_INTEGER;
TYPE tp_autofilter IS RECORD
( column_start PLS_INTEGER
, column_end PLS_INTEGER
, row_start PLS_INTEGER
, row_end PLS_INTEGER
);
TYPE tp_autofilters IS TABLE OF tp_autofilter INDEX BY PLS_INTEGER;
TYPE tp_hyperlink IS RECORD
( cell VARCHAR2(10)
, url VARCHAR2(1000)
);
TYPE tp_hyperlinks IS TABLE OF tp_hyperlink INDEX BY PLS_INTEGER;
SUBTYPE tp_author IS VARCHAR2(32767 CHAR);
TYPE tp_authors IS TABLE OF PLS_INTEGER INDEX BY tp_author;
authors tp_authors;
TYPE tp_comment IS RECORD
( text VARCHAR2(32767 CHAR)
, author tp_author
, ROW PLS_INTEGER
, COLUMN PLS_INTEGER
, width PLS_INTEGER
, height PLS_INTEGER
);
TYPE tp_comments IS TABLE OF tp_comment INDEX BY PLS_INTEGER;
TYPE tp_mergecells IS TABLE OF VARCHAR2(21) INDEX BY PLS_INTEGER;
TYPE tp_validation IS RECORD
( TYPE VARCHAR2(10)
, errorstyle VARCHAR2(32)
, showinputmessage BOOLEAN
, prompt VARCHAR2(32767 CHAR)
, title VARCHAR2(32767 CHAR)
, error_title VARCHAR2(32767 CHAR)
, error_txt VARCHAR2(32767 CHAR)
, showerrormessage BOOLEAN
, formula1 VARCHAR2(32767 CHAR)
, formula2 VARCHAR2(32767 CHAR)
, allowBlank BOOLEAN
, sqref VARCHAR2(32767 CHAR)
);
TYPE tp_validations IS TABLE OF tp_validation INDEX BY PLS_INTEGER;
TYPE tp_sheet IS RECORD
( ROWS tp_rows
, widths tp_widths
, name VARCHAR2(100)
, freeze_rows PLS_INTEGER
, freeze_cols PLS_INTEGER
, autofilters tp_autofilters
, hyperlinks tp_hyperlinks
, col_fmts tp_col_fmts
, row_fmts tp_row_fmts
, comments tp_comments
, mergecells tp_mergecells
, validations tp_validations
);
TYPE tp_sheets IS TABLE OF tp_sheet INDEX BY PLS_INTEGER;
TYPE tp_numFmt IS RECORD
( numFmtId PLS_INTEGER
, formatCode VARCHAR2(100)
);
TYPE tp_numFmts IS TABLE OF tp_numFmt INDEX BY PLS_INTEGER;
TYPE tp_fill IS RECORD
( patternType VARCHAR2(30)
, fgRGB VARCHAR2(8)
);
TYPE tp_fills IS TABLE OF tp_fill INDEX BY PLS_INTEGER;
TYPE tp_cellXfs IS TABLE OF tp_xf_fmt INDEX BY PLS_INTEGER;
TYPE tp_font IS RECORD
( name VARCHAR2(100)
, family PLS_INTEGER
, fontsize NUMBER
, theme PLS_INTEGER
, RGB VARCHAR2(8)
, underline BOOLEAN
, italic BOOLEAN
, bold BOOLEAN
);
TYPE tp_fonts IS TABLE OF tp_font INDEX BY PLS_INTEGER;
TYPE tp_border IS RECORD
( top VARCHAR2(17)
, bottom VARCHAR2(17)
, left VARCHAR2(17)
, right VARCHAR2(17)
);
TYPE tp_borders IS TABLE OF tp_border INDEX BY PLS_INTEGER;
TYPE tp_numFmtIndexes IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
TYPE tp_strings IS TABLE OF PLS_INTEGER INDEX BY VARCHAR2(32767 CHAR);
TYPE tp_str_ind IS TABLE OF VARCHAR2(32767 CHAR) INDEX BY PLS_INTEGER;
TYPE tp_defined_name IS RECORD
( name VARCHAR2(32767 CHAR)
, REF VARCHAR2(32767 CHAR)
, sheet PLS_INTEGER
);
TYPE tp_defined_names IS TABLE OF tp_defined_name INDEX BY PLS_INTEGER;
TYPE tp_book IS RECORD
( sheets tp_sheets
, strings tp_strings
, str_ind tp_str_ind
, str_cnt PLS_INTEGER := 0
, fonts tp_fonts
, fills tp_fills
, borders tp_borders
, numFmts tp_numFmts
, cellXfs tp_cellXfs
, numFmtIndexes tp_numFmtIndexes
, defined_names tp_defined_names
);
workbook tp_book;
--
PROCEDURE blob2file
( p_blob blob
, p_directory VARCHAR2 := 'MY_DIR'
, p_filename VARCHAR2 := 'my.xlsx'
)
IS
t_fh utl_file.file_type;
t_len PLS_INTEGER := 32767;
BEGIN
t_fh := utl_file.fopen( p_directory
, p_filename
, 'wb'
);
FOR i IN 0 .. TRUNC( ( dbms_lob.getlength( p_blob ) - 1 ) / t_len )
LOOP
utl_file.put_raw( t_fh
, dbms_lob.SUBSTR( p_blob
, t_len
, i * t_len + 1
)
);
END LOOP;
utl_file.fclose( t_fh );
END;
--
FUNCTION raw2num( p_raw RAW, p_len INTEGER, p_pos INTEGER )
RETURN NUMBER
IS
BEGIN
RETURN utl_raw.cast_to_binary_integer( utl_raw.SUBSTR( p_raw, p_pos, p_len ), utl_raw.little_endian );
END;
--
FUNCTION little_endian( p_big NUMBER, p_bytes PLS_INTEGER := 4 )
RETURN RAW
IS
BEGIN
RETURN utl_raw.SUBSTR( utl_raw.cast_from_binary_integer( p_big, utl_raw.little_endian ), 1, p_bytes );
END;
--
FUNCTION blob2num( p_blob blob, p_len INTEGER, p_pos INTEGER )
RETURN NUMBER
IS
BEGIN
RETURN utl_raw.cast_to_binary_integer( dbms_lob.SUBSTR( p_blob, p_len, p_pos ), utl_raw.little_endian );
END;
--
PROCEDURE add1file
( p_zipped_blob IN OUT blob
, p_name VARCHAR2
, p_content blob
)
IS
t_now DATE;
t_blob blob;
t_len INTEGER;
t_clen INTEGER;
t_crc32 RAW(4) := HEXTORAW( '00000000' );
t_compressed BOOLEAN := FALSE;
t_name RAW(32767);
BEGIN
t_now := SYSDATE;
t_len := NVL( dbms_lob.getlength( p_content ), 0 );
IF t_len > 0
THEN
t_blob := utl_compress.lz_compress( p_content );
t_clen := dbms_lob.getlength( t_blob ) - 18;
t_compressed := t_clen < t_len;
t_crc32 := dbms_lob.SUBSTR( t_blob, 4, t_clen + 11 );
END IF;
IF NOT t_compressed
THEN
t_clen := t_len;
t_blob := p_content;
END IF;
IF p_zipped_blob IS NULL
THEN
dbms_lob.createtemporary( p_zipped_blob, TRUE );
END IF;
t_name := utl_i18n.string_to_raw( p_name, 'AL32UTF8' );
dbms_lob.append( p_zipped_blob
, utl_raw.CONCAT( c_LOCAL_FILE_HEADER -- Local file header signature
, HEXTORAW( '1400' ) -- version 2.0
, CASE WHEN t_name = utl_i18n.string_to_raw( p_name, 'US8PC437' )
THEN HEXTORAW( '0000' ) -- no General purpose bits
ELSE HEXTORAW( '0008' ) -- set Language encoding flag (EFS)
END
, CASE WHEN t_compressed
THEN HEXTORAW( '0800' ) -- deflate
ELSE HEXTORAW( '0000' ) -- stored
END
, little_endian( TO_NUMBER( TO_CHAR( t_now, 'ss' ) ) / 2
+ TO_NUMBER( TO_CHAR( t_now, 'mi' ) ) * 32
+ TO_NUMBER( TO_CHAR( t_now, 'hh24' ) ) * 2048
, 2
) -- File last modification time
, little_endian( TO_NUMBER( TO_CHAR( t_now, 'dd' ) )
+ TO_NUMBER( TO_CHAR( t_now, 'mm' ) ) * 32
+ ( TO_NUMBER( TO_CHAR( t_now, 'yyyy' ) ) - 1980 ) * 512
, 2
) -- File last modification date
, t_crc32 -- CRC-32
, little_endian( t_clen ) -- compressed size
, little_endian( t_len ) -- uncompressed size
, little_endian( utl_raw.LENGTH( t_name ), 2 ) -- File name length
, HEXTORAW( '0000' ) -- Extra field length
, t_name -- File name
)
);
IF t_compressed
THEN
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 11 ); -- compressed content
ELSIF t_clen > 0
THEN
dbms_lob.copy( p_zipped_blob, t_blob, t_clen, dbms_lob.getlength( p_zipped_blob ) + 1, 1 ); -- content
END IF;
IF dbms_lob.istemporary( t_blob ) = 1
THEN
dbms_lob.freetemporary( t_blob );
END IF;
END;
--
PROCEDURE finish_zip( p_zipped_blob IN OUT blob )
IS
t_cnt PLS_INTEGER := 0;
t_offs INTEGER;
t_offs_dir_header INTEGER;
t_offs_end_header INTEGER;
t_comment RAW(32767) := utl_raw.cast_to_raw( 'Implementation by Anton Scheffer' );
BEGIN
t_offs_dir_header := dbms_lob.getlength( p_zipped_blob );
t_offs := 1;
WHILE dbms_lob.SUBSTR( p_zipped_blob, utl_raw.LENGTH( c_LOCAL_FILE_HEADER ), t_offs ) = c_LOCAL_FILE_HEADER
LOOP
t_cnt := t_cnt + 1;
dbms_lob.append( p_zipped_blob
, utl_raw.CONCAT( HEXTORAW( '504B0102' ) -- Central directory file header signature
, HEXTORAW( '1400' ) -- version 2.0
, dbms_lob.SUBSTR( p_zipped_blob, 26, t_offs + 4 )
, HEXTORAW( '0000' ) -- File comment length
, HEXTORAW( '0000' ) -- Disk number where file starts
, HEXTORAW( '0000' ) -- Internal file attributes =>
-- 0000 binary file
-- 0100 (ascii)text file
, CASE
WHEN dbms_lob.SUBSTR( p_zipped_blob
, 1
, t_offs + 30 + blob2num( p_zipped_blob, 2, t_offs + 26 ) - 1
) IN ( HEXTORAW( '2F' ) -- /
, HEXTORAW( '5C' ) --
)
THEN HEXTORAW( '10000000' ) -- a directory/folder
ELSE HEXTORAW( '2000B681' ) -- a file
END -- External file attributes
, little_endian( t_offs - 1 ) -- Relative offset of local file header
, dbms_lob.SUBSTR( p_zipped_blob
, blob2num( p_zipped_blob, 2, t_offs + 26 )
, t_offs + 30
) -- File name
)
);
t_offs := t_offs + 30 + blob2num( p_zipped_blob, 4, t_offs + 18 ) -- compressed size
+ blob2num( p_zipped_blob, 2, t_offs + 26 ) -- File name length
+ blob2num( p_zipped_blob, 2, t_offs + 28 ); -- Extra field length
END LOOP;
t_offs_end_header := dbms_lob.getlength( p_zipped_blob );
dbms_lob.append( p_zipped_blob
, utl_raw.CONCAT( c_END_OF_CENTRAL_DIRECTORY -- End of central directory signature
, HEXTORAW( '0000' ) -- Number of this disk
, HEXTORAW( '0000' ) -- Disk where central directory starts
, little_endian( t_cnt, 2 ) -- Number of central directory records on this disk
, little_endian( t_cnt, 2 ) -- Total number of central directory records
, little_endian( t_offs_end_header - t_offs_dir_header ) -- Size of central directory
, little_endian( t_offs_dir_header ) -- Offset of start of central directory, relative to start of archive
, little_endian( NVL( utl_raw.LENGTH( t_comment ), 0 ), 2 ) -- ZIP file comment length
, t_comment
)
);
END;
--
FUNCTION alfan_col( p_col PLS_INTEGER )
RETURN VARCHAR2
IS
BEGIN
RETURN CASE
WHEN p_col > 702 THEN CHR( 64 + TRUNC( ( p_col - 27 ) / 676 ) ) || CHR( 65 + MOD( TRUNC( ( p_col - 1 ) / 26 ) - 1, 26 ) ) || CHR( 65 + MOD( p_col - 1, 26 ) )
WHEN p_col > 26 THEN CHR( 64 + TRUNC( ( p_col - 1 ) / 26 ) ) || CHR( 65 + MOD( p_col - 1, 26 ) )
ELSE CHR( 64 + p_col )
END;
END;
--
FUNCTION col_alfan( p_col VARCHAR2 )
RETURN PLS_INTEGER
IS
BEGIN
RETURN ASCII( SUBSTR( p_col, -1 ) ) - 64
+ NVL( ( ASCII( SUBSTR( p_col, -2, 1 ) ) - 64 ) * 26, 0 )
+ NVL( ( ASCII( SUBSTR( p_col, -3, 1 ) ) - 64 ) * 676, 0 );
END;
--
PROCEDURE clear_workbook
IS
t_row_ind PLS_INTEGER;
BEGIN
FOR s IN 1 .. workbook.sheets.COUNT()
LOOP
t_row_ind := workbook.sheets( s ).ROWS.FIRST();
WHILE t_row_ind IS NOT NULL
LOOP
workbook.sheets( s ).ROWS( t_row_ind ).DELETE();
t_row_ind := workbook.sheets( s ).ROWS.next( t_row_ind );
END LOOP;
workbook.sheets( s ).ROWS.DELETE();
workbook.sheets( s ).widths.DELETE();
workbook.sheets( s ).autofilters.DELETE();
workbook.sheets( s ).hyperlinks.DELETE();
workbook.sheets( s ).col_fmts.DELETE();
workbook.sheets( s ).row_fmts.DELETE();
workbook.sheets( s ).comments.DELETE();
workbook.sheets( s ).mergecells.DELETE();
workbook.sheets( s ).validations.DELETE();
END LOOP;
workbook.strings.DELETE();
workbook.str_ind.DELETE();
workbook.fonts.DELETE();
workbook.fills.DELETE();
workbook.borders.DELETE();
workbook.numFmts.DELETE();
workbook.cellXfs.DELETE();
workbook.defined_names.DELETE();
workbook := NULL;
END;
--
PROCEDURE new_sheet( p_sheetname VARCHAR2 := NULL )
IS
t_nr PLS_INTEGER := workbook.sheets.COUNT() + 1;
t_ind PLS_INTEGER;
BEGIN
workbook.sheets( t_nr ).name := NVL( dbms_xmlgen.CONVERT( TRANSLATE( p_sheetname, 'a/[]*:?', 'a' ) ), 'Sheet' || t_nr );
IF workbook.strings.COUNT() = 0
THEN
workbook.str_cnt := 0;
END IF;
IF workbook.fonts.COUNT() = 0
THEN
t_ind := get_font( 'Calibri' );
END IF;
IF workbook.fills.COUNT() = 0
THEN
t_ind := get_fill( 'none' );
t_ind := get_fill( 'gray125' );
END IF;
IF workbook.borders.COUNT() = 0
THEN
t_ind := get_border( '', '', '', '' );
END IF;
END;
--
PROCEDURE set_col_width
( p_sheet PLS_INTEGER
, p_col PLS_INTEGER
, p_format VARCHAR2
)
IS
t_width NUMBER;
t_nr_chr PLS_INTEGER;
BEGIN
IF p_format IS NULL
THEN
RETURN;
END IF;
IF INSTR( p_format, ';' ) > 0
THEN
t_nr_chr := LENGTH( TRANSLATE( SUBSTR( p_format, 1, INSTR( p_format, ';' ) - 1 ), 'a"', 'a' ) );
ELSE
t_nr_chr := LENGTH( TRANSLATE( p_format, 'a"', 'a' ) );
END IF;
t_width := TRUNC( ( t_nr_chr * 7 + 5 ) / 7 * 256 ) / 256; -- assume default 11 point Calibri
IF workbook.sheets( p_sheet ).widths.EXISTS( p_col )
THEN
workbook.sheets( p_sheet ).widths( p_col ) :=
GREATEST( workbook.sheets( p_sheet ).widths( p_col )
, t_width
);
ELSE
workbook.sheets( p_sheet ).widths( p_col ) := GREATEST( t_width, 8.43 );
END IF;
END;
--
FUNCTION OraFmt2Excel( p_format VARCHAR2 := NULL )
RETURN VARCHAR2
IS
t_format VARCHAR2(1000) := SUBSTR( p_format, 1, 1000 );
BEGIN
t_format := REPLACE( REPLACE( t_format, 'hh24', 'hh' ), 'hh12', 'hh' );
t_format := REPLACE( t_format, 'mi', 'mm' );
t_format := REPLACE( REPLACE( REPLACE( t_format, 'AM', '~~' ), 'PM', '~~' ), '~~', 'AM/PM' );
t_format := REPLACE( REPLACE( REPLACE( t_format, 'am', '~~' ), 'pm', '~~' ), '~~', 'AM/PM' );
t_format := REPLACE( REPLACE( t_format, 'day', 'DAY' ), 'DAY', 'dddd' );
t_format := REPLACE( REPLACE( t_format, 'dy', 'DY' ), 'DAY', 'ddd' );
t_format := REPLACE( REPLACE( t_format, 'RR', 'RR' ), 'RR', 'YY' );
t_format := REPLACE( REPLACE( t_format, 'month', 'MONTH' ), 'MONTH', 'mmmm' );
t_format := REPLACE( REPLACE( t_format, 'mon', 'MON' ), 'MON', 'mmm' );
RETURN t_format;
END;
--
FUNCTION get_numFmt( p_format VARCHAR2 := NULL )
RETURN PLS_INTEGER
IS
t_cnt PLS_INTEGER;
t_numFmtId PLS_INTEGER;
BEGIN
IF p_format IS NULL
THEN
RETURN 0;
END IF;
t_cnt := workbook.numFmts.COUNT();
FOR i IN 1 .. t_cnt
LOOP
IF workbook.numFmts( i ).formatCode = p_format
THEN
t_numFmtId := workbook.numFmts( i ).numFmtId;
EXIT;
END IF;
END LOOP;
IF t_numFmtId IS NULL
THEN
t_numFmtId := CASE WHEN t_cnt = 0 THEN 164 ELSE workbook.numFmts( t_cnt ).numFmtId + 1 END;
t_cnt := t_cnt + 1;
workbook.numFmts( t_cnt ).numFmtId := t_numFmtId;
workbook.numFmts( t_cnt ).formatCode := p_format;
workbook.numFmtIndexes( t_numFmtId ) := t_cnt;
END IF;
RETURN t_numFmtId;
END;
--
FUNCTION get_font
( p_name VARCHAR2
, p_family PLS_INTEGER := 2
, p_fontsize NUMBER := 11
, p_theme PLS_INTEGER := 1
, p_underline BOOLEAN := FALSE
, p_italic BOOLEAN := FALSE
, p_bold BOOLEAN := FALSE
, p_rgb VARCHAR2 := NULL -- this is a hex ALPHA Red Green Blue value
)
RETURN PLS_INTEGER
IS
t_ind PLS_INTEGER;
BEGIN
IF workbook.fonts.COUNT() > 0
THEN
FOR f IN 0 .. workbook.fonts.COUNT() - 1
LOOP
IF ( workbook.fonts( f ).name = p_name
AND workbook.fonts( f ).family = p_family
AND workbook.fonts( f ).fontsize = p_fontsize
AND workbook.fonts( f ).theme = p_theme
AND workbook.fonts( f ).underline = p_underline
AND workbook.fonts( f ).italic = p_italic
AND workbook.fonts( f ).bold = p_bold
AND ( workbook.fonts( f ).rgb = p_rgb
OR ( workbook.fonts( f ).rgb IS NULL AND p_rgb IS NULL )
)
)
THEN
RETURN f;
END IF;
END LOOP;
END IF;
t_ind := workbook.fonts.COUNT();
workbook.fonts( t_ind ).name := p_name;
workbook.fonts( t_ind ).family := p_family;
workbook.fonts( t_ind ).fontsize := p_fontsize;
workbook.fonts( t_ind ).theme := p_theme;
workbook.fonts( t_ind ).underline := p_underline;
workbook.fonts( t_ind ).italic := p_italic;
workbook.fonts( t_ind ).bold := p_bold;
workbook.fonts( t_ind ).rgb := p_rgb;
RETURN t_ind;
END;
--
FUNCTION get_fill
( p_patternType VARCHAR2
, p_fgRGB VARCHAR2 := NULL
)
RETURN PLS_INTEGER
IS
t_ind PLS_INTEGER;
BEGIN
IF workbook.fills.COUNT() > 0
THEN
FOR f IN 0 .. workbook.fills.COUNT() - 1
LOOP
IF ( workbook.fills( f ).patternType = p_patternType
AND NVL( workbook.fills( f ).fgRGB, 'x' ) = NVL( UPPER( p_fgRGB ), 'x' )
)
THEN
RETURN f;
END IF;
END LOOP;
END IF;
t_ind := workbook.fills.COUNT();
workbook.fills( t_ind ).patternType := p_patternType;
workbook.fills( t_ind ).fgRGB := UPPER( p_fgRGB );
RETURN t_ind;
END;
--
FUNCTION get_border
( p_top VARCHAR2 := 'thin'
, p_bottom VARCHAR2 := 'thin'
, p_left VARCHAR2 := 'thin'
, p_right VARCHAR2 := 'thin'
)
RETURN PLS_INTEGER
IS
t_ind PLS_INTEGER;
BEGIN
IF workbook.borders.COUNT() > 0
THEN
FOR b IN 0 .. workbook.borders.COUNT() - 1
LOOP
IF ( NVL( workbook.borders( b ).top, 'x' ) = NVL( p_top, 'x' )
AND NVL( workbook.borders( b ).bottom, 'x' ) = NVL( p_bottom, 'x' )
AND NVL( workbook.borders( b ).left, 'x' ) = NVL( p_left, 'x' )
AND NVL( workbook.borders( b ).right, 'x' ) = NVL( p_right, 'x' )
)
THEN
RETURN b;
END IF;
END LOOP;
END IF;
t_ind := workbook.borders.COUNT();
workbook.borders( t_ind ).top := p_top;
workbook.borders( t_ind ).bottom := p_bottom;
workbook.borders( t_ind ).left := p_left;
workbook.borders( t_ind ).right := p_right;
RETURN t_ind;
END;
--
FUNCTION get_alignment
( p_vertical VARCHAR2 := NULL
, p_horizontal VARCHAR2 := NULL
, p_wrapText BOOLEAN := NULL
)
RETURN tp_alignment
IS
t_rv tp_alignment;
BEGIN
t_rv.vertical := p_vertical;
t_rv.horizontal := p_horizontal;
t_rv.wrapText := p_wrapText;
RETURN t_rv;
END;
--
FUNCTION get_XfId
( p_sheet PLS_INTEGER
, p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
)
RETURN VARCHAR2
IS
t_cnt PLS_INTEGER;
t_XfId PLS_INTEGER;
t_XF tp_XF_fmt;
t_col_XF tp_XF_fmt;
t_row_XF tp_XF_fmt;
BEGIN
IF workbook.sheets( p_sheet ).col_fmts.EXISTS( p_col )
THEN
t_col_XF := workbook.sheets( p_sheet ).col_fmts( p_col );
END IF;
IF workbook.sheets( p_sheet ).row_fmts.EXISTS( p_row )
THEN
t_row_XF := workbook.sheets( p_sheet ).row_fmts( p_row );
END IF;
t_XF.numFmtId := COALESCE( p_numFmtId, t_col_XF.numFmtId, t_row_XF.numFmtId, 0 );
t_XF.fontId := COALESCE( p_fontId, t_col_XF.fontId, t_row_XF.fontId, 0 );
t_XF.fillId := COALESCE( p_fillId, t_col_XF.fillId, t_row_XF.fillId, 0 );
t_XF.borderId := COALESCE( p_borderId, t_col_XF.borderId, t_row_XF.borderId, 0 );
t_XF.alignment := COALESCE( p_alignment, t_col_XF.alignment, t_row_XF.alignment );
IF ( t_XF.numFmtId + t_XF.fontId + t_XF.fillId + t_XF.borderId = 0
AND t_XF.alignment.vertical IS NULL
AND t_XF.alignment.horizontal IS NULL
AND NOT NVL( t_XF.alignment.wrapText, FALSE )
)
THEN
RETURN '';
END IF;
IF t_XF.numFmtId > 0
THEN
set_col_width( p_sheet, p_col, workbook.numFmts( workbook.numFmtIndexes( t_XF.numFmtId ) ).formatCode );
END IF;
t_cnt := workbook.cellXfs.COUNT();
FOR i IN 1 .. t_cnt
LOOP
IF ( workbook.cellXfs( i ).numFmtId = t_XF.numFmtId
AND workbook.cellXfs( i ).fontId = t_XF.fontId
AND workbook.cellXfs( i ).fillId = t_XF.fillId
AND workbook.cellXfs( i ).borderId = t_XF.borderId
AND NVL( workbook.cellXfs( i ).alignment.vertical, 'x' ) = NVL( t_XF.alignment.vertical, 'x' )
AND NVL( workbook.cellXfs( i ).alignment.horizontal, 'x' ) = NVL( t_XF.alignment.horizontal, 'x' )
AND NVL( workbook.cellXfs( i ).alignment.wrapText, FALSE ) = NVL( t_XF.alignment.wrapText, FALSE )
)
THEN
t_XfId := i;
EXIT;
END IF;
END LOOP;
IF t_XfId IS NULL
THEN
t_cnt := t_cnt + 1;
t_XfId := t_cnt;
workbook.cellXfs( t_cnt ) := t_XF;
END IF;
RETURN 's="' || t_XfId || '"';
END;
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value NUMBER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).VALUE := p_value;
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).style := NULL;
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).style := get_XfId( t_sheet, p_col, p_row, p_numFmtId, p_fontId, p_fillId, p_borderId, p_alignment );
END;
--
FUNCTION add_string( p_string VARCHAR2 )
RETURN PLS_INTEGER
IS
t_cnt PLS_INTEGER;
BEGIN
IF workbook.strings.EXISTS( p_string )
THEN
t_cnt := workbook.strings( p_string );
ELSE
t_cnt := workbook.strings.COUNT();
workbook.str_ind( t_cnt ) := p_string;
workbook.strings( NVL( p_string, '' ) ) := t_cnt;
END IF;
workbook.str_cnt := workbook.str_cnt + 1;
RETURN t_cnt;
END;
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value VARCHAR2
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
t_alignment tp_alignment := p_alignment;
BEGIN
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).VALUE := add_string( p_value );
IF t_alignment.wrapText IS NULL AND INSTR( p_value, CHR(13) ) > 0
THEN
t_alignment.wrapText := TRUE;
END IF;
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).style := 't="s" ' || get_XfId( t_sheet, p_col, p_row, p_numFmtId, p_fontId, p_fillId, p_borderId, t_alignment );
END;
--
PROCEDURE cell
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_value DATE
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_numFmtId PLS_INTEGER := p_numFmtId;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).VALUE := p_value - TO_DATE('01-01-1904','DD-MM-YYYY');
IF t_numFmtId IS NULL
AND NOT ( workbook.sheets( t_sheet ).col_fmts.EXISTS( p_col )
AND workbook.sheets( t_sheet ).col_fmts( p_col ).numFmtId IS NOT NULL
)
AND NOT ( workbook.sheets( t_sheet ).row_fmts.EXISTS( p_row )
AND workbook.sheets( t_sheet ).row_fmts( p_row ).numFmtId IS NOT NULL
)
THEN
t_numFmtId := get_numFmt( 'dd/mm/yyyy' );
END IF;
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).style := get_XfId( t_sheet, p_col, p_row, t_numFmtId, p_fontId, p_fillId, p_borderId, p_alignment );
END;
--
PROCEDURE hyperlink
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_url VARCHAR2
, p_value VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).VALUE := add_string( NVL( p_value, p_url ) );
workbook.sheets( t_sheet ).ROWS( p_row )( p_col ).style := 't="s" ' || get_XfId( t_sheet, p_col, p_row, '', get_font( 'Calibri', p_theme => 10, p_underline => TRUE ) );
t_ind := workbook.sheets( t_sheet ).hyperlinks.COUNT() + 1;
workbook.sheets( t_sheet ).hyperlinks( t_ind ).cell := alfan_col( p_col ) || p_row;
workbook.sheets( t_sheet ).hyperlinks( t_ind ).url := p_url;
END;
--
PROCEDURE COMMENT
( p_col PLS_INTEGER
, p_row PLS_INTEGER
, p_text VARCHAR2
, p_author VARCHAR2 := NULL
, p_width PLS_INTEGER := 150
, p_height PLS_INTEGER := 100
, p_sheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
t_ind := workbook.sheets( t_sheet ).comments.COUNT() + 1;
workbook.sheets( t_sheet ).comments( t_ind ).ROW := p_row;
workbook.sheets( t_sheet ).comments( t_ind ).COLUMN := p_col;
workbook.sheets( t_sheet ).comments( t_ind ).text := dbms_xmlgen.CONVERT( p_text );
workbook.sheets( t_sheet ).comments( t_ind ).author := dbms_xmlgen.CONVERT( p_author );
workbook.sheets( t_sheet ).comments( t_ind ).width := p_width;
workbook.sheets( t_sheet ).comments( t_ind ).height := p_height;
END;
--
PROCEDURE mergecells
( p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_sheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
t_ind := workbook.sheets( t_sheet ).mergecells.COUNT() + 1;
workbook.sheets( t_sheet ).mergecells( t_ind ) := alfan_col( p_tl_col ) || p_tl_row || ':' || alfan_col( p_br_col ) || p_br_row;
END;
--
PROCEDURE add_validation
( p_type VARCHAR2
, p_sqref VARCHAR2
, p_style VARCHAR2 := 'stop' -- stop, warning, information
, p_formula1 VARCHAR2 := NULL
, p_formula2 VARCHAR2 := NULL
, p_title VARCHAR2 := NULL
, p_prompt VARCHAR := NULL
, p_show_error BOOLEAN := FALSE
, p_error_title VARCHAR2 := NULL
, p_error_txt VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
t_ind := workbook.sheets( t_sheet ).validations.COUNT() + 1;
workbook.sheets( t_sheet ).validations( t_ind ).TYPE := p_type;
workbook.sheets( t_sheet ).validations( t_ind ).errorstyle := p_style;
workbook.sheets( t_sheet ).validations( t_ind ).sqref := p_sqref;
workbook.sheets( t_sheet ).validations( t_ind ).formula1 := p_formula1;
workbook.sheets( t_sheet ).validations( t_ind ).error_title := p_error_title;
workbook.sheets( t_sheet ).validations( t_ind ).error_txt := p_error_txt;
workbook.sheets( t_sheet ).validations( t_ind ).title := p_title;
workbook.sheets( t_sheet ).validations( t_ind ).prompt := p_prompt;
workbook.sheets( t_sheet ).validations( t_ind ).showerrormessage := p_show_error;
END;
--
PROCEDURE list_validation
( p_sqref_col PLS_INTEGER
, p_sqref_row PLS_INTEGER
, p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_style VARCHAR2 := 'stop' -- stop, warning, information
, p_title VARCHAR2 := NULL
, p_prompt VARCHAR := NULL
, p_show_error BOOLEAN := FALSE
, p_error_title VARCHAR2 := NULL
, p_error_txt VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
BEGIN
add_validation( 'list'
, alfan_col( p_sqref_col ) || p_sqref_row
, p_style => LOWER( p_style )
, p_formula1 => '$' || alfan_col( p_tl_col ) || '$' || p_tl_row || ':$' || alfan_col( p_br_col ) || '$' || p_br_row
, p_title => p_title
, p_prompt => p_prompt
, p_show_error => p_show_error
, p_error_title => p_error_title
, p_error_txt => p_error_txt
, p_sheet => p_sheet
);
END;
--
PROCEDURE list_validation
( p_sqref_col PLS_INTEGER
, p_sqref_row PLS_INTEGER
, p_defined_name VARCHAR2
, p_style VARCHAR2 := 'stop' -- stop, warning, information
, p_title VARCHAR2 := NULL
, p_prompt VARCHAR := NULL
, p_show_error BOOLEAN := FALSE
, p_error_title VARCHAR2 := NULL
, p_error_txt VARCHAR2 := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
BEGIN
add_validation( 'list'
, alfan_col( p_sqref_col ) || p_sqref_row
, p_style => LOWER( p_style )
, p_formula1 => p_defined_name
, p_title => p_title
, p_prompt => p_prompt
, p_show_error => p_show_error
, p_error_title => p_error_title
, p_error_txt => p_error_txt
, p_sheet => p_sheet
);
END;
--
PROCEDURE defined_name
( p_tl_col PLS_INTEGER -- top left
, p_tl_row PLS_INTEGER
, p_br_col PLS_INTEGER -- bottom right
, p_br_row PLS_INTEGER
, p_name VARCHAR2
, p_sheet PLS_INTEGER := NULL
, p_localsheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
t_ind := workbook.defined_names.COUNT() + 1;
workbook.defined_names( t_ind ).name := p_name;
workbook.defined_names( t_ind ).REF := 'Sheet' || t_sheet || '!$' || alfan_col( p_tl_col ) || '$' || p_tl_row || ':$' || alfan_col( p_br_col ) || '$' || p_br_row;
workbook.defined_names( t_ind ).sheet := p_localsheet;
END;
--
PROCEDURE set_column_width
( p_col PLS_INTEGER
, p_width NUMBER
, p_sheet PLS_INTEGER := NULL
)
IS
BEGIN
workbook.sheets( NVL( p_sheet, workbook.sheets.COUNT() ) ).widths( p_col ) := p_width;
END;
--
PROCEDURE set_column
( p_col PLS_INTEGER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
workbook.sheets( t_sheet ).col_fmts( p_col ).numFmtId := p_numFmtId;
workbook.sheets( t_sheet ).col_fmts( p_col ).fontId := p_fontId;
workbook.sheets( t_sheet ).col_fmts( p_col ).fillId := p_fillId;
workbook.sheets( t_sheet ).col_fmts( p_col ).borderId := p_borderId;
workbook.sheets( t_sheet ).col_fmts( p_col ).alignment := p_alignment;
END;
--
PROCEDURE set_row
( p_row PLS_INTEGER
, p_numFmtId PLS_INTEGER := NULL
, p_fontId PLS_INTEGER := NULL
, p_fillId PLS_INTEGER := NULL
, p_borderId PLS_INTEGER := NULL
, p_alignment tp_alignment := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
workbook.sheets( t_sheet ).row_fmts( p_row ).numFmtId := p_numFmtId;
workbook.sheets( t_sheet ).row_fmts( p_row ).fontId := p_fontId;
workbook.sheets( t_sheet ).row_fmts( p_row ).fillId := p_fillId;
workbook.sheets( t_sheet ).row_fmts( p_row ).borderId := p_borderId;
workbook.sheets( t_sheet ).row_fmts( p_row ).alignment := p_alignment;
END;
--
PROCEDURE freeze_rows
( p_nr_rows PLS_INTEGER := 1
, p_sheet PLS_INTEGER := NULL
)
IS
BEGIN
workbook.sheets( NVL( p_sheet, workbook.sheets.COUNT() ) ).freeze_rows := p_nr_rows;
END;
--
PROCEDURE freeze_cols
( p_nr_cols PLS_INTEGER := 1
, p_sheet PLS_INTEGER := NULL
)
IS
BEGIN
workbook.sheets( NVL( p_sheet, workbook.sheets.COUNT() ) ).freeze_cols := p_nr_cols;
END;
--
PROCEDURE set_autofilter
( p_column_start PLS_INTEGER := NULL
, p_column_end PLS_INTEGER := NULL
, p_row_start PLS_INTEGER := NULL
, p_row_end PLS_INTEGER := NULL
, p_sheet PLS_INTEGER := NULL
)
IS
t_ind PLS_INTEGER;
t_sheet PLS_INTEGER := NVL( p_sheet, workbook.sheets.COUNT() );
BEGIN
t_ind := 1;
workbook.sheets( t_sheet ).autofilters( t_ind ).column_start := p_column_start;
workbook.sheets( t_sheet ).autofilters( t_ind ).column_end := p_column_end;
workbook.sheets( t_sheet ).autofilters( t_ind ).row_start := p_row_start;
workbook.sheets( t_sheet ).autofilters( t_ind ).row_end := p_row_end;
defined_name
( p_column_start
, p_row_start
, p_column_end
, p_row_end
, '_xlnm._FilterDatabase'
, t_sheet
, t_sheet - 1
);
END;
--
/*
procedure add1xml
( p_excel in out nocopy blob
, p_filename varchar2
, p_xml clob
)
is
t_tmp blob;
c_step constant number := 24396;
begin
dbms_lob.createtemporary( t_tmp, true );
for i in 0 .. trunc( length( p_xml ) / c_step )
loop
dbms_lob.append( t_tmp, utl_i18n.string_to_raw( substr( p_xml, i * c_step + 1, c_step ), 'AL32UTF8' ) );
end loop;
add1file( p_excel, p_filename, t_tmp );
dbms_lob.freetemporary( t_tmp );
end;
*/
--
PROCEDURE add1xml
( p_excel IN OUT NOCOPY blob
, p_filename VARCHAR2
, p_xml clob
)
IS
t_tmp blob;
dest_offset INTEGER := 1;
src_offset INTEGER := 1;
lang_context INTEGER;
warning INTEGER;
BEGIN
lang_context := dbms_lob.DEFAULT_LANG_CTX;
dbms_lob.createtemporary( t_tmp, TRUE );
dbms_lob.converttoblob
( t_tmp
, p_xml
, dbms_lob.lobmaxsize
, dest_offset
, src_offset
, NLS_CHARSET_ID( 'AL32UTF8' )
, lang_context
, warning
);
add1file( p_excel, p_filename, t_tmp );
dbms_lob.freetemporary( t_tmp );
END;
--
FUNCTION finish
RETURN blob
IS
t_excel blob;
t_xxx clob;
t_tmp VARCHAR2(32767 CHAR);
t_str VARCHAR2(32767 CHAR);
t_c NUMBER;
t_h NUMBER;
t_w NUMBER;
t_cw NUMBER;
t_cell VARCHAR2(1000 CHAR);
t_row_ind PLS_INTEGER;
t_col_min PLS_INTEGER;
t_col_max PLS_INTEGER;
t_col_ind PLS_INTEGER;
t_len PLS_INTEGER;
ts TIMESTAMP := SYSTIMESTAMP;
BEGIN
dbms_lob.createtemporary( t_excel, TRUE );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->




';
FOR s IN 1 .. workbook.sheets.COUNT()
LOOP
t_xxx := t_xxx || '
';
END LOOP;
t_xxx := t_xxx || '




';
FOR s IN 1 .. workbook.sheets.COUNT()
LOOP
IF workbook.sheets( s ).comments.COUNT() > 0
THEN
t_xxx := t_xxx || '
';
END IF;
END LOOP;
t_xxx := t_xxx || '
';
add1xml( t_excel, '[Content_Types].xml', t_xxx );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

' || SYS_CONTEXT( 'userenv', 'os_user' ) || '
' || SYS_CONTEXT( 'userenv', 'os_user' ) || '
' || TO_CHAR( CURRENT_TIMESTAMP, 'yyyy-mm-dd"T"hh24:mi:ssTZH:TZM' ) || '
' || TO_CHAR( CURRENT_TIMESTAMP, 'yyyy-mm-dd"T"hh24:mi:ssTZH:TZM' ) || '
';
add1xml( t_excel, 'docProps/core.xml', t_xxx );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

Microsoft Excel
0
false



Worksheets


' || workbook.sheets.COUNT() || '




';
FOR s IN 1 .. workbook.sheets.COUNT()
LOOP
t_xxx := t_xxx || '
' || workbook.sheets( s ).name || '';
END LOOP;
t_xxx := t_xxx || '

false
false
false
14.0300
';
add1xml( t_excel, 'docProps/app.xml', t_xxx );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->




';
add1xml( t_excel, '_rels/.rels', t_xxx );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

';
IF workbook.numFmts.COUNT() > 0
THEN
t_xxx := t_xxx || '';
FOR n IN 1 .. workbook.numFmts.COUNT()
LOOP
t_xxx := t_xxx || '';
END LOOP;
t_xxx := t_xxx || '';
END IF;
t_xxx := t_xxx || '';
FOR f IN 0 .. workbook.fonts.COUNT() - 1
LOOP
t_xxx := t_xxx || '' ||
CASE WHEN workbook.fonts( f ).bold THEN '<b></b>' END ||
CASE WHEN workbook.fonts( f ).italic THEN '<i></i>' END ||
CASE WHEN workbook.fonts( f ).underline THEN '' END ||
'

<color ' || CASE WHEN workbook.fonts( f ).rgb IS NOT NULL
THEN 'rgb="' || workbook.fonts( f ).rgb
ELSE 'theme="' || workbook.fonts( f ).theme
END || '"/>



';
END LOOP;
t_xxx := t_xxx || '
';
FOR f IN 0 .. workbook.fills.COUNT() - 1
LOOP
t_xxx := t_xxx || ' ' ||
CASE WHEN workbook.fills( f ).fgRGB IS NOT NULL THEN '' END ||
'';
END LOOP;
t_xxx := t_xxx || '
';
FOR b IN 0 .. workbook.borders.COUNT() - 1
LOOP
t_xxx := t_xxx || '' ||
CASE WHEN workbook.borders( b ).left IS NULL THEN '' ELSE '' END ||
CASE WHEN workbook.borders( b ).right IS NULL THEN '' ELSE '' END ||
CASE WHEN workbook.borders( b ).top IS NULL THEN '' ELSE '' END ||
CASE WHEN workbook.borders( b ).bottom IS NULL THEN '' ELSE '' END ||
'';
END LOOP;
t_xxx := t_xxx || '




';
FOR x IN 1 .. workbook.cellXfs.COUNT()
LOOP
t_xxx := t_xxx || '';
IF ( workbook.cellXfs( x ).alignment.horizontal IS NOT NULL
OR workbook.cellXfs( x ).alignment.vertical IS NOT NULL
OR workbook.cellXfs( x ).alignment.wrapText
)
THEN
t_xxx := t_xxx || '<alignment' ||
CASE WHEN workbook.cellXfs( x ).alignment.horizontal IS NOT NULL THEN ' horizontal="' || workbook.cellXfs( x ).alignment.horizontal || '"' END ||
CASE WHEN workbook.cellXfs( x ).alignment.vertical IS NOT NULL THEN ' vertical="' || workbook.cellXfs( x ).alignment.vertical || '"' END ||
CASE WHEN workbook.cellXfs( x ).alignment.wrapText THEN ' wrapText="true"' END || '/>';
END IF;
t_xxx := t_xxx || '';
END LOOP;
t_xxx := t_xxx || '








';
add1xml( t_excel, 'xl/styles.xml', t_xxx );
t_xxx := '

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->






';
FOR s IN 1 .. workbook.sheets.COUNT()
LOOP
t_xxx := t_xxx || '
';
END LOOP;
t_xxx := t_xxx || '';
IF workbook.defined_names.COUNT() > 0
THEN
t_xxx := t_xxx || '';
FOR s IN 1 .. workbook.defined_names.COUNT()
LOOP
t_xxx := t_xxx || '
case when workbook.defined_names( s ).sheet is not null then ' localSheetId="' || to_char( workbook.defined_names( s ).sheet ) || '"' end ||
'>' || workbook.defined_names( s ).ref || '';
end loop;
t_xxx := t_xxx || '';
end if;
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/workbook.xml', t_xxx );
t_xxx := '

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

























































































































































































































































































';
add1xml( t_excel, 'xl/theme/theme1.xml', t_xxx );
for s in 1 .. workbook.sheets.count()
loop
t_col_min := 16384;
t_col_max := 1;
t_row_ind := workbook.sheets( s ).rows.first();
while t_row_ind is not null
loop
t_col_min := least( t_col_min, workbook.sheets( s ).rows( t_row_ind ).first() );
t_col_max := greatest( t_col_max, workbook.sheets( s ).rows( t_row_ind ).last() );
t_row_ind := workbook.sheets( s ).rows.next( t_row_ind );
end loop;
t_xxx := '

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->



<sheetView' || case when s = 1 then ' tabSelected="1"' end || ' workbookViewId="0">';
if workbook.sheets( s ).freeze_rows > 0
then
t_xxx := t_xxx || ' ';
end if;
if workbook.sheets( s ).freeze_cols > 0
then
t_xxx := t_xxx || ' ';
end if;
t_xxx := t_xxx || '

';
if workbook.sheets( s ).widths.count() > 0
then
t_xxx := t_xxx || '
';
t_col_ind := workbook.sheets( s ).widths.first();
while t_col_ind is not null
loop
t_xxx := t_xxx ||
'
';
t_col_ind := workbook.sheets( s ).widths.next( t_col_ind );
end loop;
t_xxx := t_xxx || '';
end if;
t_xxx := t_xxx || '';
t_row_ind := workbook.sheets( s ).rows.first();
t_tmp := null;
while t_row_ind is not null
loop
t_tmp := t_tmp || '';
t_len := length( t_tmp );
t_col_ind := workbook.sheets( s ).rows( t_row_ind ).first();
while t_col_ind is not null
loop
t_cell := ' || ' ' || workbook.sheets( s ).ROWS( t_row_ind )( t_col_ind ).style
|| '>'
|| TO_CHAR( workbook.sheets( s ).ROWS( t_row_ind )( t_col_ind ).VALUE, 'TM9', 'NLS_NUMERIC_CHARACTERS=.,' )
|| '';
IF t_len > 32000
THEN
dbms_lob.writeappend( t_xxx, t_len, t_tmp );
t_tmp := NULL;
t_len := 0;
END IF;
t_tmp := t_tmp || t_cell;
t_len := t_len + LENGTH( t_cell );
t_col_ind := workbook.sheets( s ).ROWS( t_row_ind ).next( t_col_ind );
END LOOP;
t_tmp := t_tmp || '';
t_row_ind := workbook.sheets( s ).ROWS.next( t_row_ind );
END LOOP;
t_tmp := t_tmp || '';
t_len := LENGTH( t_tmp );
dbms_lob.writeappend( t_xxx, t_len, t_tmp );
FOR a IN 1 .. workbook.sheets( s ).autofilters.COUNT()
LOOP
t_xxx := t_xxx || ' alfan_col( nvl( workbook.sheets( s ).autofilters( a ).column_start, t_col_min ) ) ||
nvl( workbook.sheets( s ).autofilters( a ).row_start, workbook.sheets( s ).rows.first() ) || ':' ||
alfan_col( coalesce( workbook.sheets( s ).autofilters( a ).column_end, workbook.sheets( s ).autofilters( a ).column_start, t_col_max ) ) ||
nvl( workbook.sheets( s ).autofilters( a ).row_end, workbook.sheets( s ).rows.last() ) || '"/>';
end loop;
if workbook.sheets( s ).mergecells.count() > 0
then
t_xxx := t_xxx || '';
for m in 1 .. workbook.sheets( s ).mergecells.count()
loop
t_xxx := t_xxx || '';
end loop;
t_xxx := t_xxx || '';
end if;
--
if workbook.sheets( s ).validations.count() > 0
then
t_xxx := t_xxx || '';
for m in 1 .. workbook.sheets( s ).validations.count()
loop
t_xxx := t_xxx || '<dataValidation' ||
' type="' || workbook.sheets( s ).validations( m ).type || '"' ||
' errorStyle="' || workbook.sheets( s ).validations( m ).errorstyle || '"' ||
' allowBlank="' || case when nvl( workbook.sheets( s ).validations( m ).allowBlank, true ) then '1' else '0' end || '"' ||
' sqref="' || workbook.sheets( s ).validations( m ).sqref || '"';
if workbook.sheets( s ).validations( m ).prompt is not null
then
t_xxx := t_xxx || ' showInputMessage="1" prompt="' || workbook.sheets( s ).validations( m ).prompt || '"';
if workbook.sheets( s ).validations( m ).title is not null
then
t_xxx := t_xxx || ' promptTitle="' || workbook.sheets( s ).validations( m ).title || '"';
end if;
end if;
if workbook.sheets( s ).validations( m ).showerrormessage
then
t_xxx := t_xxx || ' showErrorMessage="1"';
if workbook.sheets( s ).validations( m ).error_title is not null
then
t_xxx := t_xxx || ' errorTitle="' || workbook.sheets( s ).validations( m ).error_title || '"';
end if;
if workbook.sheets( s ).validations( m ).error_txt is not null
then
t_xxx := t_xxx || ' error="' || workbook.sheets( s ).validations( m ).error_txt || '"';
end if;
end if;
t_xxx := t_xxx || '>';
if workbook.sheets( s ).validations( m ).formula1 is not null
then
t_xxx := t_xxx || '
' || workbook.sheets( s ).validations( m ).formula1 || '';
end if;
if workbook.sheets( s ).validations( m ).formula2 is not null
then
t_xxx := t_xxx || '
' || workbook.sheets( s ).validations( m ).formula2 || '';
end if;
t_xxx := t_xxx || '';
end loop;
t_xxx := t_xxx || '';
end if;
--
if workbook.sheets( s ).hyperlinks.count() > 0
then
t_xxx := t_xxx || '';
for h in 1 .. workbook.sheets( s ).hyperlinks.count()
loop
t_xxx := t_xxx || '';
end loop;
t_xxx := t_xxx || '';
end if;
t_xxx := t_xxx || ' ';
if workbook.sheets( s ).comments.count() > 0
then
t_xxx := t_xxx || '';
end if;
--
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/worksheets/sheet' || s || '.xml', t_xxx );
if workbook.sheets( s ).hyperlinks.count() > 0 or workbook.sheets( s ).comments.count() > 0
then
t_xxx := '

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->
';
if workbook.sheets( s ).comments.count() > 0
then
t_xxx := t_xxx || '';
t_xxx := t_xxx || '';
end if;
for h in 1 .. workbook.sheets( s ).hyperlinks.count()
loop
t_xxx := t_xxx || '';
end loop;
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/worksheets/_rels/sheet' || s || '.xml.rels', t_xxx );
end if;
--
if workbook.sheets( s ).comments.count() > 0
then
declare
cnt pls_integer;
author_ind tp_author;
-- t_col_ind := workbook.sheets( s ).widths.next( t_col_ind );
begin
authors.delete();
for c in 1 .. workbook.sheets( s ).comments.count()
loop
authors( workbook.sheets( s ).comments( c ).author ) := 0;
end loop;
t_xxx := '

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

';
cnt := 0;
author_ind := authors.first();
while author_ind is not null or authors.next( author_ind ) is not null
loop
authors( author_ind ) := cnt;
t_xxx := t_xxx || '' || author_ind || '';
cnt := cnt + 1;
author_ind := authors.next( author_ind );
end loop;
end;
t_xxx := t_xxx || '';
for c in 1 .. workbook.sheets( s ).comments.count()
loop
t_xxx := t_xxx || '
';
if workbook.sheets( s ).comments( c ).author is not null
then
t_xxx := t_xxx || '<b></b>
' ||
workbook.sheets( s ).comments( c ).author || ':';
end if;
t_xxx := t_xxx || '
' ||
case when workbook.sheets( s ).comments( c ).author is not null then '
' end || workbook.sheets( s ).comments( c ).text || '';
end loop;
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/comments' || s || '.xml', t_xxx );
t_xxx := '

';
for c in 1 .. workbook.sheets( s ).comments.count()
loop
t_xxx := t_xxx || ' style="position:absolute;margin-left:35.25pt;margin-top:3pt;z-INDEX:' || to_char( c ) || ';visibility:hidden;" fillcolor="#ffffe1" o:insetmode="auto">
<div style="text-align: left;"></div>
';
t_w := workbook.sheets( s ).comments( c ).width;
t_c := 1;
loop
if workbook.sheets( s ).widths.exists( workbook.sheets( s ).comments( c ).column + t_c )
then
t_cw := 256 * workbook.sheets( s ).widths( workbook.sheets( s ).comments( c ).column + t_c );
t_cw := trunc( ( t_cw + 18 ) / 256 * 7); -- assume default 11 point Calibri
else
t_cw := 64;
end if;
exit when t_w < t_cw;
t_c := t_c + 1;
t_w := t_w - t_cw;
end loop;
t_h := workbook.sheets( s ).comments( c ).height;
t_xxx := t_xxx || to_char( '' || workbook.sheets( s ).comments( c ).column || ',15,' ||
workbook.sheets( s ).comments( c ).row || ',30,' ||
( workbook.sheets( s ).comments( c ).column + t_c - 1 ) || ',' || round( t_w ) || ',' ||
( workbook.sheets( s ).comments( c ).row + 1 + trunc( t_h / 20 ) ) || ',' || mod( t_h, 20 ) || '' );
t_xxx := t_xxx || to_char( 'False' ||
( workbook.sheets( s ).comments( c ).row - 1 ) || '' ||
( workbook.sheets( s ).comments( c ).column - 1 ) || '' );
end loop;
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/drawings/vmlDrawing' || s || '.vml', t_xxx );
end if;
--
end loop;
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->



';
for s in 1 .. workbook.sheets.count()
loop
t_xxx := t_xxx || '
';
end loop;
t_xxx := t_xxx || '';
add1xml( t_excel, 'xl/_rels/workbook.xml.rels', t_xxx );
t_xxx := '<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->
';
t_tmp := null;
for i in 0 .. workbook.str_ind.count() - 1
loop
t_str := '' || dbms_xmlgen.convert( substr( workbook.str_ind( i ), 1, 32000 ) ) || '';
if length( t_tmp ) + length( t_str ) > 32000
then
t_xxx := t_xxx || t_tmp;
t_tmp := null;
end if;
t_tmp := t_tmp || t_str;
end loop;
t_xxx := t_xxx || t_tmp || '';
add1xml( t_excel, 'xl/sharedStrings.xml', t_xxx );
finish_zip( t_excel );
clear_workbook;
return t_excel;
end;
--
procedure save
( p_directory varchar2
, p_filename varchar2
)
is
begin
blob2file( finish, p_directory, p_filename );
end;
--
procedure query2sheet
( p_sql varchar2
, p_column_headers boolean := true
, p_directory varchar2 := null
, p_filename varchar2 := null
, p_sheet pls_integer := null
)
is
t_sheet pls_integer;
t_c integer;
t_col_cnt integer;
t_desc_tab dbms_sql.desc_tab2;
d_tab dbms_sql.date_table;
n_tab dbms_sql.number_table;
v_tab dbms_sql.varchar2_table;
t_bulk_size pls_integer := 200;
t_r integer;
t_cur_row pls_integer;
begin
if p_sheet is null
then
new_sheet;
end if;
t_c := dbms_sql.open_cursor;
dbms_sql.parse( t_c, p_sql, dbms_sql.native );
dbms_sql.describe_columns2( t_c, t_col_cnt, t_desc_tab );
for c in 1 .. t_col_cnt
loop
if p_column_headers
then
cell( c, 1, t_desc_tab( c ).col_name, p_sheet => t_sheet );
end if;
-- dbms_output.put_line( t_desc_tab( c ).col_name || ' ' || t_desc_tab( c ).col_type );
case
when t_desc_tab( c ).col_type in ( 2, 100, 101 )
then
dbms_sql.define_array( t_c, c, n_tab, t_bulk_size, 1 );
when t_desc_tab( c ).col_type in ( 12, 178, 179, 180, 181 , 231 )
then
dbms_sql.define_array( t_c, c, d_tab, t_bulk_size, 1 );
when t_desc_tab( c ).col_type in ( 1, 8, 9, 96, 112 )
then
dbms_sql.define_array( t_c, c, v_tab, t_bulk_size, 1 );
else
null;
end case;
end loop;
--
t_cur_row := case when p_column_headers then 2 else 1 end;
t_sheet := nvl( p_sheet, workbook.sheets.count() );
--
t_r := dbms_sql.execute( t_c );
loop
t_r := dbms_sql.fetch_rows( t_c );
if t_r > 0
then
for c in 1 .. t_col_cnt
loop
case
when t_desc_tab( c ).col_type in ( 2, 100, 101 )
then
dbms_sql.column_value( t_c, c, n_tab );
for i in 0 .. t_r - 1
loop
if n_tab( i + n_tab.first() ) is not null
then
cell( c, t_cur_row + i, n_tab( i + n_tab.first() ), p_sheet => t_sheet );
end if;
end loop;
n_tab.delete;
when t_desc_tab( c ).col_type in ( 12, 178, 179, 180, 181 , 231 )
then
dbms_sql.column_value( t_c, c, d_tab );
for i in 0 .. t_r - 1
loop
if d_tab( i + d_tab.first() ) is not null
then
cell( c, t_cur_row + i, d_tab( i + d_tab.first() ), p_sheet => t_sheet );
end if;
end loop;
d_tab.delete;
when t_desc_tab( c ).col_type in ( 1, 8, 9, 96, 112 )
then
dbms_sql.column_value( t_c, c, v_tab );
for i in 0 .. t_r - 1
loop
if v_tab( i + v_tab.first() ) is not null
then
cell( c, t_cur_row + i, v_tab( i + v_tab.first() ), p_sheet => t_sheet );
end if;
end loop;
v_tab.delete;
else
null;
end case;
end loop;
end if;
exit when t_r != t_bulk_size;
t_cur_row := t_cur_row + t_r;
end loop;
dbms_sql.close_cursor( t_c );
if ( p_directory is not null and p_filename is not null )
then
save( p_directory, p_filename );
end if;
exception
when others
then
if dbms_sql.is_open( t_c )
then
dbms_sql.close_cursor( t_c );
end if;
end;
end;
/

Пример формирования xlsx файла из запроса:

BEGIN
  as_xlsx.query2sheet('select * from my_table');
  as_xlsx.save('MY_DIR', 'my_xlsx.xlsx');
END;

Проверно, работает все замечательно.По всей видимости, этот пакет можно использовать в Oracle Forms, в этом случае можно будет сохранять Excel файлы на клиентской машине, но это предположение, требующее проверки.

Ссылка на основную публикацию