Beruflich Dokumente
Kultur Dokumente
mbleron / ExcelTable
Dismiss
Join GitHub today
GitHub is home to over 31 million developers working together to host and
review code, manage projects, and build software together.
Sign up
ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file (.ods) as external tables
java Bug fix : BINARY_DOUBLE constructor does not support String argument … 7 months ago
README.md
https://github.com/mbleron/ExcelTable 1/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
Version 2.0 : ExcelTable can read old 97-2003 Excel files (.xls).
Version 2.2 : ExcelTable can read ODF (OpenDocument) spreadsheet files (.ods).
Support for TIMESTAMP data type
Bug tracker
Found bugs? I'm sure there are...
Please create an issue here.
Installation
Database requirement
ExcelTable requires Oracle Database 11.2.0.1 and onwards.
Note that the interface may work as well on versions 11.1.0.6 and 11.1.0.7, with limited support for CLOB projections, but
that scenario has not been tested.
In order to read encrypted files, the interface requires access to the DBMS_CRYPTO API (see PL/SQL section below).
The owner must therefore be granted EXECUTE privilege on it :
PL/SQL
Create the following objects, in this order :
@xutl_cdf.pks
@xutl_cdf.pkb
@xutl_offcrypto.pks
@xutl_offcrypto.pkb
@ExcelTableCell.tps
@ExcelTableCellList.tps
@xutl_xls.pks
@xutl_xls.pkb
@xutl_xlsb.pks
@xutl_xlsb.pkb
@ExcelTableImpl.tps
@ExcelTable.pks
https://github.com/mbleron/ExcelTable 2/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
@ExcelTable.pkb
@ExcelTableImpl.tpb
Java
ExcelTable requires additional Java classes for the following features :
Usage
function getRows (
p_file in blob
, p_sheet in varchar2
, p_cols in varchar2
, p_range in varchar2 default null
, p_method in binary_integer default DOM_READ
, p_password in varchar2 default null
)
return anydataset pipelined
using ExcelTableImpl;
p_file : Input Office file (.xlsx, .xlsm, .xlsb, .xls or .ods format). A helper function ExcelTable.getFile is available to directly
reference the file from a directory.
p_sheet : Worksheet name
p_range : Excel-like range expression that defines the table boundaries in the worksheet (see specs below)
p_method : Read method - DOM_READ (0) the default, or STREAM_READ (1). The parameter value is ignored if the file is not a
.xlsx or .xlsm file.
p_password : Optional - password used to encrypt the spreadsheet document
https://github.com/mbleron/ExcelTable 3/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
Use setFetchSize() to control the number of rows returned by each invocation of the ODCITableFetch method.
If the number of rows requested by the client is greater than the fetch size, the fetch size is used instead.
The default fetch size is 100.
function getCursor (
p_file in blob
, p_sheet in varchar2
, p_cols in varchar2
, p_range in varchar2 default null
, p_method in binary_integer default DOM_READ
, p_password in varchar2 default null
)
return sys_refcursor;
getCursor() returns a REF cursor allowing the consumer to iterate through the resultset returned by an equivalent getRows() call.
It may be useful in PL/SQL code where static reference to table function returning ANYDATASET is not supported.
function createDMLContext (
p_table_name in varchar2
)
return DMLContext;
createDMLContext() initializes a new DML context based on the input table/view name.
Argument p_table_name may be a simple or qualified SQL name, with no database link part, for example :
MY_TABLE
MY_SCHEMA.MY_TABLE
"myTable"
MY_SCHEMA."myTable"
The function returns a handle to the context (of type ExcelTable.DMLContext), to be used by related routines mapColumn() and
loadData() .
Example :
declare
ctx ExcelTable.DMLContext;
begin
ctx := ExcelTable.createDMLContext('MY_TARGET_TABLE');
...
procedure mapColumn (
p_ctx in DMLContext
, p_col_name in varchar2
, p_col_ref in varchar2
, p_format in varchar2 default null
, p_meta in pls_integer default null
, p_key in boolean default false
);
mapColumn() associates a column from the target table to a column reference from the spreadsheet file.
https://github.com/mbleron/ExcelTable 4/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
p_col_name : column name from the target table
p_format : Optional - a date or timestamp format mask, same as p_format argument from getRows() function
p_meta : Optional - allowed values are META_ORDINALITY and META_COMMENT, same as FOR ORDINALITY and FOR
METADATA (COMMENT) clauses in the column list
p_key : Optional - defaults to false - marks this column as a key of the input data set. At least one column must be marked
as key in an UPDATE, MERGE or DELETE context.
Example :
declare
ctx ExcelTable.DMLContext;
begin
ctx := ExcelTable.createDMLContext('MY_TARGET_TABLE');
ExcelTable.mapColumn(ctx, p_col_name => 'ID', p_col_ref => 'A', p_key => true);
ExcelTable.mapColumn(ctx, p_col_name => 'NAME', p_col_ref => 'B');
ExcelTable.mapColumn(ctx, p_col_name => 'VAL', p_col_ref => 'C');
ExcelTable.mapColumn(ctx, p_col_name => 'VAL_COMMENT', p_col_ref => 'C', p_meta =>
ExcelTable.META_COMMENT);
...
function loadData (
p_ctx in DMLContext
, p_file in blob
, p_sheet in varchar2
, p_range in varchar2 default null
, p_method in binary_integer default DOM_READ
, p_password in varchar2 default null
, p_dml_type in pls_integer default DML_INSERT
, p_err_log in varchar2 default null
)
return integer;
loadData() executes the data loading operation into the target table, using the mode specified in the p_dml_type argument.
An optional error logging clause is available.
p_dml_type : Optional - the DML context type, one of DML_INSERT, DML_UPDATE, DML_MERGE or DML_DELETE. Default is
DML_INSERT
p_err_log : a text-literal DML error logging clause, to capture exceptions during load
Example :
declare
ctx ExcelTable.DMLContext;
nrows integer;
begin
...
nrows :=
ExcelTable.loadData(
p_ctx => ctx
, p_file => ExcelTable.getFile('XL_DATA_DIR','sample0.xlsx')
https://github.com/mbleron/ExcelTable 5/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
, p_sheet => 'DataSource'
, p_method => ExcelTable.STREAM_READ
, p_dml_type => ExcelTable.DML_MERGE
);
end;
Metadata_clause::=
VARCHAR2 - including CHAR/BYTE semantics. Values larger than the maximum length declared are silently truncated and no
error is reported.
DATE - with optional format mask. The format mask is used if the value is stored as text in the spreadsheet, otherwise the
date value is assumed to be stored as date in Excel's internal serial format.
TIMESTAMP - with optional scale and format mask specifications. The format mask is used if the value is stored as text in the
spreadsheet, otherwise the timestamp value is assumed to be stored in Excel's internal serial format.
CLOB
A special "FOR ORDINALITY" clause (like XMLTABLE or JSON_TABLE's one) is also available to autogenerate a sequence number.
Each column definition (except for the one qualified with FOR ORDINALITY) may be complemented with an optional "COLUMN"
clause to explicitly target a named column in the spreadsheet, instead of relying on the order of the declarations (relative to the
range). Positional and named column definitions cannot be mixed.
ExcelTable can also extract cell comments and project them as regular columns.
In order to do so, add this specific FOR METADATA clause :
Examples :
https://github.com/mbleron/ExcelTable 6/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
, "COL5" number(10,2)
, "COL6" varchar2(5)
, "COL7" timestamp(3) format 'YYYY-MM-DD HH24:MI:SS.FF'
Range of rows : '1:100' - in this case the range of columns implicitly starts at A.
Range of columns : 'B:E' - in this case the range of rows implicitly starts at 1.
Range of cells (top-left to bottom-right) : 'B2:F150'
Single cell anchor (top-left cell) : 'C3'
As for ODF :
Oracle, through DBMS_CRYPTO API, only supports SHA-2 algorithms (SHA256, 384, 512) starting from 12c.
Therefore, in prior versions, the OfficeCrypto implementation cannot read Office 2013 or ODF 1.2 (and onwards) documents
encrypted with the default options.
Examples
https://github.com/mbleron/ExcelTable 7/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
Given this sample file : sample_3.xlsx
Selecting all six columns, starting at cell A2, in order to skip the header :
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','sample_3.xlsx')
, 'DataSource'
, ' "SRNO" number
, "NAME" varchar2(10)
, "VAL" number
, "DT" date
, "SPARE1" varchar2(6)
, "SPARE2" varchar2(6)'
, 'A2'
)
) t
;
Selecting columns B and F only, from rows 2 to 10, with a generated sequence :
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','sample_3.xlsx')
, 'DataSource'
, q'{
"R_NUM" for ordinality
, "NAME" varchar2(10) column 'B'
, "SPARE2" varchar2(6) column 'F'
}'
, '2:10'
)
) t
;
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','crypto2016.xlsx')
, 'Feuil1'
, '"COL1" number'
, 'C5'
, 0
, p_password => 'AZE'
)
) t
;
COL1
----------
1
2
3
Selecting first three columns, row 1 to 91, from a password-encrypted .xls workbook (crypto2003.xls) :
select t.srno
, t.name
, t.content
https://github.com/mbleron/ExcelTable 8/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
, length(t.content) as content_length
from table(
ExcelTable.getRows(
p_file => ExcelTable.getFile('XL_DATA_DIR','crypto2003.xls')
, p_sheet => 'DataSource'
, p_cols => ' "SRNO" number
, "NAME" varchar2(10)
, "CONTENT" clob'
, p_range => '1:91'
, p_method => null
, p_password => 'pass123'
)
) t
;
SQL> print rc
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','sample_3.xlsx')
, 'DataSource'
, q'{
"RN" for ordinality
, "SPARE2" varchar2(30) column 'F'
, "SPARE2_COMMENT" varchar2(2000) column 'F' for metadata (comment)
}'
, '2:11'
)
) t
;
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','LO_AES256.ods')
, 'Feuille1'
, q'{
"COL_1" number
, "COL_2" number
, "COL_3" timestamp(3)
}'
, p_range => 'A1'
, p_method => null
, p_password => 'pass123'
)
) t
;
select t.*
from table(
ExcelTable.getRows(
ExcelTable.getFile('XL_DATA_DIR','sample_1.xlsb')
, 'data'
https://github.com/mbleron/ExcelTable 10/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
, '"VAL" varchar2(15)'
, 'F3:F11'
)
) t
;
VAL
---------------
FALSE
TRUE
#N/A
#NULL!
#DIV/0!
#VALUE!
#REF!
#NAME?
#NUM!
declare
ctx ExcelTable.DMLContext;
nrows integer;
begin
ctx := ExcelTable.createDMLContext('TMP_SAMPLE2');
nrows :=
ExcelTable.loadData(
p_ctx => ctx
, p_file => ExcelTable.getFile('XL_DATA_DIR','sample_2.xlsx')
, p_sheet => 'DataSource'
, p_method => ExcelTable.STREAM_READ
, p_dml_type => ExcelTable.DML_INSERT
);
end;
/
Using the DML API - example 2 : MERGE with DML error logging
https://github.com/mbleron/ExcelTable 11/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
declare
ctx ExcelTable.DMLContext;
nrows integer;
begin
ctx := ExcelTable.createDMLContext('TMP_SAMPLE1');
ExcelTable.mapColumn(ctx, p_col_name => 'ID', p_col_ref => 'A', p_key => true);
ExcelTable.mapColumn(ctx, p_col_name => 'NAME', p_col_ref => 'B');
ExcelTable.mapColumn(ctx, p_col_name => 'VAL', p_col_ref => 'C');
ExcelTable.mapColumn(ctx, p_col_name => 'TS', p_col_ref => 'D');
ExcelTable.mapColumn(ctx, p_col_name => 'TXT', p_col_ref => 'E');
ExcelTable.mapColumn(ctx, p_col_name => 'SPARE1', p_col_ref => 'F');
ExcelTable.mapColumn(ctx, p_col_name => 'SPARE1_CMT', p_col_ref => 'F', p_meta =>
ExcelTable.META_COMMENT);
nrows :=
ExcelTable.loadData(
p_ctx => ctx
, p_file => ExcelTable.getFile('XL_DATA_DIR','sample_1.xlsb')
, p_sheet => 'data'
, p_dml_type => ExcelTable.DML_MERGE
, p_err_log => 'LOG ERRORS (''Some comment'') REJECT LIMIT UNLIMITED'
);
end;
/
CHANGELOG
2.3.2 (2018-10-22)
XUTL_XLS enhancement (new buffered lob reader)
2.3.1 (2018-09-15)
XUTL_XLS enhancement
2.3 (2018-08-23)
New API for DML operations
Internal modularization, unified interface for cell sources
2.2 (2018-07-07)
Added support for OpenDocument (ODF) spreadsheets (.ods), including encrypted files
Added support for TIMESTAMP data type
2.1 (2018-04-22)
Added support for Excel Binary File Format (.xlsb)
https://github.com/mbleron/ExcelTable 12/13
3/20/2019 GitHub - mbleron/ExcelTable: ExcelTable is an Oracle SQL interface to read Microsoft Excel files (.xlsx, .xlsm, .xlsb, .xls) and ODF spreadsheet file…
2.0 (2018-04-01)
Added support for Excel 97-2003 files (.xls)
1.6.1 (2018-03-17)
Added large strings support for versions prior 11.2.0.2
1.6 (2017-12-31)
Added cell comments extraction
Internal modularization
1.5 (2017-07-10)
Fixed bug related to zip archives created with data descriptors. Now reading CRC-32, compressed and uncompressed sizes
directly from Central Directory entries.
Removed dependency to V$PARAMETER view (thanks Paul for the suggestion)
1.4 (2017-06-11)
Added getCursor() function
Fixed NullPointerException when using streaming method and file has no sharedStrings
1.3 (2017-05-30)
Added support for password-encrypted files
Fixed minor bugs
1.2 (2016-10-30)
Added new streaming read method
Added setFetchSize() procedure
1.1 (2016-06-25)
Added internal collection and LOB freeing
1.0 (2016-05-01)
Creation
https://github.com/mbleron/ExcelTable 13/13