Commit 31569226 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

Improved Excel source

fix row handler
parent ff6e4078
/** /**
* Copyright 2014 Global Crop Diversity Trust * Copyright 2018 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
package org.genesys2.anno.reader; package org.genesys2.anno.reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
...@@ -134,15 +137,15 @@ public class MyXSSFSheetHandler extends DefaultHandler { ...@@ -134,15 +137,15 @@ public class MyXSSFSheetHandler extends DefaultHandler {
// c => cell // c => cell
else if ("c".equals(name)) { else if ("c".equals(name)) {
// Get the cell reference // Get the cell reference
String r = attributes.getValue("r"); final String newColRef = attributes.getValue("r");
int firstDigit = -1; int firstDigit = -1;
for (int c = 0; c < r.length(); ++c) { for (int c = 0; c < newColRef.length(); ++c) {
if (Character.isDigit(r.charAt(c))) { if (Character.isDigit(newColRef.charAt(c))) {
firstDigit = c; firstDigit = c;
break; break;
} }
} }
thisColumn = nameToColumn(r.substring(0, firstDigit)); thisColumn = nameToColumn(newColRef.substring(0, firstDigit));
// Set up defaults. // Set up defaults.
this.nextDataType = xssfDataType.NUMBER; this.nextDataType = xssfDataType.NUMBER;
...@@ -225,8 +228,11 @@ public class MyXSSFSheetHandler extends DefaultHandler { ...@@ -225,8 +228,11 @@ public class MyXSSFSheetHandler extends DefaultHandler {
break; break;
case NUMBER: case NUMBER:
String n = value.toString(); final String n = value.toString();
if (n.contains(".")) if (DateUtil.isADateFormat(this.formatIndex, this.formatString)) {
final Date date = DateUtil.getJavaDate(Double.valueOf(n));
thisObj = new SimpleDateFormat("yyyyMMdd").format(date);
} else if (n.contains("."))
thisObj = Double.parseDouble(n); thisObj = Double.parseDouble(n);
else else
thisObj = Long.parseLong(n); thisObj = Long.parseLong(n);
...@@ -247,9 +253,14 @@ public class MyXSSFSheetHandler extends DefaultHandler { ...@@ -247,9 +253,14 @@ public class MyXSSFSheetHandler extends DefaultHandler {
// Emit commas for any fields that were missing on this row // Emit commas for any fields that were missing on this row
if (lastColumnNumber == -1) { if (lastColumnNumber == -1) {
lastColumnNumber = 0; lastColumnNumber = 0;
for (int i = lastColumnNumber; i < thisColumn; ++i) {
row.add("");
}
} else {
for (int i = lastColumnNumber; i < thisColumn - 1; ++i) {
row.add("");
}
} }
for (int i = lastColumnNumber; i < thisColumn - 1; ++i)
row.add(null);
// Might be the empty string. // Might be the empty string.
row.add(thisObj); row.add(thisObj);
...@@ -314,5 +325,4 @@ public class MyXSSFSheetHandler extends DefaultHandler { ...@@ -314,5 +325,4 @@ public class MyXSSFSheetHandler extends DefaultHandler {
} }
return column; return column;
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment