23
23
*/
24
24
package pattypan .panes ;
25
25
26
- import freemarker .core .InvalidReferenceException ;
27
26
import freemarker .template .Configuration ;
28
27
import freemarker .template .Template ;
29
28
import freemarker .template .TemplateException ;
30
29
import freemarker .template .TemplateExceptionHandler ;
30
+ import java .io .BufferedInputStream ;
31
31
import java .io .File ;
32
+ import java .io .FileInputStream ;
32
33
import java .io .IOException ;
34
+ import java .io .InputStream ;
33
35
import java .io .StringReader ;
34
36
import java .io .StringWriter ;
35
37
import java .text .SimpleDateFormat ;
36
38
import java .util .ArrayList ;
39
+ import java .util .Date ;
37
40
import java .util .HashMap ;
41
+ import java .util .List ;
38
42
import java .util .Map ;
39
43
import java .util .Set ;
40
44
import java .util .TimeZone ;
43
47
import javafx .scene .layout .VBox ;
44
48
import javafx .stage .FileChooser ;
45
49
import javafx .stage .Stage ;
46
- import jxl .Cell ;
47
- import jxl .CellType ;
48
- import jxl .DateCell ;
49
- import jxl .Sheet ;
50
- import jxl .Workbook ;
51
- import jxl .WorkbookSettings ;
52
- import jxl .read .biff .BiffException ;
50
+ import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
51
+ import org .apache .poi .poifs .filesystem .FileMagic ;
52
+ import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
53
+ import org .apache .poi .ss .usermodel .Cell ;
54
+ import org .apache .poi .ss .usermodel .CellType ;
55
+ import org .apache .poi .ss .usermodel .DateUtil ;
56
+ import org .apache .poi .ss .usermodel .Row ;
57
+ import org .apache .poi .ss .usermodel .Sheet ;
58
+ import org .apache .poi .ss .usermodel .Workbook ;
59
+ import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
60
+
53
61
import pattypan .Session ;
54
62
import pattypan .Settings ;
55
63
import pattypan .UploadElement ;
@@ -236,10 +244,9 @@ private void addInfo(String text, String cssClass) {
236
244
* @throws Exception when essential headers are missing
237
245
*/
238
246
private void readHeaders (Sheet sheet ) throws Exception {
239
- int columns = sheet .getColumns ();
240
- ArrayList <String > cols = new ArrayList <>();
241
- for (int col = 0 ; col < columns ; col ++) {
242
- cols .add (sheet .getCell (col , 0 ).getContents ());
247
+ List <String > cols = new ArrayList <>();
248
+ for (Cell c : sheet .getRow (0 )) {
249
+ cols .add (c .getStringCellValue ());
243
250
}
244
251
245
252
if (cols .isEmpty ()) {
@@ -264,18 +271,22 @@ private String getCellValue(Sheet sheet, int column, int row) {
264
271
formatDate .setTimeZone (TimeZone .getTimeZone ("UTC" ));
265
272
formatDateHour .setTimeZone (TimeZone .getTimeZone ("UTC" ));
266
273
267
- Cell valueCell = sheet .getCell (column , row );
268
- String value ;
274
+ Cell valueCell = sheet .getRow ( row ). getCell (column );
275
+ String value = null ;
269
276
270
- if (valueCell .getType () == CellType .DATE ) {
271
- DateCell dateCell = (DateCell ) valueCell ;
277
+ if (valueCell != null ) {
278
+ if (valueCell .getCellType () == CellType .NUMERIC && DateUtil .isCellDateFormatted (valueCell )) {
279
+ Date date = DateUtil .getJavaDate (valueCell .getNumericCellValue ());
280
+ // FIXME: Restore more sophisticated date handling
281
+ value = formatDate .format (date );
272
282
//@TODO: more elegant hour detection
273
- value = dateCell .getContents ().contains (":" )
274
- ? formatDateHour .format (dateCell .getDate ())
275
- : formatDate .format (dateCell .getDate ());
276
- } else {
277
- value = sheet .getCell (column , row ).getContents ().trim ();
278
- }
283
+ // value = dateCell.getContents().contains(":")
284
+ // ? formatDateHour.format(dateCell.getDate())
285
+ // : formatDate.format(dateCell.getDate());
286
+ } else {
287
+ value = valueCell .getStringCellValue ().trim ();
288
+ }
289
+ }
279
290
return value ;
280
291
}
281
292
@@ -294,9 +305,7 @@ private void loadSpreadsheet(File file) {
294
305
readSpreadSheet ();
295
306
} catch (IOException ex ) {
296
307
addInfo ("File error: there are problems opening file. It may be corrupted." );
297
- } catch (BiffException ex ) {
298
- addInfo ("File error: file needs to be saved in binnary format. Please save your file in \" Excel 97-2003 format\" " );
299
- } catch (InvalidReferenceException ex ) {
308
+ } catch (TemplateException ex ) {
300
309
addInfo ("File error: variables mismatch. Column headers variables must match wikitemplate variables." );
301
310
} catch (Exception ex ) {
302
311
addInfo (ex .getMessage ());
@@ -312,14 +321,27 @@ private void loadSpreadsheet(File file) {
312
321
*/
313
322
private ArrayList <Map <String , String >> readDescriptions (Sheet sheet ) {
314
323
ArrayList <Map <String , String >> descriptions = new ArrayList <>();
315
- int rows = sheet .getRows ();
316
- int columns = sheet .getColumns ();
324
+ int rows = sheet .getLastRowNum ();
325
+ int columns = sheet .getRow ( 0 ). getLastCellNum ();
317
326
327
+ // Collect header labels
328
+ String [] labels = new String [columns ];
329
+ Row cells = sheet .getRow (0 );
330
+ for (int col = 0 ; col < columns ; col ++) {
331
+ Cell cell = cells .getCell (col );
332
+ if (cell != null ) {
333
+ String value = cell .getStringCellValue ();
334
+ labels [col ] = value ;
335
+ } else {
336
+ labels [col ] = null ;
337
+ }
338
+ }
318
339
for (int row = 1 ; row < rows ; row ++) {
319
340
Map <String , String > description = new HashMap ();
341
+ cells = sheet .getRow (row );
320
342
for (int column = 0 ; column < columns ; column ++) {
321
- String label = sheet . getCell ( column , 0 ). getContents (). trim () ;
322
- if (label .isEmpty ()) {
343
+ String label = labels [ column ] ;
344
+ if (label == null || label .isEmpty ()) {
323
345
continue ;
324
346
}
325
347
String value = getCellValue (sheet , column , row );
@@ -333,17 +355,19 @@ private ArrayList<Map<String, String>> readDescriptions(Sheet sheet) {
333
355
/**
334
356
* Reads spreadsheet stored in Session.FILE.
335
357
*/
336
- private void readSpreadSheet () throws BiffException , IOException , Exception {
358
+ private void readSpreadSheet () throws IOException , TemplateException , Exception {
337
359
infoContainer .getChildren ().clear ();
338
360
Session .SCENES .remove ("CheckPane" );
339
361
340
- WorkbookSettings ws = new WorkbookSettings ();
341
- ws .setEncoding ("Cp1252" );
362
+ InputStream inputStream = new BufferedInputStream (new FileInputStream (Session .FILE ));
363
+ Workbook wb = FileMagic .valueOf (inputStream ) == FileMagic .OOXML ? new XSSFWorkbook (inputStream )
364
+ : new HSSFWorkbook (new POIFSFileSystem (inputStream ));
365
+
366
+ // ws.setEncoding("Cp1252"); // FIXME
342
367
343
368
try {
344
- Workbook workbook = Workbook .getWorkbook (Session .FILE , ws );
345
- Sheet dataSheet = workbook .getSheet (0 );
346
- Sheet templateSheet = workbook .getSheet (1 );
369
+ Sheet dataSheet = wb .getSheetAt (0 );
370
+ Sheet templateSheet = wb .getSheetAt (1 );
347
371
readHeaders (dataSheet );
348
372
addFilesToUpload (readDescriptions (dataSheet ), readTemplate (templateSheet ));
349
373
} catch (IndexOutOfBoundsException ex ) {
@@ -362,7 +386,7 @@ private void readSpreadSheet() throws BiffException, IOException, Exception {
362
386
*/
363
387
private Template readTemplate (Sheet sheet ) throws Exception {
364
388
try {
365
- String text = sheet .getCell (0 , 0 ). getContents ();
389
+ String text = sheet .getRow ( 0 ). getCell (0 ). getStringCellValue ();
366
390
return new Template ("wikitemplate" , new StringReader (text ), cfg );
367
391
} catch (ArrayIndexOutOfBoundsException ex ) {
368
392
throw new Exception ("Error: template in spreadsheet looks empty. Check if wikitemplate is present in second tab of your spreadsheet (first row and first column)." );
0 commit comments