-
Notifications
You must be signed in to change notification settings - Fork 86
/
DHxlsReaderIOS.m
450 lines (377 loc) · 13 KB
/
DHxlsReaderIOS.m
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* This file is part of DHlibxls -- permitting code to read Excel(TM) files.
*
* Copyright 2012 David Hoerl All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Only the ObjectiveC code is under this license - separate license may apply to libxls source.
* Read the documents and source headers in the libxls files which you downloaded earlier.
*
* THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "DHxlsReaderIOS.h"
#import "DHcell-Private.h"
#import "xls.h"
#if ! __has_feature(objc_arc)
#error THIS CODE MUST BE COMPILED WITH ARC ENABLED!
#endif
@interface DHxlsReader ()
- (void)setWorkBook:(xlsWorkBook *)wb;
- (void)openSheet:(NSUInteger)sheetNum;
- (void)formatContent:(DHcell *)content withCell:(xlsCell *)cell;
@end
@implementation DHxlsReader
{
xlsWorkBook *workBook;
uint32_t numSheets;
uint32_t activeWorkSheetID; // keep last one active
xlsWorkSheet *activeWorkSheet; // keep last one active
xlsSummaryInfo *summary;
BOOL iterating;
uint32_t lastRowIndex;
uint32_t lastColIndex;
}
+ (DHxlsReader *)xlsReaderFromFile:(NSString *)filePath
{
DHxlsReader *reader;
xlsWorkBook *workBook;
// NSLog(@"sizeof FORMULA=%zd LABELSST=%zd", sizeof(FORMULA), sizeof(LABELSST) );
const char *file = [filePath cStringUsingEncoding:NSUTF8StringEncoding];
if((workBook = xls_open(file, "UTF-8"))) {
reader = [DHxlsReader new];
[reader setWorkBook:workBook];
}
return reader;
}
- (id)init
{
if((self = [super init])) {
activeWorkSheetID = NSNotFound;
}
return self;
}
- (void)dealloc
{
xls_close_summaryInfo(summary);
xls_close_WS(activeWorkSheet);
xls_close_WB(workBook);
}
- (void)setWorkBook:(xlsWorkBook *)wb
{
workBook = wb;
xls_parseWorkBook(workBook);
numSheets = workBook->sheets.count;
summary = xls_summaryInfo(workBook);
}
- (NSString *)libaryVersion
{
return [NSString stringWithCString:xls_getVersion() encoding:NSASCIIStringEncoding];
}
// Sheet Information
- (NSUInteger)numberOfSheets
{
return numSheets;
}
- (NSString *)sheetNameAtIndex:(NSUInteger)idx
{
return idx < numSheets ? [NSString stringWithCString:(char *)workBook->sheets.sheet[idx].name encoding:NSUTF8StringEncoding] : nil;
}
- (BOOL)isSheetVisibleAtIndex:(NSUInteger)idx
{
return idx < numSheets ? (BOOL)workBook->sheets.sheet[idx].visibility : NO;
}
- (void)openSheet:(NSUInteger)sheetNum
{
if(sheetNum >= numSheets) {
iterating = true;
lastColIndex = UINT32_MAX;
lastRowIndex = UINT32_MAX;
} else
if(sheetNum != activeWorkSheetID) {
activeWorkSheetID = sheetNum;
xls_close_WS(activeWorkSheet);
activeWorkSheet = xls_getWorkSheet(workBook, sheetNum);
xls_parseWorkSheet(activeWorkSheet);
}
}
// Random Access
- (DHcell *)cellInWorkSheetIndex:(NSUInteger)sheetNum row:(uint16_t)row col:(uint16_t)col
{
DHcell *content = [DHcell blankCell];
assert(row && col);
[self startIterator:NSNotFound];
[self openSheet:sheetNum];
--row, --col;
NSUInteger numRows = activeWorkSheet->rows.lastrow + 1;
NSUInteger numCols = activeWorkSheet->rows.lastcol + 1;
for (NSUInteger t=0; t<numRows; t++)
{
xlsRow *rowP = &activeWorkSheet->rows.row[t];
for (NSUInteger tt=0; tt<numCols; tt++)
{
xlsCell *cell = &rowP->cells.cell[tt];
NSLog(@"Looking for %d:%d:%d - testing %d:%d Type: 0x%4.4x [t=%d tt=%d]", sheetNum, row, col, cell->row, cell->col, cell->id, t, tt);
if(cell->row < row) break;
if(cell->row > row) return content;
if(cell->id == 0x201) continue; // "Blank" filler cell created by libxls
if(cell->col == col) {
[self formatContent:content withCell:cell];
return content;
}
}
}
return content;
}
- (DHcell *)cellInWorkSheetIndex:(NSUInteger)sheetNum row:(uint16_t)row colStr:(char *)colStr
{
if(strlen(colStr) > 2 || strlen(colStr) == 0) return [DHcell blankCell];
NSInteger col = colStr[0] - 'A';
if(col < 0 || col >= 26) return [DHcell blankCell];
char c = colStr[1];
if(c) {
col *= 26;
NSInteger col2 = c - 'A';
if(col2 < 0 || col2 >= 26) return [DHcell blankCell];
col += col2;
}
col += 1;
return [self cellInWorkSheetIndex:sheetNum row:row col:(uint16_t)col];
}
// Iterate through all cells
- (void)startIterator:(NSUInteger)sheetNum
{
if(sheetNum != NSNotFound) {
[self openSheet:sheetNum];
iterating = true;
lastColIndex = 0;
lastRowIndex = 0;
} else {
iterating = false;
}
}
- (DHcell *)nextCell
{
DHcell *content = [DHcell blankCell];
if(!iterating) return nil;
NSUInteger numRows = activeWorkSheet->rows.lastrow + 1;
NSUInteger numCols = activeWorkSheet->rows.lastcol + 1;
if(lastRowIndex >= numRows) return content;
for (NSUInteger t=lastRowIndex; t<numRows; t++)
{
xlsRow *rowP = &activeWorkSheet->rows.row[t];
for (NSUInteger tt=lastColIndex; tt<numCols; tt++)
{
xlsCell *cell = &rowP->cells.cell[tt];
if(cell->id == 0x201) continue;
lastColIndex = tt + 1;
[ self formatContent:content withCell:cell];
return content;
}
++lastRowIndex;
lastColIndex = 0;
}
// don't make iterator false - user can keep asking for cells, they all just be blank ones though
return content;
}
- (void)formatContent:(DHcell *)content withCell:(xlsCell *)cell
{
NSUInteger col = cell->col;
content.row = cell->row + 1;
{
content.col = col + 1;
char colStr[3];
if(col < 26) {
colStr[0] = 'A' + (char)col;
colStr[1] = '\0';
} else {
colStr[0] = 'A' + (char)(col/26);
colStr[1] = 'A' + (char)(col%26);
}
colStr[2] = '\0';
[content setColStr:colStr];
}
switch(cell->id) {
case 0x0006: //FORMULA
// test for formula, if
if(cell->l == 0) {
content.type = cellFloat;
content.val = [NSNumber numberWithDouble:cell->d];
} else {
if(!strcmp((char *)cell->str, "bool")) {
BOOL b = (BOOL)cell->d;
content.type = cellBool;
content.val = [NSNumber numberWithBool:b];
content.str = b ? @"YES" : @"NO";
} else
if(!strcmp((char *)cell->str, "error")) {
NSInteger err = (NSInteger)cell->d;
content.type = cellError;
content.val = [NSNumber numberWithInteger:err];
content.str = [NSString stringWithFormat:@"%d", err];
} else {
content.type = cellString;
}
}
break;
case 0x00FD: //LABELSST
case 0x0204: //LABEL
content.type = cellString;
content.val = [NSNumber numberWithLong:cell->l]; // possible numeric conversion done for you
break;
case 0x0203: //NUMBER
case 0x027E: //RK
content.type = cellFloat;
content.val = [NSNumber numberWithDouble:cell->d];
break;
default:
content.type = cellUnknown;
break;
}
if(!content.str) {
content.str = [NSString stringWithCString:(char *)cell->str encoding:NSUTF8StringEncoding];
}
// NSLog(@"GOING TO PRINT STRING");
// NSLog(@"Cell creator: t=%d num=%@ str=%@", content.type, content.val, content.str);
}
// Summary Information
- (NSString *)appName { return summary->appName ? [NSString stringWithCString:(char *)summary->appName encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)author { return summary->author ? [NSString stringWithCString:(char *)summary->author encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)category { return summary->category ? [NSString stringWithCString:(char *)summary->category encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)comment { return summary->comment ? [NSString stringWithCString:(char *)summary->comment encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)company { return summary->company ? [NSString stringWithCString:(char *)summary->company encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)keywords { return summary->keywords ? [NSString stringWithCString:(char *)summary->keywords encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)lastAuthor { return summary->lastAuthor? [NSString stringWithCString:(char *)summary->lastAuthor encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)manager { return summary->manager ? [NSString stringWithCString:(char *)summary->manager encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)subject { return summary->subject ? [NSString stringWithCString:(char *)summary->subject encoding:NSUTF8StringEncoding] : @""; }
- (NSString *)title { return summary->title ? [NSString stringWithCString:(char *)summary->title encoding:NSUTF8StringEncoding] : @""; }
@end
#if 0
private:
WorkBook(const WorkBook& that);
WorkBook& operator=(const WorkBook& right);
private:
void OpenSheet(uint32_t sheetNum);
void FormatCell(xlsCell *cell, cellContent& content) const;
xlsString char2string(const uint8_t *ptr) const;
#if XLS_WIDE_STRINGS == 1
bool isAscii(const uint8_t *ptr) const;
#endif
private:
const char *charSet; // must be first ivar
bool isUTF8; // unused in the wstring case
#if XLS_WIDE_STRINGS == 1
iconv_t iconvCD;
#endif
uint32_t numSheets;
xlsWorkBook *workBook;
uint32_t activeWorkSheetID; // keep last one active
xlsWorkSheet *activeWorkSheet; // keep last one active
xlsSummaryInfo *summary;
bool iterating;
uint32_t lastRowIndex;
uint32_t lastColIndex;
};
#endif
#if 0
namespace xls
{
typedef enum { cellBlank=0, cellString, cellInteger, cellFloat, cellBool, cellError, cellUnknown } contentsType;
struct cellContent {
contentsType type;
char colStr[3]; // String "A"..."Z", "AA"..."ZZ" (second char is either nil or a capital letter)
uint32_t col; // 1 based
uint16_t row; // 1 based
xlsString str; // even for numbers these values are formatted as well as provided below
union Val {
long l;
double d;
bool b;
int32_t e;
Val(int x) { l = x; }
~Val() { }
} val;
cellContent(void) :
type(cellBlank),
colStr(),
col(0),
row(0),
val(0) { }
~cellContent() {}
};
class WorkBook
{
public:
#if XLS_WIDE_STRINGS == 0
// characterSet is the 8-bit encoding you want to convert 16-bit unicode strings to
WorkBook(const std::string& fileName, int debug=0, const char *characterSet="UTF-8");
#else
// characterSet has to be UTF-8
WorkBook(const std::string& fileName, int debug=0);
#endif
~WorkBook();
std::string GetLibraryVersion() const;
uint32_t GetSheetCount() const;
// Sheets
xlsString GetSheetName(uint32_t sheetNum) const;
bool GetSheetVisible(uint32_t sheetNum) const;
// Summary
xlsString GetSummaryAppName(void) const;
xlsString GetSummaryAuthor(void) const;
xlsString GetSummaryCategory(void) const;
xlsString GetSummaryComment(void) const;
xlsString GetSummaryCompany(void) const;
xlsString GetSummaryKeywords(void) const;
xlsString GetSummaryLastAuthor(void) const;
xlsString GetSummaryManager(void) const;
xlsString GetSummarySubject(void) const;
xlsString GetSummaryTitle(void) const;
cellContent GetCell(uint32_t workSheetIndex, uint16_t row, uint16_t col); // uses 1 based indexing!
cellContent GetCell(uint32_t workSheetIndex, uint16_t row, const char *colStr); // "A"...."Z" "AA"..."ZZ"
void InitIterator(uint32_t sheetNum = UINT32_MAX); // call this first...
cellContent GetNextCell(void); // ...then this continually til you get a blank cell
void ShowCell(const cellContent& content) const;
private:
WorkBook(const WorkBook& that);
WorkBook& operator=(const WorkBook& right);
private:
void OpenSheet(uint32_t sheetNum);
void FormatCell(xlsCell *cell, cellContent& content) const;
xlsString char2string(const uint8_t *ptr) const;
#if XLS_WIDE_STRINGS == 1
bool isAscii(const uint8_t *ptr) const;
#endif
private:
const char *charSet; // must be first ivar
bool isUTF8; // unused in the wstring case
#if XLS_WIDE_STRINGS == 1
iconv_t iconvCD;
#endif
uint32_t numSheets;
xlsWorkBook *workBook;
uint32_t activeWorkSheetID; // keep last one active
xlsWorkSheet *activeWorkSheet; // keep last one active
xlsSummaryInfo *summary;
bool iterating;
uint32_t lastRowIndex;
uint32_t lastColIndex;
};
}
#endif