Skip to content

Commit

Permalink
Merge pull request #3 from Ryan-fai/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
noahzark committed Jan 2, 2020
2 parents a9f30b0 + 938d716 commit f2d66f7
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ gradle-app.setting
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
/.idea/
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
Expand Down
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

26 changes: 0 additions & 26 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/kotlinc.xml

This file was deleted.

22 changes: 0 additions & 22 deletions .idea/misc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/modules/xlsx2json.iml

This file was deleted.

22 changes: 0 additions & 22 deletions .idea/modules/xlsx2json_main.iml

This file was deleted.

26 changes: 0 additions & 26 deletions .idea/modules/xlsx2json_test.iml

This file was deleted.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ You can use "Basic" to let the parser automatically detect types

Support all strings with format **HH:mm:ss** (directly out) or cell format **Time** (converted to a calander object and format with simple date format)

> **Date** and **DateTIme** support would be added in future versions, add an issue if you need it.
### Date Type
Support string or numeric with format **yyyyMMdd** and it will format as **yyyy-MM-dd** in the json.

> **DateTIme** support would be added in future versions, add an issue if you need it.
### Array Types

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ repositories {

dependencies {
// http://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.9'
compile group: 'org.apache.poi', name: 'poi', version: '4.1.0'

// http://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.14'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.0'

// http://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20160212'
Expand Down
82 changes: 60 additions & 22 deletions src/main/java/moe/imvery/utils/xlsx2json/ExcelParser.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package moe.imvery.utils.xlsx2json;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

/**
* Created by Feliciano on 6/1/2016.
*/
Expand Down Expand Up @@ -88,7 +84,7 @@ public static Row findRowByColumn( ParsedSheet sheet, String key, String value)
if (cell == null)
continue;

if (cell.getCellType() == CELL_TYPE_BLANK)
if (cell.getCellType() == CellType.BLANK)
continue;

String cellValue = getCellStringValue(cell);
Expand Down Expand Up @@ -138,27 +134,28 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {
switch (type) {
case BASIC:
// Handle "Null" string
if (cell != null && cell.getCellType() == CELL_TYPE_STRING) {
if (cell != null && cell.getCellType() == CellType.STRING) {
if (cell.getStringCellValue().equalsIgnoreCase("null")) {
jsonRow.put(key, JSONObject.NULL);
continue;
}
}
case DATE:
case TIME:
if (cell != null && cell.getCellType() == CELL_TYPE_STRING) {
if (cell != null && cell.getCellType() == CellType.STRING) {
if (cell.getStringCellValue().equalsIgnoreCase("null")) {
jsonRow.put(key, JSONObject.NULL);
continue;
}
}
case OBJECT:
if (cell == null || cell.getCellType() == CELL_TYPE_BLANK) {
if (cell == null || cell.getCellType() == CellType.BLANK) {
jsonRow.put(key, JSONObject.NULL);
continue;
}
break;
case REFERENCE:
if (cell == null || cell.getCellType() == CELL_TYPE_BLANK) {
if (cell == null || cell.getCellType() == CellType.BLANK) {
jsonRow.put(key.substring(0, key.indexOf("@")), JSONObject.NULL);
continue;
}
Expand All @@ -167,7 +164,7 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {
case ARRAY_STRING:
case ARRAY_BOOLEAN:
case ARRAY_DOUBLE:
if (cell == null || cell.getCellType() == CELL_TYPE_BLANK) {
if (cell == null || cell.getCellType() == CellType.BLANK) {
jsonRow.put(key, new ArrayList());
continue;
}
Expand All @@ -185,10 +182,10 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {
case ARRAY_DOUBLE:
{
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
case BOOLEAN:
stringCellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
case NUMERIC:
stringCellValue = String.valueOf(cell.getNumericCellValue());
break;
default:
Expand All @@ -203,10 +200,10 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {
case BASIC:
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
case NUMERIC:
jsonRow.put( key, cell.getNumericCellValue() );
break;
case Cell.CELL_TYPE_BOOLEAN:
case BOOLEAN:
jsonRow.put( key, cell.getBooleanCellValue() );
break;
default:
Expand All @@ -215,15 +212,44 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {
};
break;

case DATE:
if(cell.getCellType() == CellType.NUMERIC){
BigDecimal data = new BigDecimal(cell.getNumericCellValue()+"");
String dateStr = data+"";
Date date = null;
if ((date=isValidDate(dateStr)) !=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateStr = sdf.format(date);
jsonRow.put(key, dateStr);
} else{
jsonRow.put(key, "1990-01-01");
}
} else if(cell.getCellType() == CellType.STRING){
String dateStr = cell.getStringCellValue();
Date date = null;
if ((date=isValidDate(dateStr)) !=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateStr = sdf.format(date);
jsonRow.put(key, dateStr);
} else{
jsonRow.put(key, "1990-01-01");
}
} else {
throw new IllegalArgumentException("Unhandled cell of " + cell.getCellType()+ " type at "
+ "row " + row.getRowNum()
+ "column " + index);
}
break;

case TIME:
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (cell.getCellType() == CellType.NUMERIC) {
Date time = cell.getDateCellValue();
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String timeStr = sdf.format(calendar.getTime());
jsonRow.put( key, timeStr );
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING){
} else if (cell.getCellType() == CellType.STRING){
jsonRow.put( key, cell.getStringCellValue() );
} else {
throw new IllegalArgumentException("Unhandled cell of " + cell.getCellType()+ " type at "
Expand Down Expand Up @@ -284,13 +310,13 @@ public static JSONObject parseRow(Row row, ParsedSheet parsedSheet) {

private static String getCellStringValue(Cell cell) {
switch (cell.getCellType()) {
case CELL_TYPE_BLANK:
case BLANK:
break;

case Cell.CELL_TYPE_NUMERIC:
case NUMERIC:
return cell.getNumericCellValue() + "";

case Cell.CELL_TYPE_BOOLEAN:
case BOOLEAN:
return cell.getBooleanCellValue() + "";

default:
Expand All @@ -300,6 +326,18 @@ private static String getCellStringValue(Cell cell) {
return null;
}

private static Date isValidDate(String dateStr){
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
try{
format.setLenient(false);
date = format.parse(dateStr);
}catch (ParseException e){
return null;
}
return date;
}

/**
* Parse a cell of the row
* @param type The data type
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/moe/imvery/utils/xlsx2json/ParsedCellType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum ParsedCellType {
* Time type format: hh:mm:ss
*/
TIME("Time"),
/**
* Date type format: yyyyMMdd
*/
DATE("Date"),
/**
* String array
*/
Expand Down
Binary file modified testcases/test.xlsx
Binary file not shown.

0 comments on commit f2d66f7

Please sign in to comment.