forked from andreac/RSSheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSworkBook.m
309 lines (250 loc) · 11.4 KB
/
RSworkBook.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
//
// workBook.m
// excelWriterExample
//
// Created by andrea cappellotto on 14/09/11.
// Copyright 2011 Università degli studi di Trento. All rights reserved.
//
#import "RSworkBook.h"
@interface RSworkBook()
- (NSString*)formatTypeToStringVertical:(verticalAlign)formatType;
- (NSString*)formatTypeToStringHorizontal:(horizontalAlign)formatType;
- (NSString*)formatTypeToCellType:(cellType)formatType;
- (int)countMaxCell:(NSMutableArray*)sheet;
-(BOOL)isEqualStyle:(RSStyle*)style1 toStyle:(RSStyle*)style2;
@end
@implementation RSworkBook
@synthesize version, author, date, defaultStyle;
- (id)init
{
self = [super init];
if (self)
{
defaultStyle = [[RSStyle alloc] init];
defaultStyle.font = [UIFont systemFontOfSize:14];
defaultStyle.size = 14;
defaultStyle.color = [UIColor blackColor];
defaultStyle.alignmentH = RSStyleMiddleAlign;
defaultStyle.alignmentV = RSStyleCenterAlign;
version = 1.0;
author = [[NSString alloc] initWithFormat:@"system"];
date = [NSDate date];
arrayWorkSheet = [[NSMutableArray alloc] init];
}
return self;
}
- (void)addWorkSheet:(RSworkSheet *)sheet
{
[arrayWorkSheet addObject:sheet];
}
- (BOOL)writeWithName:(NSString*)name toPath:(NSString*)path
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-ddTHH:mm:ss"];
NSString *dateString = [format stringFromDate:self.date];
NSString * head = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\"encoding=\"UTF-8\"?>\n<?mso-application progid=\"Excel.Sheet\"?>\n<Workbook xmlns:c=\"urn:schemas-microsoft-com:office:component:spreadsheet\"\n xmlns:html=\"http://www.w3.org/TR/REC-html40\"\n xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n xmlns:x2=\"http://schemas.microsoft.com/office/excel/2003/xml\"\n xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\n<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"><Author>%@</Author>\n<LastAuthor>%@</LastAuthor>\n<Created>%@</Created>\n<Version>%.2f</Version>\n</DocumentProperties>\n", self.author, self.author, dateString, self.version];
NSString * officeSetting = [[NSString alloc] initWithFormat:@"<OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">\n</OfficeDocumentSettings>\n<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n<WindowHeight>20000</WindowHeight>\n<WindowWidth>20000</WindowWidth>\n<WindowTopX>0</WindowTopX>\n<WindowTopY>0</WindowTopY>\n<ProtectStructure>False</ProtectStructure>\n<ProtectWindows>False</ProtectWindows>\n</ExcelWorkbook>"];
NSMutableString * styleDefault = [[NSMutableString alloc] initWithFormat:@"<Styles>"];
[styleDefault appendFormat:@"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n<Alignment ss:Vertical=\"%@\" ss:Horizontal=\"%@\"/>\n<Borders/>\n<Font ss:FontName=\"%@\" ss:Size=\"%.2f\" ss:Color=\"#%@\"/>\n<Interior/>\n<NumberFormat/>\n<Protection/>\n</Style>\n",
[self formatTypeToStringVertical:self.defaultStyle.alignmentV],
[self formatTypeToStringHorizontal:self.defaultStyle.alignmentH],
defaultStyle.font.fontName,
self.defaultStyle.size,
[self.defaultStyle.color hexStringFromColor]];
BOOL dateType = FALSE;
int i = 0;
for (RSworkSheet * sheet in arrayWorkSheet)
{
for (RSworkSheetRow * sheetRow in sheet.arrayWorkSheetRow)
{
dateType = FALSE;
if (![self isEqualStyle:sheetRow.style toStyle:defaultStyle])
{
[styleDefault appendFormat:@"<Style ss:ID=\"s%i\">\n<Alignment ss:Vertical=\"%@\" ss:Horizontal=\"%@\"/>\n<Borders/>\n<Font ss:FontName=\"%@\" ss:Size=\"%.2f\" ss:Color=\"#%@\"/>\n<Interior/>\n<NumberFormat/>\n<Protection/>\n</Style>\n",
60+i,
[self formatTypeToStringVertical: sheetRow.style.alignmentV],
[self formatTypeToStringHorizontal:sheetRow.style.alignmentH],
sheetRow.style.font.fontName,
sheetRow.style.size,
[sheetRow.style.color hexStringFromColor]];
i++;
}
for (RSCell * cell in sheetRow.cellArray)
{
if (![self isEqualStyle:cell.style toStyle:defaultStyle])
{
[styleDefault appendFormat:@"<Style ss:ID=\"s%i\">\n<Alignment ss:Vertical=\"%@\" ss:Horizontal=\"%@\"/>\n<Borders/>\n<Font ss:FontName=\"%@\" ss:Size=\"%.2f\" ss:Color=\"#%@\"/>\n<Interior/>\n<NumberFormat/>\n<Protection/>\n</Style>\n",
60+i,
[self formatTypeToStringVertical: sheetRow.style.alignmentV],
[self formatTypeToStringHorizontal:sheetRow.style.alignmentH],
sheetRow.style.font.fontName,
sheetRow.style.size,
[sheetRow.style.color hexStringFromColor]];
i++;
}
if ((cell.type == cellTypeDate)&&(![self isEqualStyle:sheetRow.style toStyle:defaultStyle]))
{
[styleDefault appendFormat:@"<Style ss:ID=\"s%i\">\n<Alignment ss:Vertical=\"%@\" ss:Horizontal=\"%@\"/>\n<Borders/>\n<Font ss:FontName=\"%@\" ss:Size=\"%.2f\" ss:Color=\"#%@\"/>\n<Interior/>\n<NumberFormat ss:Format=\"Short Date\" />\n<Protection/>\n</Style>\n",
60+i,
[self formatTypeToStringVertical: sheetRow.style.alignmentV],
[self formatTypeToStringHorizontal:sheetRow.style.alignmentH],
sheetRow.style.font.fontName,
sheetRow.style.size,
[sheetRow.style.color hexStringFromColor]];
dateType = TRUE;
i++;
}
else if(cell.type == cellTypeDate)
{
[styleDefault appendFormat:@"<Style ss:ID=\"s%i\">\n<NumberFormat ss:Format=\"Short Date\"/>\n</Style>\n", 60+i];
i++;
}
}
}
}
[styleDefault appendFormat:@"</Styles>\n"];
NSMutableString * sheet = [[NSMutableString alloc] init];
int j = 0;
for (RSworkSheet * sheetwork in arrayWorkSheet)
{
[sheet appendFormat:@"<Worksheet ss:Name=\"%@\">\n", sheetwork.name];
[sheet appendFormat:@"<Table ss:ExpandedColumnCount=\"%i\" ss:ExpandedRowCount=\"%i\" x:FullColumns=\"1\" x:FullRows=\"1\" ss:DefaultColumnWidth=\"%.2f\" ss:DefaultRowHeight=\"%.2f\">\n",
[self countMaxCell:sheetwork.arrayWorkSheetRow],
[sheetwork.arrayWorkSheetRow count],
sheetwork.columnWidth,
sheetwork.rowHeight];
[sheet appendFormat:@"<Column ss:Width=\"80\"/>\n"];
for (RSworkSheetRow * sheetRow in sheetwork.arrayWorkSheetRow)
{
if (![self isEqualStyle:sheetRow.style toStyle:defaultStyle])
{
[sheet appendFormat:@"<Row ss:AutoFitHeight=\"0\" ss:Height=\"%.2f\" ss:StyleID=\"s%i\">\n", sheetRow.height, 60+j];
j++;
}
else
{
[sheet appendFormat:@"<Row ss:AutoFitHeight=\"0\" ss:Height=\"%.2f\" >\n", sheetRow.height];
}
for (RSCell * cell in sheetRow.cellArray)
{
if (![self isEqualStyle:cell.style toStyle:defaultStyle])
{
[sheet appendFormat:@"<Cell ss:StyleID=\"s%i\">\n", 60+j];
j++;
}
else if (cell.type == cellTypeDate)
{
[sheet appendFormat:@"<Cell ss:StyleID=\"s%i\">\n", 60+j];
j++;
}
else
{
[sheet appendFormat:@"<Cell>\n"];
}
[sheet appendFormat:@"<Data ss:Type=\"%@\">%@</Data>\n", [self formatTypeToCellType:cell.type], cell.content];
[sheet appendFormat:@"</Cell>\n"];
}
[sheet appendFormat:@"</Row>\n"];
}
[sheet appendFormat:@"</Table>\n<WorksheetOptions/>\n</Worksheet>\n"];
}
[sheet appendFormat:@"</Workbook>\n"];
NSMutableString * finalText = [[NSMutableString alloc] init];
[finalText appendFormat:@"%@",head];
[finalText appendFormat:@"%@",officeSetting];
[finalText appendFormat:@"%@",styleDefault];
[finalText appendFormat:@"%@",sheet];
NSLog(@"%@", finalText);
NSString *pathFinal = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xls",name]];
NSError *err;
if([finalText writeToFile:pathFinal atomically:YES encoding:NSUTF8StringEncoding error:&err])
{
NSLog(@"salvataggio corretto");
NSLog(@"path:%@", pathFinal);
return YES;
}
else
{
NSLog(@"error:%@", err);
return NO;
}
}
-(BOOL)isEqualStyle:(RSStyle*)style1 toStyle:(RSStyle*)style2
{
if (![style1.font isEqual:style2.font])
return FALSE;
if (![style1.color isEqual:style2.color])
return FALSE;
if (style1.size != style2.size)
return FALSE;
if (style1.alignmentH != style2.alignmentH)
return FALSE;
if (style1.alignmentV != style2.alignmentV)
return FALSE;
return TRUE;
}
- (int)countMaxCell:(NSMutableArray*)sheet
{
int max=0;
for (RSworkSheetRow * row in sheet)
{
if ([row.cellArray count] > max)
{
max = [row.cellArray count];
}
}
return max;
}
- (NSString*)formatTypeToStringVertical:(verticalAlign)formatType {
NSString *result = nil;
switch(formatType) {
case RSStyleTopAlign:
result = @"Top";
break;
case RSStyleCenterAlign:
result = @"Center";
break;
case RSStyleBottomAlign:
result = @"Bottom";
break;
default:
[NSException raise:NSGenericException format:@"Unexpected FormatType."];
}
return result;
}
- (NSString*)formatTypeToStringHorizontal:(horizontalAlign)formatType {
NSString *result = nil;
switch(formatType) {
case RSStyleLeftAlign:
result = @"Left";
break;
case RSStyleMiddleAlign:
result = @"Center";
break;
case RSStyleRightAlign:
result = @"Right";
break;
default:
[NSException raise:NSGenericException format:@"Unexpected FormatType."];
}
return result;
}
- (NSString*)formatTypeToCellType:(cellType)formatType
{
NSString *result = nil;
switch(formatType) {
case cellTypeNumber:
result = @"Number";
break;
case cellTypeString:
result = @"String";
break;
case cellTypeDate:
result = @"DateTime";
break;
default:
[NSException raise:NSGenericException format:@"Unexpected FormatType."];
}
return result;
}
@end