--解析excel,转换成table,可供查询,支持xls、xlsx
--首先修改这个Type,长度改为4000.
CREATE OR REPLACE TYPE XYG_PUB_DATA_UPLOAD_Obj AS OBJECT(
SOURCE_TYPE VARCHAR2(240)--EXCEL/TXT
,BATCH_CODE VARCHAR2 (480 BYTE)--批的Code,Excel用,因为一个Excel可能有多个
,BATCH_NAME VARCHAR2 (4000 BYTE)--批的名称
,ROW_NUM NUMBER
,ATTRIBUTE1 VARCHAR2 (4000 BYTE)
,ATTRIBUTE2 VARCHAR2 (4000 BYTE)
,ATTRIBUTE3 VARCHAR2 (4000 BYTE)
,ATTRIBUTE4 VARCHAR2 (4000 BYTE)
,ATTRIBUTE5 VARCHAR2 (4000 BYTE)
,ATTRIBUTE6 VARCHAR2 (4000 BYTE)
,ATTRIBUTE7 VARCHAR2 (4000 BYTE)
,ATTRIBUTE8 VARCHAR2 (4000 BYTE)
,ATTRIBUTE9 VARCHAR2 (4000 BYTE)
,ATTRIBUTE10 VARCHAR2 (4000 BYTE)
,ATTRIBUTE11 VARCHAR2 (4000 BYTE)
,ATTRIBUTE12 VARCHAR2 (4000 BYTE)
,ATTRIBUTE13 VARCHAR2 (4000 BYTE)
,ATTRIBUTE14 VARCHAR2 (4000 BYTE)
,ATTRIBUTE15 VARCHAR2 (4000 BYTE)
,ATTRIBUTE16 VARCHAR2 (4000 BYTE)
,ATTRIBUTE17 VARCHAR2 (4000 BYTE)
,ATTRIBUTE18 VARCHAR2 (4000 BYTE)
,ATTRIBUTE19 VARCHAR2 (4000 BYTE)
,ATTRIBUTE20 VARCHAR2 (4000 BYTE)
,ATTRIBUTE21 VARCHAR2 (4000 BYTE)
,ATTRIBUTE22 VARCHAR2 (4000 BYTE)
,ATTRIBUTE23 VARCHAR2 (4000 BYTE)
,ATTRIBUTE24 VARCHAR2 (4000 BYTE)
,ATTRIBUTE25 VARCHAR2 (4000 BYTE)
,ATTRIBUTE26 VARCHAR2 (4000 BYTE)
,ATTRIBUTE27 VARCHAR2 (4000 BYTE)
,ATTRIBUTE28 VARCHAR2 (4000 BYTE)
,ATTRIBUTE29 VARCHAR2 (4000 BYTE)
,ATTRIBUTE30 VARCHAR2 (4000 BYTE)
,PROCESS_FLAG NUMBER ---0:INIT,3:SUCESSFUL
,PROCESS_MESSAGE VARCHAR2(4000));

包体内容,包头:

create or replace package xyg_pub_data_upload_pkg as

  /*TYPE XYG_PUB_DATA_UPLOAD_Obj is record(
SOURCE_TYPE VARCHAR2(240)--EXCEL/TXT
,BATCH_CODE VARCHAR2 (480 BYTE)--批的Code,Excel用,因为一个Excel可能有多个
,BATCH_NAME VARCHAR2 (4000 BYTE)--批的名称
,ROW_NUM NUMBER
,ATTRIBUTE1 VARCHAR2 (4000 BYTE)
,ATTRIBUTE2 VARCHAR2 (4000 BYTE)
,ATTRIBUTE3 VARCHAR2 (4000 BYTE)
,ATTRIBUTE4 VARCHAR2 (4000 BYTE)
,ATTRIBUTE5 VARCHAR2 (4000 BYTE)
,ATTRIBUTE6 VARCHAR2 (4000 BYTE)
,ATTRIBUTE7 VARCHAR2 (4000 BYTE)
,ATTRIBUTE8 VARCHAR2 (4000 BYTE)
,ATTRIBUTE9 VARCHAR2 (4000 BYTE)
,ATTRIBUTE10 VARCHAR2 (4000 BYTE)
,ATTRIBUTE11 VARCHAR2 (4000 BYTE)
,ATTRIBUTE12 VARCHAR2 (4000 BYTE)
,ATTRIBUTE13 VARCHAR2 (4000 BYTE)
,ATTRIBUTE14 VARCHAR2 (4000 BYTE)
,ATTRIBUTE15 VARCHAR2 (4000 BYTE)
,ATTRIBUTE16 VARCHAR2 (4000 BYTE)
,ATTRIBUTE17 VARCHAR2 (4000 BYTE)
,ATTRIBUTE18 VARCHAR2 (4000 BYTE)
,ATTRIBUTE19 VARCHAR2 (4000 BYTE)
,ATTRIBUTE20 VARCHAR2 (4000 BYTE)
,ATTRIBUTE21 VARCHAR2 (4000 BYTE)
,ATTRIBUTE22 VARCHAR2 (4000 BYTE)
,ATTRIBUTE23 VARCHAR2 (4000 BYTE)
,ATTRIBUTE24 VARCHAR2 (4000 BYTE)
,ATTRIBUTE25 VARCHAR2 (4000 BYTE)
,ATTRIBUTE26 VARCHAR2 (4000 BYTE)
,ATTRIBUTE27 VARCHAR2 (4000 BYTE)
,ATTRIBUTE28 VARCHAR2 (4000 BYTE)
,ATTRIBUTE29 VARCHAR2 (4000 BYTE)
,ATTRIBUTE30 VARCHAR2 (4000 BYTE)
,PROCESS_FLAG NUMBER ---0:INIT,3:SUCESSFUL
,PROCESS_MESSAGE VARCHAR2(4000));
*/
type xyg_pub_data_upload_obj_tab is table of xyg_pub_data_upload_obj; c_item_return_num constant number := 0; c_item_return_char constant varchar2(1) := null; c_item_err_code constant number := -20120; c_return_num constant number := -1; c_return_char constant varchar2(1) := null; c_true constant number := 1; c_false constant number := 0; function convert_file_blob(p_filedir in varchar2 ---文件路径 /usr/usr/glmr/customer
,
p_filename in varchar2 ---文件名称 DHS.csv
,
p_raise in number default /*xyg_pub_const_pkg.*/ c_true)
return blob; ----------------------
------程序主体部分----
----------------------
function conver_excel_to_tab(p_document blob,
p_sheets in varchar2 default null,
p_raise in number default /*xyg_pub_const_pkg.*/ c_true)
return xyg_pub_data_upload_obj_tab
pipelined; end xyg_pub_data_upload_pkg;

包体:

create or replace package body xyg_pub_data_upload_pkg as

  function convert_file_blob(p_filedir  in varchar2 ---文件路径 /usr/usr/glmr/customer
,
p_filename in varchar2 ---文件名称 DHS.csv
,
p_raise in number default /*xyg_pub_const_pkg.*/ c_true)
return blob is
l_result blob := empty_blob();
l_filedir varchar2(240);
l_files bfile;
l_dest_offset binary_integer;
l_src_offset binary_integer;
l_process_phase number;
begin
l_process_phase := 0;
select directory_name
into l_filedir
from all_directories
where 1 = 1
and (upper(directory_path) = case
when substr(p_filedir, -1) = '/' then
upper(substr(p_filedir, 1, length(p_filedir) - 1))
else
upper(p_filedir)
end or upper(directory_path) = upper(p_filedir) or
directory_name = p_filedir)
and rownum <= 1;
--DBMS_OUTPUT.PUT_LINE('L_FILEDIR:'||L_FILEDIR);
l_process_phase := 1;
l_files := bfilename(l_filedir, p_filename);
dbms_lob.createtemporary(lob_loc => l_result, cache => true,
dur => dbms_lob.call);
l_dest_offset := 1;
l_src_offset := 1;
dbms_lob.open(l_files, dbms_lob.lob_readonly);
dbms_lob.loadblobfromfile(l_result --dest_lob IN OUT NOCOPY BLOB,
, l_files
--src_lob IN BFILE,
, dbms_lob.lobmaxsize
--amount IN INTEGER,
, l_dest_offset
--dest_offset IN INTEGER := 1,
, l_src_offset
--src_offset IN INTEGER := 1
);
l_process_phase := 2;
dbms_lob.close(l_files);
l_process_phase := 99;
return l_result;
exception
when others then
if p_raise = /*xyg_pub_const_pkg.*/
c_true then
dbms_output.put_line('转换文件有异常错误!进度:' || l_process_phase);
raise;
/*xyg_pub_common_pkg.raise_error('-20001' --'ERR_DEFAULT_CODE'
, sqlerrm,
'转换文件有异常错误!进度:' || l_process_phase);*/
--DBMS_OUTPUT.PUT_LINE ('THERE ARE SOME ERROR, PLEASE CONTACT WITH MIS');
else
--UTL_FILE.FCLOSE (L_FILEHANDLE);
return empty_blob();
end if;
end; ----------------------
------程序主体部分----
----------------------
function conver_excel_to_tab(p_document blob,
p_sheets in varchar2 default null,
p_raise in number default /*xyg_pub_const_pkg.*/ c_true)
return xyg_pub_data_upload_obj_tab
pipelined is
type tp_cell is record(
data_type varchar2(1),
string_val varchar2(32767),
number_val number,
date_val date,
blob_val blob);
type tp_row is table of tp_cell index by pls_integer;
type tp_rows is table of tp_row index by pls_integer;
type tp_sheet is record(
name varchar2(2000),
rows tp_rows);
type tp_data is table of tp_sheet index by pls_integer;
t_data tp_data;
t_collection_base varchar2(32767);
t_collection_name varchar2(32767);
--type tp_2col is table of vc_arr2 index by pls_integer;
--t2 tp_2col;
l_round char(1) := 'Y';
l_process_phase number;
---xls:
type tp_sheet_rec is record(
name varchar2(32767),
ind integer);
type tp_sheets is table of tp_sheet_rec index by pls_integer;
t_sheets tp_sheets;
t_sheet tp_sheet_rec;
--xlsx:
type tp_strings is table of varchar2(32767) index by pls_integer;
t_sheet_ids tp_strings;
t_sheet_names tp_strings;
-----------------
----XLS解析器----
-----------------
function g1(i pls_integer, r pls_integer, c pls_integer) return varchar2 is
l_return varchar2(4000);
begin
if l_round = 'Y' then
l_return := case
when t_data(i).rows(r).exists(c) then
coalesce(substr(t_data(i).rows(r)(c).string_val, 1, 4000),
to_char(round(t_data(i).rows(r)(c).number_val,
14 -
substr(to_char(t_data(i).rows(r)(c)
.number_val, 'TME'), -3)),
'TM9'),
to_char(t_data(i).rows(r)(c).date_val,
'yyyy-mm-dd hh24:mi:ss'))
end;
else
l_return := case
when t_data(i).rows(r).exists(c) then
coalesce(substr(t_data(i).rows(r)(c).string_val, 1, 4000),
to_char(t_data(i).rows(r)(c).number_val, 'TM9'),
to_char(t_data(i).rows(r)(c).date_val,
'yyyy-mm-dd hh24:mi:ss'))
end;
end if;
return l_return;
--return case when t_data(i)(r).exists(c) then substr( t_data(i)(r)(c), 1, 4000 ) end;
end; function parse_xls(p_document blob,
p_sheets varchar2 := null,
p_extra dbmsoutput_linesarray := null)
return tp_data is
t_cell tp_cell;
t_rows tp_rows;
t_data tp_data;
t_workbook blob;
t_ind integer;
t_sind integer;
t_len integer;
t_max_len integer;
t_cnt integer;
t_grbit raw(1);
t_biff5 boolean;
t_str varchar2(32767);
t_tmp raw(32767);
t_rec raw(32767);
t_date1904 boolean;
type tp_sst is table of varchar2(32767) index by pls_integer;
t_sst tp_sst;
type tp_date is table of boolean index by pls_integer;
t_xf_date tp_date;
t_fmt_date tp_date;
type tp_xf_fmt is table of pls_integer index by pls_integer;
t_xf_fmt tp_xf_fmt;
t_fmt varchar2(32767);
t_char_set varchar2(100) := 'WE8MSWIN1252';
t_c pls_integer;
t_type varchar2(1);
t_max_c pls_integer;
procedure read_unicode_string is
t_uni raw(32767);
begin
t_str := null;
while t_cnt > 0 loop
if utl_raw.bit_and(t_grbit, hextoraw('')) = hextoraw('') then
if (t_sind + t_cnt * 2 > utl_raw.length(t_rec) + 1 and
dbms_lob.substr(t_workbook, 2, t_ind + t_len + 4) =
hextoraw('3C00')) then
t_str := t_str ||
utl_i18n.raw_to_char(utl_raw.substr(t_rec, t_sind),
'AL16UTF16LE');
t_cnt := t_cnt -
utl_raw.length(utl_raw.substr(t_rec, t_sind)) / 2;
t_ind := t_ind + t_len + 4;
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
t_grbit := dbms_lob.substr(t_workbook, 1, t_ind + 4);
t_rec := dbms_lob.substr(t_workbook, t_len - 1,
t_ind + 4 + 1);
t_sind := 1;
else
t_str := t_str ||
utl_i18n.raw_to_char(utl_raw.substr(t_rec, t_sind,
t_cnt * 2),
'AL16UTF16LE');
t_sind := t_sind + t_cnt * 2;
t_cnt := 0;
end if;
else
if (t_sind + t_cnt > utl_raw.length(t_rec) + 1 and
dbms_lob.substr(t_workbook, 2, t_ind + t_len + 4) =
hextoraw('3C00')) then
t_tmp := utl_raw.substr(t_rec, t_sind);
t_cnt := t_cnt -
utl_raw.length(utl_raw.substr(t_rec, t_sind));
t_ind := t_ind + t_len + 4;
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
t_grbit := dbms_lob.substr(t_workbook, 1, t_ind + 4);
t_rec := dbms_lob.substr(t_workbook, t_len - 1,
t_ind + 4 + 1);
t_sind := 1;
else
t_tmp := utl_raw.substr(t_rec, t_sind, t_cnt);
t_sind := t_sind + t_cnt;
t_cnt := 0;
end if;
t_uni := null;
for i in 1 .. utl_raw.length(t_tmp) loop
t_uni := utl_raw.concat(t_uni, utl_raw.substr(t_tmp, i, 1),
hextoraw(''));
end loop;
t_str := t_str || utl_i18n.raw_to_char(t_uni, 'AL16UTF16LE');
end if;
end loop;
end; function rk2number(p_rk raw) return number is
begin
return case rawtohex(utl_raw.bit_and(utl_raw.substr(p_rk, 1, 1),
'')) when '' then utl_raw.cast_to_binary_integer(utl_raw.bit_and(p_rk,
'FCFFFFFF'),
utl_raw.little_endian) / 4 when '' then utl_raw.cast_to_binary_integer(utl_raw.bit_and(p_rk,
'FCFFFFFF'),
utl_raw.little_endian) / 400 when '' then utl_raw.cast_to_binary_double(utl_raw.concat('',
p_rk),
utl_raw.little_endian) when '' then utl_raw.cast_to_binary_double(utl_raw.concat('',
utl_raw.bit_and(p_rk,
'FCFFFFFF')),
utl_raw.little_endian) / 100 end;
end; function num2date(p_num number) return date is
begin
if t_date1904 then
return to_date('01-01-1904', 'DD-MM-YYYY') + p_num;
end if;
return to_date('01-03-1900', 'DD-MM-YYYY') +(p_num - 61);
end; procedure read_cfb(p_cf blob) is
t_header raw(512);
t_byte_order pls_integer;
t_encoding varchar2(30);
t_ssz pls_integer;
t_sssz pls_integer;
t_sectid pls_integer;
t_tmp_sectid t_sectid%type;
type tp_secids is table of t_sectid%type index by pls_integer;
t_msat tp_secids;
t_sat tp_secids;
t_ssat tp_secids;
t_sector raw(2048);
t_short_container blob;
t_stream blob;
t_len pls_integer;
t_name varchar2(32 char);
c_free_secid constant pls_integer := -1;
c_end_of_chain_secid constant pls_integer := -2;
c_sat_secid constant pls_integer := -3;
c_msat_secid constant pls_integer := -4;
c_dir_empty constant raw(1) := hextoraw('');
c_dir_storage constant raw(1) := hextoraw('');
c_dir_stream constant raw(1) := hextoraw('');
c_dir_lock constant raw(1) := hextoraw('');
c_dir_property constant raw(1) := hextoraw('');
c_dir_root constant raw(1) := hextoraw('');
begin
t_header := dbms_lob.substr(p_cf, 512, 1);
if (t_header is null or utl_raw.length(t_header) < 512 or
utl_raw.substr(t_header, 1, 8) != hextoraw('D0CF11E0A1B11AE1')) then
return;
end if;
t_byte_order := case
when utl_raw.substr(t_header, 29, 2) = hextoraw('FEFF') then
utl_raw.little_endian
else
utl_raw.big_endian
end;
if t_byte_order = utl_raw.little_endian then
t_encoding := 'AL16UTF16LE';
else
t_encoding := 'AL16UTF16';
end if;
t_ssz := power(2,
utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
31, 2),
t_byte_order));
t_sssz := power(2,
utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
33, 2),
t_byte_order));
for i in 0 .. 109 - 1 loop
t_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
77 +
i * 4, 4),
t_byte_order);
exit when t_sectid = c_free_secid;
t_msat(i) := t_sectid;
end loop;
t_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
69, 4),
t_byte_order);
while t_sectid != c_end_of_chain_secid loop
t_sector := dbms_lob.substr(p_cf, t_ssz,
512 + t_ssz * t_sectid + 1);
for i in 0 .. t_ssz / 4 - 2 loop
t_msat(t_msat.count()) := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 4 + 1,
4),
t_byte_order);
end loop;
t_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
-4, 4),
t_byte_order);
end loop;
for j in 0 .. t_msat.count() - 1 loop
t_sector := dbms_lob.substr(p_cf, t_ssz,
512 + t_ssz * t_msat(j) + 1);
for i in 0 .. t_ssz / 4 - 1 loop
t_sat(t_sat.count()) := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 4 + 1,
4),
t_byte_order);
end loop;
end loop;
t_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
61, 4),
t_byte_order);
while t_sectid != c_end_of_chain_secid loop
t_sector := dbms_lob.substr(p_cf, t_ssz,
512 + t_ssz * t_sectid + 1);
for i in 0 .. t_ssz / 4 - 1 loop
t_ssat(t_ssat.count()) := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 4 + 1,
4),
t_byte_order);
end loop;
t_sectid := t_sat(t_sectid);
end loop;
t_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
49, 4),
t_byte_order);
while t_sectid != c_end_of_chain_secid loop
t_sector := dbms_lob.substr(p_cf, t_ssz,
512 + t_ssz * t_sectid + 1);
for i in 0 .. t_ssz / 128 - 1 loop
t_len := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 128 + 65,
2),
t_byte_order);
if t_len > 2 then
t_name := utl_i18n.raw_to_char(utl_raw.substr(t_sector,
i * 128 + 1,
t_len - 2),
t_encoding);
end if;
case utl_raw.substr(t_sector, i * 128 + 67, 1)
when c_dir_stream then
dbms_lob.createtemporary(t_stream, true);
t_tmp_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 128 + 117,
4),
t_byte_order);
t_len := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 128 + 121,
4),
t_byte_order);
if t_len >=
utl_raw.cast_to_binary_integer(utl_raw.substr(t_header,
57, 4),
t_byte_order) then
while t_tmp_sectid != c_end_of_chain_secid loop
dbms_lob.append(t_stream,
dbms_lob.substr(p_cf, t_ssz,
512 +
t_ssz * t_tmp_sectid + 1));
t_tmp_sectid := t_sat(t_tmp_sectid);
end loop;
else
while t_tmp_sectid != c_end_of_chain_secid loop
dbms_lob.append(t_stream,
dbms_lob.substr(t_short_container,
t_sssz,
t_sssz * t_tmp_sectid + 1));
t_tmp_sectid := t_ssat(t_tmp_sectid);
end loop;
end if;
dbms_lob.trim(t_stream, t_len);
if t_name = 'Workbook' then
t_workbook := t_stream;
end if;
if t_name = 'Book' then
t_workbook := t_stream;
end if;
when c_dir_root then
dbms_lob.createtemporary(t_short_container, true);
t_tmp_sectid := utl_raw.cast_to_binary_integer(utl_raw.substr(t_sector,
i * 128 + 117,
4),
t_byte_order);
while t_tmp_sectid != c_end_of_chain_secid loop
dbms_lob.append(t_short_container,
dbms_lob.substr(p_cf, t_ssz,
512 + t_ssz * t_tmp_sectid + 1));
t_tmp_sectid := t_sat(t_tmp_sectid);
end loop;
else
null;
end case;
end loop;
t_sectid := t_sat(t_sectid);
end loop;
if dbms_lob.istemporary(t_short_container) = 1 then
dbms_lob.freetemporary(t_short_container);
end if;
if dbms_lob.istemporary(t_stream) = 1 then
dbms_lob.freetemporary(t_stream);
end if;
end; begin
--my_log('parsing XLS');
read_cfb(p_document);
if t_workbook is null or dbms_lob.getlength(t_workbook) = 0 then
--my_log('No workbook file found');
raise_application_error(-20003, 'Not a valid XLS-file', true);
end if;
t_ind := 1;
t_max_len := dbms_lob.getlength(t_workbook);
if (dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') and
dbms_lob.substr(t_workbook, 2, t_ind + 4) in
(hextoraw(''), hextoraw('')) and
dbms_lob.substr(t_workbook, 2, t_ind + 6) = hextoraw('')) then
t_biff5 := dbms_lob.substr(t_workbook, 2, t_ind + 4) =
hextoraw('');
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
t_ind := t_ind + t_len + 4;
loop
exit when t_ind >= t_max_len;
exit when dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('0A00');
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
if dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('FC00') then
declare
t_run integer;
t_ext integer;
procedure add_cont(p_len pls_integer) is
begin
if (t_sind + p_len > utl_raw.length(t_rec) + 1 and
dbms_lob.substr(t_workbook, 2, t_ind + t_len + 4) =
hextoraw('3C00')) then
t_ind := t_ind + t_len + 4;
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
if t_sind <= utl_raw.length(t_rec) then
t_rec := utl_raw.concat(utl_raw.substr(t_rec, t_sind),
dbms_lob.substr(t_workbook, t_len,
t_ind + 4));
else
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
end if;
t_sind := 1;
end if;
end; begin
t_sind := 1;
t_rec := dbms_lob.substr(t_workbook, t_len - 8, t_ind + 12);
for j in 1 .. utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
4,
t_ind + 8),
utl_raw.little_endian) loop
add_cont(3);
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
t_sind,
2),
utl_raw.little_endian);
t_sind := t_sind + 2;
t_grbit := utl_raw.substr(t_rec, t_sind, 1);
t_sind := t_sind + 1;
if utl_raw.bit_and(t_grbit, hextoraw('')) =
hextoraw('') then
add_cont(2);
t_run := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
t_sind,
2),
utl_raw.little_endian);
t_sind := t_sind + 2;
else
t_run := 0;
end if;
if utl_raw.bit_and(t_grbit, hextoraw('')) =
hextoraw('') then
add_cont(4);
t_ext := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
t_sind,
4),
utl_raw.little_endian);
if t_ext < 0 then
t_ext := t_ext + 4294967296;
end if;
t_sind := t_sind + 4;
else
t_ext := 0;
end if;
read_unicode_string;
t_sst(t_sst.count()) := t_str;
add_cont(t_run * 4 + t_ext);
t_sind := t_sind + t_run * 4 + t_ext;
end loop;
end;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_biff5 then
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
7,
1),
utl_raw.little_endian);
t_tmp := utl_raw.substr(t_rec, 8, t_cnt);
t_sheet.name := utl_i18n.raw_to_char(t_tmp, t_char_set);
else
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
7, 1));
t_grbit := utl_raw.substr(t_rec, 8, 1);
if utl_raw.bit_and(t_grbit, hextoraw('')) = hextoraw('') then
t_str := utl_raw.substr(t_rec, 9, t_cnt * 2);
else
t_str := null;
t_tmp := utl_raw.substr(t_rec, 9, t_cnt);
for i in 1 .. utl_raw.length(t_tmp) loop
t_str := utl_raw.concat(t_str, utl_raw.substr(t_tmp, i, 1),
hextoraw(''));
end loop;
end if;
t_sheet.name := utl_i18n.raw_to_char(t_str, 'AL16UTF16LE');
end if;
t_sheet.ind := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
1, 4),
utl_raw.little_endian);
t_sheets(t_sheets.count()) := t_sheet;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_date1904 := dbms_lob.substr(t_workbook, 2, t_ind + 4) =
hextoraw('');
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('1E04') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_biff5 then
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 1),
utl_raw.little_endian);
t_tmp := utl_raw.substr(t_rec, 4, t_cnt);
t_fmt := utl_i18n.raw_to_char(t_tmp, t_char_set);
else
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian);
t_grbit := utl_raw.substr(t_rec, 5, 1);
if utl_raw.bit_and(t_grbit, hextoraw('')) = hextoraw('') then
t_str := utl_raw.substr(t_rec, 6, t_cnt * 2);
else
t_str := null;
t_tmp := utl_raw.substr(t_rec, 6, t_cnt);
for i in 1 .. utl_raw.length(t_tmp) loop
t_str := utl_raw.concat(t_str, utl_raw.substr(t_tmp, i, 1),
hextoraw(''));
end loop;
end if;
t_fmt := utl_i18n.raw_to_char(t_str, 'AL16UTF16LE');
end if;
t_fmt_date(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian)) := (instr(t_fmt,
'dd') > 0 or
instr(t_fmt,
'mm') > 0 or
instr(t_fmt,
'yy') > 0);
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('E000') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
t_xf_fmt(t_xf_fmt.count()) := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3,
2),
utl_raw.little_endian);
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if (rawtohex(t_rec) in ('', '') and
nls_charset_id('WE8MACROMAN8') is not null) then
t_char_set := 'WE8MACROMAN8';
end if;
end if;
t_ind := t_ind + t_len + 4;
end loop;
t_fmt_date(14) := true;
t_fmt_date(15) := true;
t_fmt_date(16) := true;
t_fmt_date(17) := true;
t_fmt_date(22) := true;
for i in 0 .. t_xf_fmt.count() - 1 loop
t_xf_date(i) := t_fmt_date.exists(t_xf_fmt(i)) and
t_fmt_date(t_xf_fmt(i));
end loop;
end if;
for s in 0 .. t_sheets.count - 1 loop
t_ind := t_sheets(s).ind + 1;
if (dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') and
dbms_lob.substr(t_workbook, 2, t_ind + 4) = hextoraw('') and
dbms_lob.substr(t_workbook, 2, t_ind + 6) = hextoraw('') and
(p_sheets is null or
instr(':' || p_sheets || ':', ':' || to_char(s + 1) || ':') > 0 or
instr(':' || p_sheets || ':', ':' || t_sheets(s).name || ':') > 0)) then
t_max_c := 0;
t_rows.delete;
t_data(t_data.count + 1).name := t_sheets(s).name;
--my_log('read ' || t_sheets(s).name);
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
t_ind := t_ind + t_len + 4;
loop
exit when t_ind >= t_max_len;
exit when dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('0A00');
t_cell := null;
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
if dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('7E02') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_xf_date(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
5, 2),
utl_raw.little_endian)) then
t_cell.data_type := 'D';
t_cell.date_val := num2date(rk2number(utl_raw.substr(t_rec,
7, 4)));
else
t_cell.data_type := 'N';
t_cell.number_val := rk2number(utl_raw.substr(t_rec, 7, 4));
end if;
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 3,
2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_xf_date(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
5, 2),
utl_raw.little_endian)) then
t_cell.data_type := 'D';
t_cell.date_val := num2date(utl_raw.cast_to_binary_double(utl_raw.substr(t_rec,
7,
8),
utl_raw.little_endian));
else
t_cell.data_type := 'N';
t_cell.number_val := utl_raw.cast_to_binary_double(utl_raw.substr(t_rec,
7,
8),
utl_raw.little_endian);
end if;
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 3,
2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if (rawtohex(utl_raw.substr(t_rec, 7, 1)) not in
('', '', '', '') or
utl_raw.substr(t_rec, 13, 2) != hextoraw('FFFF')) then
if t_xf_date(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
5,
2),
utl_raw.little_endian)) then
t_cell.data_type := 'D';
t_cell.date_val := num2date(utl_raw.cast_to_binary_double(utl_raw.substr(t_rec,
7,
8),
utl_raw.little_endian));
else
t_cell.data_type := 'N';
t_cell.number_val := utl_raw.cast_to_binary_double(utl_raw.substr(t_rec,
7,
8),
utl_raw.little_endian);
end if;
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
else
case rawtohex(utl_raw.substr(t_rec, 7, 1))
when '' then
t_cell.data_type := 'S';
t_cell.string_val := case
rawtohex(utl_raw.substr(t_rec, 9, 1))
when '' then
'FALSE'
when '' then
'TRUE'
end;
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
when '' then
null;
when '' then
if dbms_lob.substr(t_workbook, 2, t_ind + t_len + 4) =
hextoraw('') then
declare
t_row pls_integer := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
1,
2),
utl_raw.little_endian) + 1;
t_col pls_integer := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3,
2),
utl_raw.little_endian) + 1;
begin
t_ind := t_ind + t_len + 4;
t_len := utl_raw.cast_to_binary_integer(dbms_lob.substr(t_workbook,
2,
t_ind + 2),
utl_raw.little_endian);
t_rec := dbms_lob.substr(t_workbook,
t_len, t_ind + 4);
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
1,
2),
utl_raw.little_endian);
t_sind := 4;
t_cell.data_type := 'S';
if t_biff5 then
t_tmp := utl_raw.substr(t_rec, 3,
t_cnt);
t_cell.string_val := utl_i18n.raw_to_char(t_tmp,
t_char_set);
else
t_grbit := dbms_lob.substr(t_rec, 1, 3);
read_unicode_string;
t_cell.string_val := t_str;
end if;
t_max_c := greatest(t_max_c, t_col);
t_rows(t_row)(t_col) := t_cell;
end;
end if;
else
t_cell.data_type := 'S';
t_cell.string_val := '';
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
end case;
end if;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_biff5 then
t_cell.data_type := 'S';
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
7,
2),
utl_raw.little_endian);
if t_cnt = 0 then
t_cell.string_val := null;
else
t_tmp := utl_raw.substr(t_rec, 9, t_cnt);
t_cell.string_val := utl_i18n.raw_to_char(t_tmp,
t_char_set);
end if;
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
end if;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('D600') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if t_biff5 then
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
7, 2),
utl_raw.little_endian);
t_tmp := utl_raw.substr(t_rec, 9, t_cnt);
t_cell.data_type := 'S';
t_cell.string_val := utl_i18n.raw_to_char(t_tmp, t_char_set);
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
end if;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('FD00') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
t_cell.data_type := 'S';
t_c := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 3,
2),
utl_raw.little_endian) + 1;
t_max_c := greatest(t_max_c, t_c);
t_cell.string_val := t_sst(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
7,
4),
utl_raw.little_endian));
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(t_c) := t_cell;
elsif dbms_lob.substr(t_workbook, 2, t_ind) = hextoraw('BD00') then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
t_cnt := utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian);
for i in utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
3, 2),
utl_raw.little_endian) .. utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec,
-2,
2),
utl_raw.little_endian) loop
t_tmp := utl_raw.substr(t_rec, 5 + 6 * (i - t_cnt), 6);
if t_xf_date(utl_raw.cast_to_binary_integer(utl_raw.substr(t_tmp,
1,
2),
utl_raw.little_endian)) then
t_cell.data_type := 'D';
t_cell.date_val := num2date(rk2number(utl_raw.substr(t_tmp,
3, 4)));
else
t_cell.data_type := 'N';
t_cell.number_val := rk2number(utl_raw.substr(t_tmp, 3, 4));
end if;
t_max_c := greatest(t_max_c, i + 1);
t_rows(utl_raw.cast_to_binary_integer(utl_raw.substr(t_rec, 1, 2), utl_raw.little_endian) + 1)(i + 1) := t_cell;
end loop;
end if;
t_ind := t_ind + t_len + 4;
end loop;
if t_rows.count > 0 then
t_c := t_rows(t_rows.last).first;
t_type := t_rows(t_rows.last)(t_c).data_type;
for r in 1 .. t_rows.last - 1 loop
if not t_rows.exists(r) then
t_rows(r)(t_c).data_type := t_type;
end if;
end loop;
if t_rows.count > 1 then
for c in 1 .. t_max_c loop
t_type := null;
for r in 2 .. t_rows.last loop
if t_rows(r).exists(c) then
t_type := t_rows(r)(c).data_type;
exit;
end if;
end loop;
if t_type is null then
if t_rows(1).exists(c) then
t_type := t_rows(1)(c).data_type;
else
t_type := 'S';
end if;
end if;
for r in 1 .. t_rows.last loop
if not t_rows(r).exists(c) then
t_rows(r)(c).data_type := t_type;
end if;
end loop;
end loop;
else
for c in 1 .. t_max_c loop
if not t_rows(1).exists(c) then
t_rows(1)(c).data_type := 'S';
end if;
end loop;
end if;
end if;
t_data(t_data.count).rows := t_rows;
end if;
end loop;
return t_data;
end; -----------------
----XLSX解析器----
-----------------
function g2(i pls_integer, r pls_integer, c pls_integer) return varchar2 is
l_return varchar2(4000);
begin
if l_round = 'Y' then
l_return := case
when t_data(i).rows(r).exists(c) then
coalesce(substr(t_data(i).rows(r)(c).string_val, 1, 4000),
to_char(round(t_data(i).rows(r)(c).number_val,
14 -
substr(to_char(t_data(i).rows(r)(c)
.number_val, 'TME'), -3)),
'TM9'),
to_char(t_data(i).rows(r)(c).date_val,
'yyyy-mm-dd hh24:mi:ss'))
end;
else
l_return := case
when t_data(i).rows(r).exists(c) then
coalesce(substr(t_data(i).rows(r)(c).string_val, 1, 4000),
to_char(t_data(i).rows(r)(c).number_val, 'TM9'),
to_char(t_data(i).rows(r)(c).date_val,
'yyyy-mm-dd hh24:mi:ss'))
end;
end if;
return l_return;
--return case when t_data(i)(r).exists(c) then substr( t_data(i)(r)(c), 1, 4000 ) end;
end; function blob2node(p_blob blob) return dbms_xmldom.domnode is
begin
if p_blob is null or dbms_lob.getlength(p_blob) = 0 then
return null;
end if;
return dbms_xmldom.makenode(dbms_xmldom.getdocumentelement(dbms_xmldom.newdomdocument(xmltype(p_blob,
nls_charset_id('AL32UTF8')))));
exception
when others then
declare
t_nd dbms_xmldom.domnode;
t_clob clob;
t_dest_offset integer;
t_src_offset integer;
t_lang_context number := dbms_lob.default_lang_ctx;
t_warning integer;
begin
dbms_lob.createtemporary(t_clob, true);
t_dest_offset := 1;
t_src_offset := 1;
dbms_lob.converttoclob(t_clob, p_blob, dbms_lob.lobmaxsize,
t_dest_offset, t_src_offset,
nls_charset_id('AL32UTF8'), t_lang_context,
t_warning);
t_nd := dbms_xmldom.makenode(dbms_xmldom.getdocumentelement(dbms_xmldom.newdomdocument(t_clob)));
dbms_lob.freetemporary(t_clob);
return t_nd;
end;
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; 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 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; function get_file(p_zipped_blob blob, p_file_name varchar2) return blob is
t_tmp blob;
t_ind integer;
t_hd_ind integer;
t_fl_ind integer;
t_encoding varchar2(10);
t_len integer;
begin
t_ind := dbms_lob.getlength(p_zipped_blob) - 21;
loop
exit when t_ind < 1 or dbms_lob.substr(p_zipped_blob, 4, t_ind) = hextoraw('504B0506');
t_ind := t_ind - 1;
end loop;
if t_ind <= 0 then
return null;
end if;
t_hd_ind := blob2num(p_zipped_blob, 4, t_ind + 16) + 1;
for i in 1 .. blob2num(p_zipped_blob, 2, t_ind + 8) loop
if utl_raw.bit_and(dbms_lob.substr(p_zipped_blob, 1, t_hd_ind + 9),
hextoraw('')) = hextoraw('') then
t_encoding := 'AL32UTF8';
else
t_encoding := 'US8PC437';
end if;
if p_file_name =
utl_i18n.raw_to_char(dbms_lob.substr(p_zipped_blob,
blob2num(p_zipped_blob, 2,
t_hd_ind + 28),
t_hd_ind + 46), t_encoding) then
t_len := blob2num(p_zipped_blob, 4, t_hd_ind + 24);
if t_len = 0 then
if substr(p_file_name, -1) in ('/', '\') then
return null;
else
return empty_blob();
end if;
end if;
if dbms_lob.substr(p_zipped_blob, 2, t_hd_ind + 10) =
hextoraw('') then
t_fl_ind := blob2num(p_zipped_blob, 4, t_hd_ind + 42);
t_tmp := hextoraw('1F8B0800000000000003');
dbms_lob.copy(t_tmp, p_zipped_blob,
blob2num(p_zipped_blob, 4, t_hd_ind + 20), 11,
t_fl_ind + 31 +
blob2num(p_zipped_blob, 2, t_fl_ind + 27) +
blob2num(p_zipped_blob, 2, t_fl_ind + 29));
dbms_lob.append(t_tmp,
utl_raw.concat(dbms_lob.substr(p_zipped_blob, 4,
t_hd_ind + 16),
little_endian(t_len)));
return utl_compress.lz_uncompress(t_tmp);
end if;
if dbms_lob.substr(p_zipped_blob, 2, t_hd_ind + 10) =
hextoraw('') then
t_fl_ind := blob2num(p_zipped_blob, 4, t_hd_ind + 42);
dbms_lob.createtemporary(t_tmp, true);
dbms_lob.copy(t_tmp, p_zipped_blob, t_len, 1,
t_fl_ind + 31 +
blob2num(p_zipped_blob, 2, t_fl_ind + 27) +
blob2num(p_zipped_blob, 2, t_fl_ind + 29));
return t_tmp;
end if;
end if;
t_hd_ind := t_hd_ind + 46 +
blob2num(p_zipped_blob, 2, t_hd_ind + 28) +
blob2num(p_zipped_blob, 2, t_hd_ind + 30) +
blob2num(p_zipped_blob, 2, t_hd_ind + 32);
end loop;
return null;
end; function parse_xlsx(p_doc blob,
p_sheets varchar2 := null,
p_extra dbmsoutput_linesarray := null)
return tp_data is
t_rows tp_rows;
t_data tp_data;
t_date1904 boolean;
type tp_date is table of boolean index by pls_integer;
t_xf_date tp_date;
t_numfmt_date tp_date;
t_strings tp_strings;
t_r varchar2(32767);
t_s varchar2(32767);
t_t varchar2(32767);
t_val varchar2(32767);
t_nr number;
t_x pls_integer;
t_xx pls_integer;
t_c pls_integer;
t_sc pls_integer;
t_rr pls_integer;
t_ns varchar2(200) := 'xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"';
t_nd dbms_xmldom.domnode;
t_nd2 dbms_xmldom.domnode;
t_nl dbms_xmldom.domnodelist;
t_nl2 dbms_xmldom.domnodelist;
t_nl3 dbms_xmldom.domnodelist;
t_type varchar2(1);
t_max_c pls_integer;
begin
--my_log('parsing XLSX');
t_nd := blob2node(get_file(p_doc, 'xl/workbook.xml'));
t_date1904 := lower(dbms_xslprocessor.valueof(t_nd,
'/workbook/workbookPr/@date1904',
t_ns)) in ('true', '');
t_nl := dbms_xslprocessor.selectnodes(t_nd,
'/workbook/sheets/sheet',
t_ns);
for i in 0 .. dbms_xmldom.getlength(t_nl) - 1 loop
t_sheet_ids(i + 1) := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl,
i),
'@r:id',
'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"');
t_sheet_names(i + 1) := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl,
i),
'@name');
end loop;
dbms_xmldom.freenode(t_nd);
t_nd := blob2node(get_file(p_doc, 'xl/styles.xml'));
t_nl := dbms_xslprocessor.selectnodes(t_nd,
'/styleSheet/numFmts/numFmt',
t_ns);
for i in 0 .. dbms_xmldom.getlength(t_nl) - 1 loop
t_val := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl, i),
'@formatCode');
if (instr(t_val, 'dd') > 0 or instr(t_val, 'mm') > 0 or
instr(t_val, 'yy') > 0) then
t_numfmt_date(dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl, i), '@numFmtId')) := true;
end if;
end loop;
t_numfmt_date(14) := true;
t_numfmt_date(15) := true;
t_numfmt_date(16) := true;
t_numfmt_date(17) := true;
t_numfmt_date(22) := true;
t_nl := dbms_xslprocessor.selectnodes(t_nd,
'/styleSheet/cellXfs/xf/@numFmtId',
t_ns);
for i in 0 .. dbms_xmldom.getlength(t_nl) - 1 loop
t_xf_date(i) := t_numfmt_date.exists(dbms_xmldom.getnodevalue(dbms_xmldom.item(t_nl,
i)));
end loop;
dbms_xmldom.freenode(t_nd);
t_nd := blob2node(get_file(p_doc, 'xl/sharedStrings.xml'));
if not dbms_xmldom.isnull(t_nd) then
t_x := 0;
t_xx := 10000;
loop
t_nl := dbms_xslprocessor.selectnodes(t_nd,
'/sst/si[position()>="' ||
to_char(t_x * t_xx + 1) ||
'" and position()<="' ||
to_char((t_x + 1) * t_xx) || '"]',
t_ns);
exit when dbms_xmldom.getlength(t_nl) = 0;
t_x := t_x + 1;
for i in 0 .. dbms_xmldom.getlength(t_nl) - 1 loop
t_sc := t_strings.count;
t_strings(t_sc) := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl,
i),
'.');
if t_strings(t_sc) is null then
t_strings(t_sc) := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl,
i),
'*/text()');
if t_strings(t_sc) is null then
t_nl2 := dbms_xslprocessor.selectnodes(dbms_xmldom.item(t_nl,
i),
'r/t/text()');
for j in 0 .. dbms_xmldom.getlength(t_nl2) - 1 loop
t_strings(t_sc) := t_strings(t_sc) ||
dbms_xmldom.getnodevalue(dbms_xmldom.item(t_nl2,
j));
end loop;
end if;
end if;
end loop;
end loop;
end if;
t_nd2 := blob2node(get_file(p_doc, 'xl/_rels/workbook.xml.rels'));
for i in 1 .. t_sheet_ids.count loop
if (p_sheets is null or
instr(':' || p_sheets || ':', ':' || to_char(i) || ':') > 0 or
instr(':' || p_sheets || ':', ':' || t_sheet_names(i) || ':') > 0) then
--跟踪日志
--p_ins_log(t_sheet_names(i));
t_max_c := 0;
t_rows.delete;
t_data(t_data.count + 1).name := t_sheet_names(i);
--my_log('read ' || t_sheet_names(i));
t_val := dbms_xslprocessor.valueof(t_nd2,
'/Relationships/Relationship[@Id="' ||
t_sheet_ids(i) || '"]/@Target',
'xmlns="http://schemas.openxmlformats.org/package/2006/relationships"');
t_nd := blob2node(get_file(p_doc, 'xl/' || t_val));
t_x := 0;
t_xx := 10000;
loop
t_nl3 := dbms_xslprocessor.selectnodes(t_nd,
'/worksheet/sheetData/row[position()>="' ||
to_char(t_x * t_xx + 1) ||
'" and position()<="' ||
to_char((t_x + 1) * t_xx) || '"]');
exit when dbms_xmldom.getlength(t_nl3) = 0;
t_x := t_x + 1;
for r in 0 .. dbms_xmldom.getlength(t_nl3) - 1 loop
t_nl2 := dbms_xslprocessor.selectnodes(dbms_xmldom.item(t_nl3,
r),
'c[v]');
for j in 0 .. dbms_xmldom.getlength(t_nl2) - 1 loop
t_r := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl2,
j),
'@r', t_ns);
t_rr := ltrim(t_r, rtrim(t_r, ''));
t_c := col_alfan(rtrim(t_r, ''));
t_max_c := greatest(t_max_c, t_c);
t_val := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl2,
j), 'v');
t_t := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl2,
j),
'@t');
if t_t = 's' then
if t_val is not null then
t_rows(t_rr)(t_c).data_type := 'S';
t_rows(t_rr)(t_c).string_val := t_strings(to_number(t_val));
end if;
elsif t_t in ('str', 'inlineStr', 'e') then
if t_val is not null then
t_rows(t_rr)(t_c).data_type := 'S';
t_rows(t_rr)(t_c).string_val := t_val;
end if;
else
t_nr := to_number(t_val,
case
when instr(t_val, 'E') = 0 then
translate(t_val, '.012345678,-+', 'D999999999')
else
translate(substr(t_val, 1, instr(t_val, 'E') - 1),
'.012345678,-+', 'D999999999') || 'EEEE'
end, 'NLS_NUMERIC_CHARACTERS=.,');
t_s := dbms_xslprocessor.valueof(dbms_xmldom.item(t_nl2,
j), '@s');
if t_s is not null and t_xf_date.exists(to_number(t_s)) and
t_xf_date(to_number(t_s)) then
t_rows(t_rr)(t_c).data_type := 'D';
if t_date1904 then
t_rows(t_rr)(t_c).date_val := to_date('01-01-1904',
'DD-MM-YYYY') + t_nr;
else
t_rows(t_rr)(t_c).date_val := to_date('01-03-1900',
'DD-MM-YYYY') +
(t_nr - 61);
end if;
else
t_rows(t_rr)(t_c).data_type := 'N';
t_rows(t_rr)(t_c).number_val := t_nr;
end if;
end if;
end loop;
end loop;
end loop;
dbms_xmldom.freenode(t_nd);
if t_rows.count > 0 then
t_c := t_rows(t_rows.last).first;
t_type := t_rows(t_rows.last)(t_c).data_type;
for r in 1 .. t_rows.last - 1 loop
if not t_rows.exists(r) then
t_rows(r)(t_c).data_type := t_type;
end if;
end loop;
if t_rows.count > 1 then
for c in 1 .. t_max_c loop
t_type := null;
for r in 2 .. t_rows.last loop
if t_rows(r).exists(c) then
t_type := t_rows(r)(c).data_type;
exit;
end if;
end loop;
if t_type is null then
if t_rows(1).exists(c) then
t_type := t_rows(1)(c).data_type;
else
t_type := 'S';
end if;
end if;
for r in 1 .. t_rows.last loop
if not t_rows(r).exists(c) then
t_rows(r)(c).data_type := t_type;
end if;
end loop;
end loop;
else
for c in 1 .. t_max_c loop
if not t_rows(1).exists(c) then
t_rows(1)(c).data_type := 'S';
end if;
end loop;
end if;
end if;
t_data(t_data.count).rows := t_rows;
end if;
end loop;
dbms_xmldom.freenode(t_nd2);
return t_data;
end; begin
if dbms_lob.substr(p_document, 8, 1) = hextoraw('D0CF11E0A1B11AE1') then
--dbms_output.put_line( 'parsing XLS' );
--t_what := 'XLS-file';
--t_collection_base := :col_name;
l_process_phase := 0;
t_data := parse_xls(p_document, p_sheets);
l_process_phase := 10;
--DBMS_OUTPUT.PUT_LINE('parsed,' || t_data.count || ' sheets found');
--my_log('moving to Collection(s)');
--apex_collection.create_or_truncate_collection(t_collection_base ||'_$MAP');
for i in 1 .. t_data.count loop
--t_collection_name := t_collection_base || to_char(nullif(i, 1));
--my_log('moving sheet ' || i || ': ' || t_data(i).name || ' to ' ||t_collection_name);
/*
apex_collection.add_member(t_collection_base || '_$MAP',
p_c001 => t_data(i).name,
p_c002 => t_collection_name,
p_n001 => i);
apex_collection.create_or_truncate_collection(t_collection_name);*/
if t_data(i).rows.count() > 0 then
--t2.delete;
--DBMS_OUTPUT.PUT_LINE('t_data(i).rows.count():' || t_data(i).rows.count());
--DBMS_OUTPUT.PUT_LINE('t_sheets(i).name:' || t_sheets(i-1).name);
for r in 1 .. t_data(i).rows.last loop
if t_data(i).rows.exists(r) then
pipe row(xyg_pub_data_upload_obj('EXCEL-XLS' --P_SOURCE_TYPE
, t_sheets(i - 1).name
--P_BATCH_CODE
, null
--P_BATCH_NAME
, r, g1(i, r, 1), g1(i, r, 2),
g1(i, r, 3), g1(i, r, 4),
g1(i, r, 5), g1(i, r, 6),
g1(i, r, 7), g1(i, r, 8),
g1(i, r, 9), g1(i, r, 10),
g1(i, r, 11), g1(i, r, 12),
g1(i, r, 13), g1(i, r, 14),
g1(i, r, 15), g1(i, r, 16),
g1(i, r, 17), g1(i, r, 18),
g1(i, r, 19), g1(i, r, 20),
g1(i, r, 21), g1(i, r, 22),
g1(i, r, 23), g1(i, r, 24),
g1(i, r, 25), g1(i, r, 26),
g1(i, r, 27), g1(i, r, 28),
g1(i, r, 29), g1(i, r, 30), 0,
null));
else
--t2(1)(r) := ''
null;
end if;
end loop;
end if;
end loop;
elsif dbms_lob.substr(p_document, 4, 1) = hextoraw('504B0304') then
--log( 'parsing XLSX' );
--t_what := 'XLSX-file';
--t_collection_base := :col_name;
t_data := parse_xlsx(p_document, p_sheets);
--my_log('parsed,' || t_data.count || ' sheets found');
--my_log('moving to Collection(s)');
--apex_collection.create_or_truncate_collection(t_collection_base ||'_$MAP');
for i in 1 .. t_data.count loop
/*
t_collection_name := t_collection_base || to_char(nullif(i, 1));
my_log('moving sheet ' || i || ': ' || t_data(i).name || ' to ' ||t_collection_name);
apex_collection.add_member(t_collection_base || '_$MAP',
p_c001 => t_data(i).name,
p_c002 => t_collection_name,
p_n001 => i);
apex_collection.create_or_truncate_collection(t_collection_name);*/
if t_data(i).rows.count() > 0 then
--t2.delete;
for r in 1 .. t_data(i).rows.last loop
if t_data(i).rows.exists(r) then
pipe row(xyg_pub_data_upload_obj('EXCEL-XLSX' --P_SOURCE_TYPE
, t_sheet_names(i)
--P_BATCH_CODE
, null
--P_BATCH_NAME
, r, g2(i, r, 1), g2(i, r, 2),
g2(i, r, 3), g2(i, r, 4),
g2(i, r, 5), g2(i, r, 6),
g2(i, r, 7), g2(i, r, 8),
g2(i, r, 9), g2(i, r, 10),
g2(i, r, 11), g2(i, r, 12),
g2(i, r, 13), g2(i, r, 14),
g2(i, r, 15), g2(i, r, 16),
g2(i, r, 17), g2(i, r, 18),
g2(i, r, 19), g2(i, r, 20),
g2(i, r, 21), g2(i, r, 22),
g2(i, r, 23), g2(i, r, 24),
g2(i, r, 25), g2(i, r, 26),
g2(i, r, 27), g2(i, r, 28),
g2(i, r, 29), g2(i, r, 30), 0,
null));
else
--t2(1)(r) := '';
null;
end if;
end loop;
end if;
end loop;
--RETURN CONVER_XLSX_TO_TAB(P_DOCUMENT,P_SHEETS,P_RAISE);
else
if p_raise = /*xyg_pub_const_pkg.*/
c_true then
l_process_phase := 97;
raise no_data_found;
else
l_process_phase := 98;
null;
end if;
end if;
l_process_phase := 99;
return;
exception
when others then
if p_raise = /*xyg_pub_const_pkg.*/
c_true then
--DBMS_OUTPUT.PUT_LINE (R_LINE || '-' || V_PROCESS_MESSAGE);
/*XYG_PUB_COMMON_PKG.RAISE_ERROR (
'-20001' --'ERR_DEFAULT_CODE'
,SQLERRM
,'ERROR RAISE!程序进度:' || L_PROCESS_PHASE
);*/
dbms_output.put_line('程序进度:' || l_process_phase);
raise;
else
return;
--return -1
end if;
end; end xyg_pub_data_upload_pkg;
--使用方法
select *
from table(xyg_pub_data_upload_pkg.conver_excel_to_tab(
xyg_pub_data_upload_pkg.convert_file_blob('XLS_DIR_TEST','test.xlsx'),'', 1))

Oracle读取excel的更多相关文章

  1. 【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable

    OleDbDataAdapter方式: /// <summary> /// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </summ ...

  2. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  3. poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算

    /** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...

  4. Oracle导出excel

    oracle导出excel(非csv)的方法有两种,1.使用sqlplus  spool,2.使用包体 现将网上相关代码整理后贴出以备不时之需: 使用sqlplus: 使用sqlplus需要两个文件: ...

  5. C#读取Excel,或者多个excel表,返回dataset

    把excel 表作为一个数据源进行读取 /// <summary> /// 读取Excel单个Sheet /// </summary> /// <param name=& ...

  6. PHP读取EXCEL时间

    在使用php读取excel表格中的时间时得到一串数字而不是时间:40359.58333333334 excel 中的时间值是自1900年以来的天数,注意是格林威治时间php 中的时间值是自1970年以 ...

  7. Open Xml 读取Excel中的图片

      在我的一个项目中,需要分析客户提供的Excel, 读出其中的图片信息(显示在Excel的第几行,第几列,以及图片本身). 网络上有许多使用Open Xml插入图片到Word,Excel的文章, 但 ...

  8. 使用Open xml 操作Excel系列之一-读取Excel

    一. 安装Open Xml SDK 从微软网站下载Open xml SDK,安装SDK. 二. 在项目中添加对DocumentFormat.OpenXml库的引用

  9. 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature

    写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...

随机推荐

  1. 借助亚马逊S3和RapidMiner将机器学习应用到文本挖掘

    本挖掘典型地运用了机器学习技术,例如聚类,分类,关联规则,和预测建模.这些技术揭示潜在内容中的意义和关系.文本发掘应用于诸如竞争情报,生命科学,客户呼声,媒体和出版,法律和税收,法律实施,情感分析和趋 ...

  2. Java模块化规范之争(转载)

    经过近20年的发展,Java语言已成为今日世界上最成功.使用的开发者人数最多的语言之一,Java世界中无数商业的或开源的组织.技术和产品共同构成了一个无比庞大的生态系统. 与大多数开发人员的普遍认知不 ...

  3. Android开发-之监听button点击事件

    一.实现button点击事件的方法 实现button点击事件的监听方法有很多种,这里总结了常用的四种方法: 1.匿名内部类 2.外部类(独立类) 3.实现OnClickListener接口 4.添加X ...

  4. h5应用缓存及收藏时Icon显示

    h5应用实现离线缓存,加载后,断网仍然可以继续使用. 一.需求 转行做h5,目前做赛道游戏,动手做了个赛道编辑器web版的,由于web版需要开启服务器才能使用,策划要想回家使用就要发布到外网服务器了, ...

  5. 【Data Cluster】真机环境下MySQL数据库集群搭建

    真机环境下MySQL-Cluster搭建文档  摘要:本年伊始阶段,由于实验室对不同数据库性能测试需求,才出现MySQL集群搭建.购置主机,交换机,双绞线等一系列准备工作就绪,也就开始集群搭建.起初笔 ...

  6. Vertica删除历史分区数据

    假设test用户下创建的t_jingyu表 vsql -Utest -wtestpwd create table t_jingyu( col1 int, col2 varchar, col3 time ...

  7. 网页或微信小程序中使元素占满整个屏幕高度

    在项目中经常要用到一个容器元素占满屏幕高度和宽度,然后再在这个容器元素里放置其他元素. 宽度很简单就是width:100% 但是高度呢,我们知道的是height:100%必须是在父元素的高度给定了的情 ...

  8. C# 条件编译

    本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...

  9. 回车去替换铵钮的click点击功能

    某一时候,我们不想在form的所有必填的域均完成之后,再去使用mouse去点击铵钮来提交数据.而是直接按回车去focus提交的铵钮来提交. 可以写jQuery script程序:

  10. StringBuilder的使用

    今天用到了StringBuilder来拼接查询语句,发现这个真好用,决定做个小结. 百度一个StringBuilder的定义:String 对象是不可改变的.每次使用 System.String 类中 ...