-
Notifications
You must be signed in to change notification settings - Fork 5
/
cxlsx_to_csv.c
938 lines (871 loc) · 29.5 KB
/
cxlsx_to_csv.c
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
/*****************************************************************
NAME:
cxlsx_to_csv - convert Excel 2007 files to .CSV
USAGE:
cxlsx_to_csv -if input.xlsx [-sh sheet_id] [-of output.csv]
COMPILATION:
cc -DCONFIG_EXPAT -o cxlsx_to_csv cxlsx_to_csv.c -l expat
or
cc -DCONFIG_MXML -o cxlsx_to_csv cxlsx_to_csv.c -l mxml
or
cc -DCONFIG_PARSIFAL -o cxlsx_to_csv cxlsx_to_csv.c -lparsifal
or (to benchmark the time used by decompressing step)
cc -o cxlsx_to_csv cxlsx_to_csv.c
Must be used with Expat compiled for UTF-8 output.
** Copyright (C) 2015 Victor Paesa
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*****************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include "miniz.c"
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;
#include <stdio.h>
#include <string.h>
#ifdef CONFIG_PARSIFAL
#include "libparsifal/parsifal.h"
#endif /* CONFIG_PARSIFAL */
#ifdef CONFIG_MXML
#include <mxml.h>
#endif /* CONFIG_MXML */
#ifdef CONFIG_EXPAT
#include <expat.h>
#if defined(__amigaos__) && defined(__USE_INLINE__)
#include <proto/expat.h>
#endif
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
#endif /* CONFIG_EXPAT */
static char *usage_str = "\n\
NAME:\n\
cxlsx_to_csv - convert Excel 2007 files to .CSV\n\
\n\
SYNOPSIS:\n\
cxlsx_to_csv -if input.xlsx [-sh sheet_id] [-of output.csv]\n\
input.xlsx input spreadsheet in Excel 2007 format (Office Open XML)\n\
sheet_id name of the sheet within the workbook (default is first one)\n\
output.csv output CSV file (default is STDOUT)\n\
\n\
CAVEATS:\n\
Separator in output CSV is comma.\n\
";
// https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3&usg=AFQjCNHniIQ4KTIFQZ6efVfpDtETwU9Cmw
// Total number of characters that an Excel cell can contain: 32,767
#define BUFFSIZE 40960
/*
** An object used to parse XML content of XLSX
*/
typedef struct XLSXCtx XLSXCtx;
struct XLSXCtx {
FILE *outf;
int xml_depth; /* Current dept while parsing the XML tree */
char **shrdstr_array;
int shrdstr_num, shrdstr_cnt;
int sheet_num_rows, sheet_num_cols;
int current_row, current_col, expected_col;
int lookup_v;
#ifdef CONFIG_PARSIFAL
XMLCH *sheet_cur_ptr;
XMLCH *sheet_end_ptr;
#endif /* CONFIG_PARSIFAL */
#ifndef CONFIG_MXML
int shrdstr_tv; /* Flag to look for a shared string when inside a <t> or <v> element */
char *shrdstr_tv_val; /* Value of a shared string when inside a <t> element in xl/sharedStrings.xml */
/* or Index of a shared string when inside a <v> element in xl/worksheets/sheet1.xml */
char shrdstr_buff[BUFFSIZE];
#endif /* Not(CONFIG_MXML) = CONFIG_EXPAT || CONFIG_PARSIFAL */
};
/*
XLSX files are zip files which contain several xml files with data:
_rels/.rels
docProps/app.xml
docProps/core.xml
xl/_rels/workbook.xml.rels
xl/sharedStrings.xml
xl/worksheets/sheet2.xml
xl/worksheets/sheet1.xml
xl/styles.xml
xl/workbook.xml
[Content_Types].xml
The name of each sheet is in xl/workbook.xml
The individual sheets are kept in xl/worksheets/sheet1.xml
To save on space, Microsoft stores all the character literal values in one common xl/sharedStrings.xml dictionary file. The individual cell value found for this string in the actual sheet1.xml file is just an index into this dictionary.
OLEDB created xlsx use inline strings instead of shared ones.
Some brief tutorial: http://openxmldeveloper.org/files/1928/download.aspx
Dates are stored as day number since 1900/01/01.
Time portion of the date is stored as a fraction of a day, so it has to be multiplied by 60*60*24 (86400) to get the actual number os seconds.
Microsoft does not store empty cells or rows in xl/worksheets/sheet1.xml, so any gaps between values have to be taken care by the code.
To figure out the number of skipped columns one need to be able to figure out the distance between, say, cell "AB67" and "C67". The way columns are named: A through Z, then AA through AZ, then AAA through AAZ, etc., suggests that we may assume they are using a base-26 system and therefore use a simple conversion method from a base-26 to the decimal system and then use subtraction to find out the number of commas between columns.
-------------------------------------------------------------------------
xl/sharedStrings.xml has in "sst:uniqueCount" a count of the number of unique strings
unzip -c 3x2.xlsx xl/sharedStrings.xml | tidy -indent -xml
-------------------------------------------------------------------------
<sst count="9" uniqueCount="5">
<si>
<t>Col1</t>
</si>
<si>
<t>Col2</t>
</si>
<si>
<t>Col3</t>
</si>
<si>
<t>a</t>
</si>
<si>
<t>b</t>
</si>
<si>
<t>c</t>
</si>
</sst>
-------------------------------------------------------------------------
-------------------------------------------------------------------------
xl/worksheets/sheet1.xml has in "dimension:ref" the enclosing range of cells used
unzip -c 3x2.xlsx xl/worksheets/sheet1.xml | tidy -indent -xml
-------------------------------------------------------------------------
<worksheet>
<dimension ref="A1:C3" />
<sheetData>
<row r="1" spans="1:3">
<c r="A1" t="s">
<v>0</v>
</c>
<c r="B1" t="s">
<v>1</v>
</c>
<c r="C1" t="s">
<v>27.344</v>
</c>
</row>
<row r="2" spans="1:3">
<c r="A2" t="s">
<v>3</v>
</c>
<c r="B2" s="1">
<v>42283</v>
</c>
<c r="C2" t="s">
<v>3</v>
</c>
</row>
<row r="3">
<c r="A3" t="inlineStr">
<is>
<t>some value</t>
</is>
</c>
<c r="B4" t="inlineStr">
<is>
<t>some other value</t>
</is>
</c>
<c r="C4" t="inlineStr">
<is>
<t>yet some value</t>
</is>
</c>
</row>
</sheetData>
</worksheet>
-------------------------------------------------------------------------
-------------------------------------------------------------------------
xl/styles.xml
-------------------------------------------------------------------------
<styleSheet>
<numFmts count="10">
<numFmt numFmtId="164" formatCode="GENERAL" />
<numFmt numFmtId="165" formatCode="DD/MM/YY" />
<numFmt numFmtId="166" formatCode="DD/MM/YYYY" />
<numFmt numFmtId="167" formatCode="D" de "MMM" de "YY" />
<numFmt numFmtId="168" formatCode="D" de "MMM" de "YYYY" />
<numFmt numFmtId="169" formatCode="DDDD, MMMM\ DD", "YYYY" />
<numFmt numFmtId="170" formatCode="YYYY\-MM\-DD" />
<numFmt numFmtId="171" formatCode="YYYY\-MM\-DD" />
<numFmt numFmtId="172" formatCode="DDDD, D\ MMMM\ YYYY" />
<numFmt numFmtId="173" formatCode="YYYY\-MM\-DD\ HH:MM:SS.SSS" />
</numFmts>
</styleSheet>
-------------------------------------------------------------------------
*/
/* CSV escaping:
If a cell value contains a comma or a line feed, the entire value has to be enclosed in doublequotes.
If a cell value contains a doublequote each of them has to be doubled and then the value should be enclosed in doublequotes.
*/
/*
** CSV code from sqlite
*/
/*
** If a field contains any character identified by a 1 in the following
** array, then the string must be quoted for CSV.
*/
static const char needCsvQuote[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
/*
** Output a single term of CSV. Actually, colSeparator is used for
** the separator, which may or may not be a comma. "" is
** the null value. Strings are quoted if necessary. The separator
** is only issued if bSep is true.
*/
static inline void output_csv(FILE *out, const char colSeparator, const char *z, int bSep)
{
if (z==0) {
//fprintf(out,"%s","");
} else{
int i;
for(i=0; z[i]; i++){
if (needCsvQuote[((unsigned char*)z)[i]] || (z[i]==colSeparator)) {
i = 0;
break;
}
}
if (i==0) {
putc('"', out);
for (i=0; z[i]; i++) {
if (z[i]=='"')
putc('"', out);
putc(z[i], out);
}
putc('"', out);
} else {
fprintf(out, "%s", z);
}
}
if (bSep) {
putc(colSeparator, out);
}
}
static inline void excelcolrow(char *string, int *outcol, int *outrow)
{
int i, col, base;
col = 0;
base = 1;
for (i = 0; i < strlen(string); i++) {
if (isalpha(string[i])) {
col = col * 26 + ((toupper(string[i])) - 'A' + 1);
}
else
break;
}
*outcol = col;
*outrow = atoi(string + i);
return;
}
static inline void rangecolrow(char *string, int *outcol, int *outrow)
{
int col, row;
char *coloninstr;
coloninstr = strchr(string, ':');
if (coloninstr) {
string = coloninstr + 1;
//fprintf(stderr, "rangecolrow: %s \n", string);
}
excelcolrow(string, &col, &row);
*outcol = col;
*outrow = row;
}
#ifdef CONFIG_EXPAT
static void XMLCALL StartSharedStrings(void *data, const char *el, const char **attr)
{
int i;
XLSXCtx *ctx = data;
if ((ctx->xml_depth == 0) && (!strcmp(el, "sst"))) {
for (i = 0; attr[i]; i += 2) {
if (!strcmp(attr[i], "uniqueCount")) {
//fprintf(stderr, " %s='%s'\n", attr[i], attr[i + 1]);
ctx->shrdstr_cnt = atoi(attr[i + 1]);
ctx->shrdstr_array = calloc(sizeof(char *), ctx->shrdstr_cnt);
}
}
}
if (((ctx->xml_depth == 2)||(ctx->xml_depth == 3)) && (!strcmp(el, "t"))) {
ctx->shrdstr_tv = 1;
ctx->shrdstr_tv_val = ctx->shrdstr_buff;
*(ctx->shrdstr_tv_val) = 0;
}
ctx->xml_depth++;
}
static void XMLCALL EndSharedStrings(void *data, const char *el)
{
XLSXCtx *ctx = data;
ctx->xml_depth--;
// "t" at depth 3 are due to multiple styles in cell, and then we need to concat the substrings
if (((ctx->xml_depth == 2)||(ctx->xml_depth == 3)) && (!strcmp(el, "t"))) {
ctx->shrdstr_tv = 0;
if (ctx->shrdstr_array[ctx->shrdstr_num]) {
int prevl, currl;
char *p;
prevl = strlen(ctx->shrdstr_array[ctx->shrdstr_num]);
currl = strlen(ctx->shrdstr_buff);
p = malloc(sizeof(char) * (prevl + currl + 1));
memcpy(p, ctx->shrdstr_array[ctx->shrdstr_num], prevl);
memcpy(p + prevl, ctx->shrdstr_buff, currl);
p[prevl + currl + 1] = 0;
free(ctx->shrdstr_array[ctx->shrdstr_num]);
ctx->shrdstr_array[ctx->shrdstr_num] = p;
}
else
ctx->shrdstr_array[ctx->shrdstr_num] = strdup(ctx->shrdstr_buff);
}
if ((ctx->xml_depth == 1) && (!strcmp(el, "si")))
ctx->shrdstr_num++;
}
static void XMLCALL ChrHndlr(void *data, const char *s, int len)
{
char *src;
XLSXCtx *ctx = data;
if (ctx->shrdstr_tv) {
src = (char *) s;
while (len) {
*(ctx->shrdstr_tv_val)++ = *src++;
len--;
}
*(ctx->shrdstr_tv_val) = 0;
}
}
static void XMLCALL StartSheet(void *data, const char *el, const char **attr)
{
int i, j;
XLSXCtx *ctx = data;
if ((ctx->xml_depth == 1) && (!strcmp(el, "dimension"))) {
for (i = 0; attr[i]; i += 2) {
if (!strcmp(attr[i], "ref")) {
//fprintf(stderr, "dimension %s='%s'\n", attr[i], attr[i + 1]);
rangecolrow((char *) attr[i + 1], &(ctx->sheet_num_cols), &(ctx->sheet_num_rows));
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "row"))) {
for (i = 0; attr[i]; i += 2) {
// (!strcmp(attr[i], "r")
if ((*attr[i] == 'r') && attr[i][1] == '\0') {
//fprintf(stderr, "row %s='%s'\n", attr[i], attr[i + 1]);
ctx->expected_col = 1;
}
}
}
if ((ctx->xml_depth == 3) && (!strcmp(el, "c"))) {
ctx->lookup_v = 0;
for (i = 0; attr[i]; i += 2) {
if (!strcmp(attr[i], "r")) {
//fprintf(stderr, "c %s='%s'\n", attr[i], attr[i + 1]);
excelcolrow((char *) attr[i + 1], &(ctx->current_col), &(ctx->current_row));
for (j = ctx->expected_col; (j<ctx->current_col)&&(j<ctx->sheet_num_cols); j++)
putc(',', ctx->outf);
ctx->expected_col = ctx->current_col+1;
}
else if (!strcmp(attr[i], "t")) {
//fprintf(stderr, "c %s='%s'\n", attr[i], attr[i + 1]);
if (*attr[i + 1] == 's') {
ctx->lookup_v = -1;
}
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
}
if ((ctx->xml_depth == 4) && (*el == 'v') && (el[1] == '\0')) {
ctx->shrdstr_tv = 1;
ctx->shrdstr_tv_val = ctx->shrdstr_buff;
*(ctx->shrdstr_tv_val) = 0;
}
ctx->xml_depth++;
}
static void XMLCALL EndSheet(void *data, const char *el)
{
int j;
XLSXCtx *ctx = data;
ctx->xml_depth--;
if ((ctx->xml_depth == 4) && (*el == 'v') && (el[1] == '\0')) {
ctx->shrdstr_tv = 0;
if (ctx->lookup_v) {
//fprintf(stderr, "v %s\n", ctx->shrdstr_array[atoi(ctx->shrdstr_buff)]);
output_csv(ctx->outf, ',', ctx->shrdstr_array[atoi(ctx->shrdstr_buff)], (ctx->current_col < ctx->sheet_num_cols));
}
else {
//fprintf(stderr, "v %s\n", ctx->shrdstr_buff);
output_csv(ctx->outf, ',', ctx->shrdstr_buff, (ctx->current_col < ctx->sheet_num_cols));
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "row"))) {
for (j = ctx->expected_col; j<ctx->sheet_num_cols; j++)
putc(',', ctx->outf);
fprintf(ctx->outf, "\r\x0A");
// TODO: Check if \r\x0A portable between Windows & UNIX
}
}
#endif /* CONFIG_EXPAT */
#ifdef CONFIG_MXML
static void SharedStrings(mxml_node_t *node, mxml_sax_event_t event, void *data)
{
const char *el, *uniqueCount, *value;
XLSXCtx *ctx = data;
if (event == MXML_SAX_ELEMENT_OPEN) {
if (ctx->xml_depth == 0) {
el = mxmlGetElement(node);
if (!strcmp(el, "sst")) {
uniqueCount = mxmlElementGetAttr(node, "uniqueCount");
if (uniqueCount) {
//fprintf(stderr, " uniqueCount='%s'\n", uniqueCount);
ctx->shrdstr_cnt = atoi(uniqueCount);
ctx->shrdstr_array = malloc(sizeof(char *) * ctx->shrdstr_cnt);
}
}
}
ctx->xml_depth++;
//fprintf(stderr, "shared strings '%s' %d\n", el, ctx->xml_depth);
}
else if (event == MXML_SAX_DATA) {
if (ctx->xml_depth == 3) {
el = mxmlGetElement(mxmlGetParent(node));
if (!strcmp(el, "t")) {
value = mxmlGetOpaque(mxmlGetParent(node));
//fprintf(stderr, " shrStr[%d]='%s'\n", ctx->shrdstr_num, value);
ctx->shrdstr_array[ctx->shrdstr_num] = strdup(value);
ctx->shrdstr_num++;
}
}
}
else if (event == MXML_SAX_ELEMENT_CLOSE) {
ctx->xml_depth--;
}
}
static void Sheet(mxml_node_t *node, mxml_sax_event_t event, void *data)
{
int i, j;
const char *el, *ref, *r, *t, *value;
XLSXCtx *ctx = data;
if (event == MXML_SAX_ELEMENT_OPEN) {
el = mxmlGetElement(node);
if ((ctx->xml_depth == 1) && (!strcmp(el, "dimension"))) {
ref = mxmlElementGetAttr(node, "ref");
if (ref) {
rangecolrow((char *)ref, &(ctx->sheet_num_cols), &(ctx->sheet_num_rows));
//fprintf(stderr, " dimension ref='%s'\n", ref);
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "row"))) {
r = mxmlElementGetAttr(node, "r");
if (r) {
//fprintf(stderr, "row r='%s'\n", r);
ctx->expected_col = 1;
}
}
if ((ctx->xml_depth == 3) && (!strcmp(el, "c"))) {
ctx->lookup_v = 0;
r = mxmlElementGetAttr(node, "r");
if (r) {
//fprintf(stderr, "c r='%s'\n", r);
excelcolrow((char *)r, &(ctx->current_col), &(ctx->current_row));
for (j = ctx->expected_col; (j<ctx->current_col)&&(j<ctx->sheet_num_cols); j++)
putc(',', ctx->outf);
ctx->expected_col = ctx->current_col+1;
}
t = mxmlElementGetAttr(node, "t");
if (t) {
//fprintf(stderr, "c t='%s'\n", t);
if (*t == 's') {
ctx->lookup_v = -1;
}
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
ctx->xml_depth++;
}
else if (event == MXML_SAX_DATA) {
if (ctx->xml_depth == 5) {
el = mxmlGetElement(mxmlGetParent(node));
if (!strcmp(el, "v")) {
value = mxmlGetOpaque(mxmlGetParent(node));
if (ctx->lookup_v) {
//fprintf(stderr, "v %s\n", ctx->shrdstr_array[atoi(value)]);
output_csv(ctx->outf, ',', ctx->shrdstr_array[atoi(value)], (ctx->current_col < ctx->sheet_num_cols));
}
else {
//fprintf(stderr, "v %s\n", value);
output_csv(ctx->outf, ',', value, (ctx->current_col < ctx->sheet_num_cols));
}
}
}
}
else if (event == MXML_SAX_ELEMENT_CLOSE) {
ctx->xml_depth--;
if (ctx->xml_depth == 2) {
el = mxmlGetElement(node);
if (!strcmp(el, "row")) {
for (j = ctx->expected_col; j<ctx->sheet_num_cols; j++)
putc(',', ctx->outf);
fprintf(ctx->outf, "\r\x0A");
// TODO: Check if \r\x0A portable between Windows & UNIX
}
}
}
}
#endif /* CONFIG_MXML */
#ifdef CONFIG_PARSIFAL
int StartSharedStrings(void *data, const XMLCH *uri, const XMLCH *localName, const XMLCH *el, LPXMLVECTOR atts)
{
int i;
LPXMLRUNTIMEATT att;
XLSXCtx *ctx = data;
if ((ctx->xml_depth == 0) && (!strcmp(el, "sst"))) {
for (i = 0; i<atts->length; i++) {
att = (LPXMLRUNTIMEATT) XMLVector_Get(atts, i);
if (!strcmp(att->qname, "uniqueCount")) {
//fprintf(stderr, " %s='%s'\n", att->qname, att->value);
ctx->shrdstr_cnt = atoi(att->value);
ctx->shrdstr_array = malloc(sizeof(char *) * ctx->shrdstr_cnt);
}
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "t"))) {
ctx->shrdstr_tv = 1;
ctx->shrdstr_tv_val = ctx->shrdstr_buff;
*(ctx->shrdstr_tv_val) = 0;
}
ctx->xml_depth++;
return 0;
}
int EndSharedStrings(void *data, const XMLCH *uri, const XMLCH *localName, const XMLCH *el)
{
XLSXCtx *ctx = data;
ctx->xml_depth--;
if ((ctx->xml_depth == 2) && (!strcmp(el, "t"))) {
ctx->shrdstr_tv = 0;
ctx->shrdstr_array[ctx->shrdstr_num] = strdup(ctx->shrdstr_buff);
ctx->shrdstr_num++;
}
return 0;
}
int ChrHndlr(void *data, const XMLCH *s, int len)
{
XMLCH *src;
XLSXCtx *ctx = data;
if (ctx->shrdstr_tv) {
src = (XMLCH *) s;
while (len) {
*(ctx->shrdstr_tv_val)++ = *src++;
len--;
}
*(ctx->shrdstr_tv_val) = 0;
}
return 0;
}
int StartSheet(void *data, const XMLCH *uri, const XMLCH *localName, const XMLCH *el, LPXMLVECTOR atts)
{
int i, j;
LPXMLRUNTIMEATT att;
XLSXCtx *ctx = data;
if ((ctx->xml_depth == 1) && (!strcmp(el, "dimension"))) {
for (i = 0; i<atts->length; i++) {
att = (LPXMLRUNTIMEATT) XMLVector_Get(atts, i);
if (!strcmp(att->qname, "ref")) {
//fprintf(stderr, "dimension %s='%s'\n", att->qname, att->value);
rangecolrow((char *) att->value, &(ctx->sheet_num_cols), &(ctx->sheet_num_rows));
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "row"))) {
for (i = 0; i<atts->length; i++) {
att = (LPXMLRUNTIMEATT) XMLVector_Get(atts, i);
if (!strcmp(att->qname, "r")) {
//fprintf(stderr, "row %s='%s'\n", att->qname, att->value);
ctx->expected_col = 1;
}
}
}
if ((ctx->xml_depth == 3) && (!strcmp(el, "c"))) {
ctx->lookup_v = 0;
for (i = 0; i<atts->length; i++) {
att = (LPXMLRUNTIMEATT) XMLVector_Get(atts, i);
if (!strcmp(att->qname, "r")) {
//fprintf(stderr, "c %s='%s'\n", att->qname, att->value);
excelcolrow((char *) att->value, &(ctx->current_col), &(ctx->current_row));
for (j = ctx->expected_col; (j<ctx->current_col)&&(j<ctx->sheet_num_cols); j++)
putc(',', ctx->outf);
ctx->expected_col = ctx->current_col+1;
}
else if (!strcmp(att->qname, "t")) {
//fprintf(stderr, "c %s='%s'\n", att->qname, att->value);
if (*(att->value) == 's') {
ctx->lookup_v = -1;
}
//fprintf(stderr, "cols: %d rows: %d\n", ctx->sheet_num_cols, ctx->sheet_num_rows);
}
}
}
if ((ctx->xml_depth == 4) && (*el == 'v') && (el[1] == '\0')) {
ctx->shrdstr_tv = 1;
ctx->shrdstr_tv_val = ctx->shrdstr_buff;
*(ctx->shrdstr_tv_val) = 0;
}
ctx->xml_depth++;
return 0;
}
int EndSheet(void *data, const XMLCH *uri, const XMLCH *localName, const XMLCH *el)
{
int j;
XLSXCtx *ctx = data;
ctx->xml_depth--;
if ((ctx->xml_depth == 4) && (*el == 'v') && (el[1] == '\0')) {
ctx->shrdstr_tv = 0;
if (ctx->lookup_v) {
//fprintf(stderr, "v %s\n", ctx->shrdstr_array[atoi(ctx->shrdstr_buff)]);
output_csv(ctx->outf, ',', ctx->shrdstr_array[atoi(ctx->shrdstr_buff)], (ctx->current_col < ctx->sheet_num_cols));
}
else {
//fprintf(stderr, "v %s\n", ctx->shrdstr_buff);
output_csv(ctx->outf, ',', ctx->shrdstr_buff, (ctx->current_col < ctx->sheet_num_cols));
}
}
if ((ctx->xml_depth == 2) && (!strcmp(el, "row"))) {
for (j = ctx->expected_col; j<ctx->sheet_num_cols; j++)
putc(',', ctx->outf);
fprintf(ctx->outf, "\r\x0A");
// TODO: Check if \r\x0A portable between Windows & UNIX
}
return 0;
}
int GetSheetBytes(BYTE *buf, int cBytes, int *cBytesActual, void *data)
{
XLSXCtx *ctx = data;
char str[64];
//snprintf(str, 64, "%s", ctx->sheet_cur_ptr);
//fprintf(stderr, "GetSheetBytes %p %d '%s'\n", ctx->sheet_cur_ptr, cBytes, str);
if ((ctx->sheet_end_ptr - ctx->sheet_cur_ptr) >= cBytes) {
memcpy(buf, ctx->sheet_cur_ptr, cBytes);
*cBytesActual = cBytes;
ctx->sheet_cur_ptr += cBytes;
return 0;
}
else {
*cBytesActual = ctx->sheet_end_ptr - ctx->sheet_cur_ptr;
memcpy(buf, ctx->sheet_cur_ptr, *cBytesActual);
ctx->sheet_cur_ptr = ctx->sheet_end_ptr;
return 1;
}
}
void ErrorHandler(LPXMLPARSER parser) {} /* dummy, only for switching ErrorString etc. on */
#endif /* CONFIG_PARSIFAL */
int main(int argc, char *argv[])
{
int i;
size_t sheet_size;
void *sheet_ptr;
XLSXCtx *parse_ctx;
char sheetname[64];
#ifdef CONFIG_EXPAT
XML_Parser p;
#endif /* CONFIG_EXPAT */
#ifdef CONFIG_MXML
mxml_node_t *root_node;
#endif /* CONFIG_MXML */
#ifdef CONFIG_PARSIFAL
LPXMLPARSER parser;
#endif /* CONFIG_PARSIFAL */
int opt_if = 0;
int opt_sh = 0;
int opt_of = 0;
parse_ctx = calloc(1, sizeof(XLSXCtx));
for (i=1; i<argc; i++) {
if (i==opt_if)
continue;
if (!strcmp("-if", argv[i]))
if ((i+1) < argc)
opt_if = i+1;
else {
fputs("'-if' needs an Excel file name for input\n", stderr);
fputs(usage_str, stderr);
return 1;
}
if (i==opt_sh)
continue;
if (!strcmp("-sh", argv[i]))
if ((i+1) < argc)
opt_sh = i+1;
else {
fputs("'-sh' needs a sheet number\n", stderr);
fputs(usage_str, stderr);
return 1;
}
if (i==opt_of)
continue;
if (!strcmp("-of", argv[i]))
if ((i+1) < argc)
opt_of = i+1;
else {
fputs("'-of' needs an CSV file name for output\n", stderr);
fputs(usage_str, stderr);
return 1;
}
}
if (!opt_if) {
fputs("Missing '-if input.xlsx'\n", stderr);
fputs(usage_str, stderr);
return 1;
}
if (!opt_sh) {
//fputs("Missing '-sh sheetnum', hence assuming first sheet.\n", stderr);
opt_sh = 1;
}
else {
opt_sh = atoi(argv[opt_sh]);
// TODO: Check sheet number among existing sheets. Accept sheet names.
if (!opt_sh)
opt_sh = 1;
}
if (!opt_of) {
//fputs("Missing '-of output.csv', hence assuming STDOUT.\n", stderr);
parse_ctx->outf = stdout;
}
else {
parse_ctx->outf = fopen(argv[opt_of], "w");
if (!parse_ctx->outf) {
fprintf(stderr, "Couldn't open output file '%s' .\n", argv[opt_of]);
exit(-1);
}
}
// Process xl/sharedStrings.xml and load them into shrdstr_array[]
sheet_ptr = mz_zip_extract_archive_file_to_heap(argv[opt_if], "xl/sharedStrings.xml", &sheet_size, MZ_ZIP_FLAG_CASE_SENSITIVE);
//fprintf(stderr, "xl/sharedStrings.xml size:%d\n", sheet_size);
if (sheet_ptr) {
parse_ctx->xml_depth = 0;
#ifdef CONFIG_EXPAT
p = XML_ParserCreate(NULL);
if (!p) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
XML_SetUserData(p, parse_ctx);
XML_SetElementHandler(p, StartSharedStrings, EndSharedStrings);
XML_SetCharacterDataHandler(p, ChrHndlr);
if (XML_Parse(p, sheet_ptr, sheet_size, -1) == XML_STATUS_ERROR) {
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
XML_ParserFree(p);
//for (i = 0; i < ctx->shrdstr_cnt; i++)
// printf("%s\n", ctx->shrdstr_array[i]);
#endif /* CONFIG_EXPAT */
#ifdef CONFIG_MXML
root_node = mxmlSAXLoadString(NULL, sheet_ptr, MXML_OPAQUE_CALLBACK, SharedStrings, parse_ctx);
#endif /* CONFIG_MXML */
#ifdef CONFIG_PARSIFAL
if (!XMLParser_Create(&parser)) {
printf("Error creating parser!\n");
exit(-1);
}
_XMLParser_SetFlag(parser, XMLFLAG_NAMESPACES, 0);
_XMLParser_SetFlag(parser, XMLFLAG_EXTERNAL_GENERAL_ENTITIES, 0);
_XMLParser_SetFlag(parser, XMLFLAG_PRESERVE_WS_ATTRIBUTES, -1);
parser->errorHandler = ErrorHandler;
parser->startElementHandler = StartSharedStrings;
parser->endElementHandler = EndSharedStrings;
parser->charactersHandler = ChrHndlr;
parser->UserData = parse_ctx;
parse_ctx->sheet_cur_ptr = sheet_ptr;
parse_ctx->sheet_end_ptr = sheet_ptr + sheet_size;
if (!XMLParser_Parse(parser, GetSheetBytes, parse_ctx, "UTF-8"))
printf("ShareStrings Error: %s\nLine: %d Col: %d\n", parser->ErrorString, parser->ErrorLine, parser->ErrorColumn);
XMLParser_Free(parser);
#endif /* CONFIG_PARSIFAL */
}
else {
//fprintf(stderr, "Warning: could not read xl/sharedStrings.xml\n");
// TODO: Only warn about missing xl/sharedStrings.xml is it referenced by some t="s"
}
// Process xl/worksheets/sheet1.xml and load them into sheet_tbl[,]
sprintf(sheetname, "xl/worksheets/sheet%d.xml", opt_sh);
sheet_ptr = mz_zip_extract_archive_file_to_heap(argv[opt_if], sheetname, &sheet_size, MZ_ZIP_FLAG_CASE_SENSITIVE);
//fprintf(stderr, "%s size:%d\n", sheetname, sheet_size);
if (sheet_ptr) {
parse_ctx->xml_depth = 0;
#ifdef CONFIG_EXPAT
parse_ctx->shrdstr_tv = 0;
p = XML_ParserCreate(NULL);
if (!p) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
XML_SetUserData(p, parse_ctx);
XML_SetElementHandler(p, StartSheet, EndSheet);
XML_SetCharacterDataHandler(p, ChrHndlr);
if (XML_Parse(p, sheet_ptr, sheet_size, -1) == XML_STATUS_ERROR) {
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
XML_ParserFree(p);
#endif /* CONFIG_EXPAT */
#ifdef CONFIG_MXML
root_node = mxmlSAXLoadString(NULL, sheet_ptr, MXML_OPAQUE_CALLBACK, Sheet, parse_ctx);
#endif /* CONFIG_MXML */
#ifdef CONFIG_PARSIFAL
if (!XMLParser_Create(&parser)) {
printf("Error creating parser!\n");
exit(-1);
}
_XMLParser_SetFlag(parser, XMLFLAG_NAMESPACES, 0);
_XMLParser_SetFlag(parser, XMLFLAG_EXTERNAL_GENERAL_ENTITIES, 0);
_XMLParser_SetFlag(parser, XMLFLAG_PRESERVE_WS_ATTRIBUTES, -1);
parser->errorHandler = ErrorHandler;
parser->startElementHandler = StartSheet;
parser->endElementHandler = EndSheet;
parser->charactersHandler = ChrHndlr;
parser->UserData = parse_ctx;
parse_ctx->sheet_cur_ptr = sheet_ptr;
parse_ctx->sheet_end_ptr = sheet_ptr + sheet_size;
if (!XMLParser_Parse(parser, GetSheetBytes, parse_ctx, "UTF-8"))
printf("Sheet Error: %s\nLine: %d Col: %d\n", parser->ErrorString, parser->ErrorLine, parser->ErrorColumn);
XMLParser_Free(parser);
#endif /* CONFIG_PARSIFAL */
}
else {
fprintf(stderr, "Error: could not read sheet number %d.\n", opt_sh);
exit(-1);
}
return 0;
}