推荐阅读:Oracle导出excel数据
废话不多说了,直接给大家奔入主题了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
--解析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));
|
包体内容,包头:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
|
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( '01' )) = hextoraw( '01' ) 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( '00' ));
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),
'03' )) when '02' then utl_raw.cast_to_binary_integer(utl_raw.bit_and(p_rk,
'FCFFFFFF' ),
utl_raw.little_endian) / 4 when '03' then utl_raw.cast_to_binary_integer(utl_raw.bit_and(p_rk,
'FCFFFFFF' ),
utl_raw.little_endian) / 400 when '00' then utl_raw.cast_to_binary_double(utl_raw.concat( '00000000' ,
p_rk),
utl_raw.little_endian) when '01' then utl_raw.cast_to_binary_double(utl_raw.concat( '00000000' ,
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( '00' );
c_dir_storage constant raw(1) := hextoraw( '01' );
c_dir_stream constant raw(1) := hextoraw( '02' );
c_dir_lock constant raw(1) := hextoraw( '03' );
c_dir_property constant raw(1) := hextoraw( '04' );
c_dir_root constant raw(1) := hextoraw( '05' );
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( '0908' ) and
dbms_lob.substr(t_workbook, 2, t_ind + 4) in
(hextoraw( '0005' ), hextoraw( '0006' )) and
dbms_lob.substr(t_workbook, 2, t_ind + 6) = hextoraw( '0500' )) then
t_biff5 := dbms_lob.substr(t_workbook, 2, t_ind + 4) =
hextoraw( '0005' );
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( '08' )) =
hextoraw( '08' ) 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( '04' )) =
hextoraw( '04' ) 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( '8500' ) 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( '01' )) = hextoraw( '01' ) 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( '00' ));
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( '2200' ) then
t_date1904 := dbms_lob.substr(t_workbook, 2, t_ind + 4) =
hextoraw( '0100' );
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( '01' )) = hextoraw( '01' ) 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( '00' ));
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( '4200' ) then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if (rawtohex(t_rec) in ( '1027' , '0080' ) 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( '0908' ) and
dbms_lob.substr(t_workbook, 2, t_ind + 4) = hextoraw( '0006' ) and
dbms_lob.substr(t_workbook, 2, t_ind + 6) = hextoraw( '1000' ) 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( '0302' ) 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( '0600' ) then
t_rec := dbms_lob.substr(t_workbook, t_len, t_ind + 4);
if (rawtohex(utl_raw.substr(t_rec, 7, 1)) not in
( '00' , '01' , '02' , '03' ) 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 '01' then
t_cell.data_type := 'S' ;
t_cell.string_val := case
rawtohex(utl_raw.substr(t_rec, 9, 1))
when '00' then
'FALSE'
when '01' 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 '02' then
null ;
when '00' then
if dbms_lob.substr(t_workbook, 2, t_ind + t_len + 4) =
hextoraw( '0702' ) 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( '0402' ) 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( '08' )) = hextoraw( '08' ) 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(' 0800 ') 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(' 0000 ') 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 ', ' 1 ');
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, ' 0123456789 '));
t_c := col_alfan(rtrim(t_r, ' 0123456789 '));
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;
|
--使用方法
1
2
3
|
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))
|