-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCY2_SPREADSHEET:StreamingWorkbook.pcode
560 lines (465 loc) · 20.2 KB
/
CY2_SPREADSHEET:StreamingWorkbook.pcode
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
<*
MIT License
Copyright (c) 2018-2023
CY2 IT Services - https://cy2.nl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*>
class StreamingWorkbook
/**
* Workbook constructor
*
* @param p_file The full path and file name of the Excel file to open or create.
* @exception An exception thrown if the file path does not exist or cannot be created.
*/
method StreamingWorkbook(&p_filePath As string);
/**
* Creates a worksheet in the workbook with the given name and makes it the
* active sheet. A sheet with that name already exists, it becomes active.
* Sheets are modified to meet Excel sheet name requirements.
*
* @param name The name of the worksheet to create
*/
rem method CreateSheet(&name As string);
/**
* Saves the Excel file and creates the file if it doesn't exist.
*/
method Save();
/**
* Set the value of the given cell to the given string value. If the row or
* column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param string The string value to set the cell to.
*/
method SetCellString(&row As integer, &column As integer, &string As string);
/**
* Set the value of the given cell to the given numeric value. If the row or
* column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param value The number to set the cell to.
*/
method SetCellNumber(&row As integer, &column As integer, &value As number);
/**
* Set the value of the given cell to the given numeric value with the given Excel format.
* If the row or column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param number The number to set the cell to.
* @param format The Excel format to apply to the number.
*/
method SetCellNumberFormat(&row As integer, &column As integer, &value As number, &format As string);
/**
* Set the value of the given cell to the given date value. The DateTime is formatted as
* dd/mm/yyyy hh:mm:ss .If the row or column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param dateTime The DateTime value to set the cell to.
*/
method SetCellDateTime(&row As integer, &column As integer, &dateTime As datetime);
/**
* Set the value of the given cell to the given date value. The Date is formatted as
* dd/mm/yyyy .If the row or column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the ceanll to set. It must be a positive integer.
* @param dateTime The Date value to set the cell to
*/
method SetCellDate(&row As integer, &column As integer, &date As date);
/**
* Set the value of the given cell to the given Boolean value.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param dateTime The Boolean value to set the cell to
*/
method SetCellBoolean(&row As integer, &column As integer, &boolean As boolean);
rem method SetCellDateTimeFormated(&row As integer, &column As integer, &date As datetime, &locale As string, &timezone As string, &format As string);
rem method SetCellDateFormated(&row As integer, &column As integer, &date As datetime, &locale As string, &timezone As string, &format As string);
/**
* Set the value of the given cell to the given formula value. If the row or
* column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param formula The formula
*/
method SetCellFormula(&row As integer, &column As integer, &formula As string);
/**
* Set the value of the given cell to the given hyperlink. If the row or
* column number is not positive, nothing is done.
*
* @param row The row number of the cell to set. It must be a positive integer.
* @param column The column number of the cell to set. It must be a positive integer.
* @param text The text value of the cell.
* @param column The URL the link points to.
*/
method SetCellHyperlink(&row As integer, &column As integer, &text As string, &url As string);
/** Close the workbook and clean up and temp resources */
method Close();
/* Default Formats */
/* Defaults to "dd/MM/yyyy HH:mm:ss" */
property string DateTimeFormat;
/* Adjust date's day value in accordance to local timezone */
/* Defaults to False for backward compatibility */
property boolean LocalTimezoneAdjustment;
private
Constant &NumericCell = "NUMERIC";
Constant &StringCell = "STRING";
Constant &FormulaCell = "FORMULA";
Constant &BlankCell = "BLANK";
Constant &NoneCell = "_NONE";
Constant &BooleanCell = "BOOLEAN";
Constant &ErrorCell = "ERROR";
method GetCell(&row As integer, &column As integer);
method GetRow(&index As integer);
method CreateRow(&index As integer);
method CreateSheetCell(&sheetRow As JavaObject, &index As integer);
method CreateCellStyle() Returns JavaObject;
method GetCellStyle(&cell As JavaObject) Returns JavaObject;
method CreateDataFormat() Returns JavaObject;
method CreateFont() Returns JavaObject;
method CreateHyperlink(&type As JavaObject) Returns JavaObject;
method SetDataFormat(&cellStyle As JavaObject, &dataFormat As integer);
method CreateWorkbook(&inputSream As JavaObject) Returns JavaObject;
/**
* Adjust date day value in accordance to local timezone
* Apache POI uses the server timezone to transform date values to string values
* method adjust the day value in a date in case its off by 1 day due to server timezone issues
*
* @param &dttm the time that should be adjusted against local timezone*/
method ApplyLocalTimezoneAdjustment(&dttm As datetime) Returns datetime;
instance JavaObject &_workbook;
instance JavaObject &_workBookUtils;
instance JavaObject &_activeSheet;
instance JavaObject &_creationHelper;
instance JavaObject &_fontClass;
instance JavaObject &_indexedColors;
instance JavaObject &_dateUtil;
instance JavaObject &_createSheetMethod;
instance JavaObject &_getSheetMethod;
instance JavaObject &_getSheetAtMethod;
instance JavaObject &_getRowMethod;
instance JavaObject &_createRowMethod;
instance JavaObject &_getCellMethod;
instance JavaObject &_createCellStyleMethod;
instance JavaObject &_createCellMethod;
instance JavaObject &_getCellStyleMethod;
instance JavaObject &_setDataFormatMethod;
instance JavaObject &_createFontMethod;
instance JavaObject &_createHyperlinkMethod;
instance JavaObject &_dataFormat;
instance JavaObject &_workbookClass;
instance string &_filePath;
instance JavaObject &_dateStyle;
instance JavaObject &_dateTimeStyle;
instance JavaObject &_cellStyle;
instance JavaObject &_currentCell;
instance JavaObject &_currentRow;
end-class;
method StreamingWorkbook
/+ &p_filePath as String +/
&_filePath = &p_filePath;
&_workbook = CreateJavaObject("org.apache.poi.xssf.streaming.SXSSFWorkbook", 100);
&_workbook.setCompressTempFiles( True);
&_workbookClass = &_workbook.getClass();
Local JavaObject &javaString = CreateJavaObject("java.lang.String", "Sheet1");
Local JavaObject &stringType = CreateJavaObject("java.lang.Class[]", &javaString.getClass());
Local JavaObject &createSheet = &_workbookClass.getDeclaredMethod("createSheet", &stringType);
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &javaString);
&_activeSheet = &createSheet.invoke(&_workbook, &args);
&_workBookUtils = GetJavaClass("org.apache.poi.ss.util.WorkbookUtil");
Local JavaObject &getCreationHelper = &_workbookClass.getDeclaredMethod("getCreationHelper", CreateJavaObject("java.lang.Class[]"));
&_creationHelper = &getCreationHelper.invoke(&_workbook, CreateJavaObject("java.lang.Object[]"));
&_creationHelper = &_creationHelper.getClass().cast(&_creationHelper);
&_dataFormat = %This.CreateDataFormat();
&_fontClass = GetJavaClass("org.apache.poi.ss.usermodel.Font");
&_indexedColors = GetJavaClass("org.apache.poi.ss.usermodel.IndexedColors");
&_dateUtil = GetJavaClass("org.apache.poi.ss.usermodel.DateUtil");
&DateTimeFormat = "dd/MM/yyyy HH:mm:ss";
&LocalTimezoneAdjustment = False;
&_cellStyle = %This.CreateCellStyle();
end-method;
method Save
Local JavaObject &outStream = CreateJavaObject("java.io.FileOutputStream", &_filePath);
&_workbook.write(&outStream);
&outStream.close();
end-method;
method SetCellString
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &string as String +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
&_currentCell.setCellValue(&string);
end-method;
method SetCellDate
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &date as Date +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
Local datetime &dateTime = DateTime6(Year(&date), Month(&date), Day(&date), 0, 0, 0);
&_currentCell.setCellValue(&dateTime);
If &_dateStyle = Null Then
&_dateStyle = %This.CreateCellStyle();
%This.SetDataFormat(&_dateStyle, &_dataFormat.getFormat("dd/mm/yyyy"));
End-If;
&_currentCell.setCellStyle(&_dateStyle);
end-method;
method SetCellDateTime
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &dateTime as DateTime +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
&_currentCell.setCellValue(&dateTime);
If &_dateTimeStyle = Null Then
&_dateTimeStyle = %This.CreateCellStyle();
%This.SetDataFormat(&_dateTimeStyle, &_dataFormat.getFormat("dd/mm/yyyy hh:mm:ss"));
End-If;
&_currentCell.setCellStyle(&_dateTimeStyle);
end-method;
method SetCellFormula
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &formula as String +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
Local string &first = Substring(&formula, 1, 1);
/* formulas cannot start with an equal sign */
If &first = "=" Then
&formula = Substring(&formula, 2, Len(&formula) - 1);
End-If;
&_currentCell.setCellFormula(&formula);
end-method;
method SetCellNumber
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &value as Number +/
%This.SetCellNumberFormat(&row, &column, &value, "");
end-method;
method SetCellNumberFormat
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &value as Number, +/
/+ &format as String +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
&_currentCell.setCellValue(Float(&value));
If &format <> "" Then
%This.SetDataFormat(&_cellStyle, &_dataFormat.getFormat(&format));
&_currentCell.setCellStyle(&_cellStyle);
End-If;
end-method;
method SetCellHyperlink
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &text as String, +/
/+ &url as String +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
&_currentCell.setCellValue(&text);
Local JavaObject &style = %This.CreateCellStyle();
Local JavaObject &font = %This.CreateFont();
&font.setUnderline(&_fontClass.U_SINGLE);
&font.setColor(&_indexedColors.BLUE.getIndex());
&style.setFont(&font);
/* depending on the tools version, the version of createHyperlink is different (differnt POI version)
need to figure out a better way here
*/
Local JavaObject &link;
try
&link = &_creationHelper.createHyperlink(1);
catch Exception &exp
Local JavaObject &hyperlinkClass = GetJavaClass("org.apache.poi.common.usermodel.HyperlinkType");
&link = %This.CreateHyperlink(&hyperlinkClass.URL);
end-try;
&link.setAddress(&url);
&_currentCell.setHyperlink(&link);
&_currentCell.setCellStyle(&style);
end-method;
method SetCellBoolean
/+ &row as Integer, +/
/+ &column as Integer, +/
/+ &boolean as Boolean +/
%This.GetCell(&row, &column);
If &_currentCell = Null Then
Return;
End-If;
&_currentCell.setCellValue(&boolean);
end-method;
method GetRow
/+ &index as Integer +/
If &_getRowMethod = Null Then
Local JavaObject &javaInteger = CreateJavaObject("java.lang.Integer", 1);
Local JavaObject &intType = CreateJavaObject("java.lang.Class[]", &javaInteger.TYPE);
&_getRowMethod = &_activeSheet.getClass().getDeclaredMethod("getRow", &intType);
End-If;
&_currentRow = Null;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &index);
Local JavaObject &sheetRow = &_getRowMethod.invoke(&_activeSheet, &args);
If &sheetRow <> Null Then
&_currentRow = &sheetRow.getClass().cast(&sheetRow);
&sheetRow = Null;
End-If;
end-method;
method GetCell
/+ &row as Integer, +/
/+ &column as Integer +/
&_currentCell = Null;
If &row < 1 Or
&column < 1 Or
&column > 1048576 Then
End-If;
%This.GetRow(&row - 1);
If &_currentRow = Null Then
%This.CreateRow(&row - 1);
End-If;
%This.CreateSheetCell(&_currentRow, &column - 1);
end-method;
method Close
&_workbook.close()
end-method;
method ApplyLocalTimezoneAdjustment
/+ &dttm as DateTime +/
/+ Returns DateTime +/
Local boolean &before;
Local datetime &newDttm = &dttm;
/* Apply the adjustment only if desred */
If %This.LocalTimezoneAdjustment Then
/* The date is the previous day, the timezone offset is negative */
&before = (Substring(TimeZoneOffset(&dttm), 1, 1) = "-");
If &before Then
/* We are the previous day so add one, trimming off any hours, minutes and seconds info */
&newDttm = AddToDateTime(DateTime6(Year(&dttm), Month(&dttm), Day(&dttm), 0, 0, 0), 0, 0, 1, 0, 0, 0);
End-If;
End-If;
Return &newDttm;
end-method;
method CreateRow
/+ &index as Integer +/
If &_createRowMethod = Null Then
Local JavaObject &javaInteger = CreateJavaObject("java.lang.Integer", 1);
Local JavaObject &intType = CreateJavaObject("java.lang.Class[]", &javaInteger.TYPE);
&_createRowMethod = &_activeSheet.getClass().getDeclaredMethod("createRow", &intType);
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &index);
Local JavaObject &sheetRow = &_createRowMethod.invoke(&_activeSheet, &args);
If &sheetRow <> Null Then
&_currentRow = &sheetRow.getClass().cast(&sheetRow);
&sheetRow = Null;
End-If;
end-method;
method CreateSheetCell
/+ &sheetRow as JavaObject, +/
/+ &index as Integer +/
If &_createCellMethod = Null Then
Local JavaObject &javaInteger = CreateJavaObject("java.lang.Integer", 1);
Local JavaObject &intType = CreateJavaObject("java.lang.Class[]", &javaInteger.TYPE);
&_createCellMethod = &sheetRow.getClass().getDeclaredMethod("createCell", &intType);
End-If;
&_currentCell = Null;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &index);
Local JavaObject &cell = &_createCellMethod.invoke(&sheetRow, &args);
If &cell <> Null Then
&_currentCell = &cell.getClass().cast(&cell);
&cell = Null;
End-If;
end-method;
method CreateCellStyle
/+ Returns JavaObject +/
If &_createCellStyleMethod = Null Then
&_createCellStyleMethod = &_workbookClass.getDeclaredMethod("createCellStyle", CreateJavaObject("java.lang.Class[]"));
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]");
Local JavaObject &style = &_createCellStyleMethod.invoke(&_workbook, &args);
Return &style.getClass().cast(&style);
end-method;
method CreateFont
/+ Returns JavaObject +/
If &_createFontMethod = Null Then
&_createFontMethod = &_workbookClass.getDeclaredMethod("createFont", CreateJavaObject("java.lang.Class[]"));
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]");
Local JavaObject &font = &_createFontMethod.invoke(&_workbook, &args);
Return &font.getClass().cast(&font);
end-method;
method GetCellStyle
/+ &cell as JavaObject +/
/+ Returns JavaObject +/
If &_getCellStyleMethod = Null Then
&_getCellStyleMethod = &cell.getClass().getDeclaredMethod("getCellStyle", CreateJavaObject("java.lang.Class[]"));
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]");
Local JavaObject &style = &_getCellStyleMethod.invoke(&cell, &args);
Return &style.getClass().cast(&style);
end-method;
method CreateDataFormat
/+ Returns JavaObject +/
Local JavaObject &method = &_creationHelper.getClass().getDeclaredMethod("createDataFormat", CreateJavaObject("java.lang.Class[]"));
Local JavaObject &args = CreateJavaObject("java.lang.Object[]");
Local JavaObject &dataFormat = &method.invoke(&_creationHelper, &args);
Return &dataFormat.getClass().cast(&dataFormat);
end-method;
method SetDataFormat
/+ &cellStyle as JavaObject, +/
/+ &dataFormat as Integer +/
Local JavaObject &javaShort = CreateJavaObject("java.lang.Short", &dataFormat);
Local JavaObject &shortType = CreateJavaObject("java.lang.Class[]", &javaShort.TYPE);
If &_setDataFormatMethod = Null Then
&_setDataFormatMethod = &cellStyle.getClass().getDeclaredMethod("setDataFormat", &shortType);
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &javaShort);
&_setDataFormatMethod.invoke(&cellStyle, &args);
end-method;
method CreateHyperlink
/+ &type as JavaObject +/
/+ Returns JavaObject +/
If &_createHyperlinkMethod = Null Then
&_createHyperlinkMethod = &_creationHelper.getClass().getDeclaredMethod("createHyperlink", CreateJavaObject("java.lang.Class[]", &type.getClass()));
End-If;
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &type);
Local JavaObject &hyperlink = &_createHyperlinkMethod.invoke(&_creationHelper, &args);
Return &hyperlink.getClass().cast(&hyperlink);
end-method;
method CreateWorkbook
/+ &inputSream as JavaObject +/
/+ Returns JavaObject +/
Local JavaObject &inputStreamClass = GetJavaClass("java.lang.Class").forName("java.io.InputStream");
Local JavaObject &workbookFactory = CreateJavaObject("org.apache.poi.xssf.usermodel.XSSFWorkbookFactory");
Local JavaObject &createMethod = &workbookFactory.getClass().getDeclaredMethod("create", CreateJavaObject("java.lang.Class[]", &inputStreamClass));
Local JavaObject &args = CreateJavaObject("java.lang.Object[]", &inputSream);
Local JavaObject &workBook = &createMethod.invoke(&workbookFactory, &args);
Return &workBook.getClass().cast(&workBook);
end-method;