diff --git a/.gitignore b/.gitignore index a09e577..bee5de7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build .gradle .idea .settings -excel-parser.iml \ No newline at end of file +excel-parser.iml +*.iml diff --git a/README.md b/README.md index 5d677ac..1c0b78e 100755 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ First we will see the steps to annotate Section object: @ExcelField(position = 3) private String section; - + @MappedExcelObject private List students; } @@ -41,26 +41,26 @@ Then, annotate the Student class: public class Student { @ExcelField(position = 2) private Long roleNumber; - + @ExcelField(position = 3) private String name; - + @ExcelField(position = 4) private Date dateOfBirth; - + @ExcelField(position = 5) private String fatherName; - + @ExcelField(position = 6) private String motherName; - + @ExcelField(position = 7) private String address; - + @ExcelField(position = 8) private Double totalScore; } - +* `end`: If optional field "end" set to 0 or not set, then it will auto find end by default. * `ExcelObject`: As shown above, this annotation tells parser to parse Rows 6 to 8 (create 3 student objects). NOTE: Optional field “zeroIfNull” , if set to true, will populate Zero to all number fields (Double,Long,Integer) by default if the data is not available in DB. * `ExcelField`: Student class has 7 values to be parsed and stored in the database. This is denoted in the domain class as annotation. * `MappedExcelObject`: Student class does not have any complex object, hence this annoation is not used in this domain class. @@ -79,4 +79,4 @@ Once the annotation is done, you have just invoke the parser with the Sheet and //Invoke the Sheet parser. List entityList = parser.createEntity(sheet, sheetName, Section.class); -Thats all it requires. Parser would populate all the fields based on the annotation for you. \ No newline at end of file +Thats all it requires. Parser would populate all the fields based on the annotation for you. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9d5dfbf --- /dev/null +++ b/pom.xml @@ -0,0 +1,173 @@ + + + 4.0.0 + + com.thoughtworks + excelparser + 1.0-SNAPSHOT + jar + ${project.artifactId} + Annotated Excel parsing library to simplify parsing excel sheet in JAVA + + thoughtworks + http://www.thoughtworks.com + + + + + + development + + true + + + development + + + + + + test + + test + + + + + + production + + production + + + + + + UTF-8 + 1.7 + + + 3.2 + 2.8.1 + 2.4 + 2.8 + 2.3 + 2.7 + + 2.6 + 1.1.1 + 1.2.17 + 3.8 + 4.12 + + + + + spring-releases + https://repo.spring.io/libs-release + + + + com.springsource.repository.bundles.release + Spring Maven Repository Repository + http://repository.springsource.com/maven/bundles/release + + + + jboss + https://repository.jboss.org/nexus/content/groups/public/ + + + + sonatype + Sonatype Repository + http://repository.sonatype.org/content/groups/public/ + + + + + + spring-releases + https://repo.spring.io/libs-release + + + + + compile + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + ${jdk.version} + ${jdk.version} + + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven.deploy.plugin.version} + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven.jar.plugin.version} + + + + org.apache.maven.plugins + maven-dependency-plugin + ${maven.dependency.plugin.version} + + + + org.apache.maven.plugins + maven-resources-plugin + ${maven-resources-plugin.version} + + + + org.apache.maven.plugins + maven-assembly-plugin + ${maven.assembly.plugin.version} + + + + + + + commons-lang + commons-lang + ${commons.lang.version} + + + + commons-logging + commons-logging + ${commons-logging.version} + + + + log4j + log4j + ${log4j.version} + + + + org.apache.poi + poi + ${poi.version} + + + + junit + junit + ${junit.version} + + + diff --git a/src/main/java/com/thoughtworks/excelparser/SheetParser.java b/src/main/java/com/thoughtworks/excelparser/SheetParser.java index 6b09935..86329bc 100644 --- a/src/main/java/com/thoughtworks/excelparser/SheetParser.java +++ b/src/main/java/com/thoughtworks/excelparser/SheetParser.java @@ -1,22 +1,18 @@ package com.thoughtworks.excelparser; -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.apache.poi.ss.usermodel.Sheet; - import com.thoughtworks.excelparser.annotations.ExcelField; import com.thoughtworks.excelparser.annotations.ExcelObject; import com.thoughtworks.excelparser.annotations.MappedExcelObject; import com.thoughtworks.excelparser.annotations.ParseType; import com.thoughtworks.excelparser.exception.ExcelParsingException; import com.thoughtworks.excelparser.helper.HSSFHelper; +import org.apache.log4j.Logger; +import org.apache.poi.ss.usermodel.Sheet; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; public class SheetParser { private HSSFHelper hssfHelper; @@ -32,7 +28,20 @@ public List createEntity(Sheet sheet, String sheetName, Class clazz) throws ExcelParsingException { List list = new ArrayList(); ExcelObject excelObject = getExcelObject(clazz); - for (int currentLocation = excelObject.start(); currentLocation <= excelObject.end(); currentLocation++) { + + if (excelObject.start() <= 0 || excelObject.end() < 0) { + return list; + } + + int end = excelObject.end(); + if (end == 0) { + end = getRowOrColumnEnd(sheet, sheetName, clazz, excelObject.parseType()); + } + if (end < excelObject.start() && end > 0) { + end = excelObject.start(); + } + + for (int currentLocation = excelObject.start(); currentLocation <= end; currentLocation++) { T object = getNewInstance(sheet, sheetName, clazz, excelObject.parseType(), currentLocation, excelObject.zeroIfNull()); List mappedExcelFields = getMappedExcelObjects(clazz); for (Field mappedField : mappedExcelFields) { @@ -50,7 +59,40 @@ public List createEntity(Sheet sheet, String sheetName, Class clazz) return list; } - private Class getFieldType(Field field) { + public int getRowOrColumnEnd(Sheet sheet, String sheetName, Class clazz, ParseType parseType) { + Set positions = getExcelFieldPositionMap(clazz).keySet(); + int maxPosition = 0; + int minPosition = 0; + for(int position : positions) { + if (minPosition == 0) { + minPosition = position; + } + + if (maxPosition < position) { + maxPosition = position; + } + + if (minPosition > position) { + minPosition = position; + } + } + + int maxIndex = 0; + if (parseType == ParseType.COLUMN) { + for(int i = minPosition; i < maxPosition; i++) { + int cellsNumber = sheet.getRow(i).getLastCellNum(); + if (maxIndex < cellsNumber) { + maxIndex = cellsNumber; + } + } + } else if (parseType == ParseType.ROW) { + maxIndex = sheet.getLastRowNum() + 1; + } + + return maxIndex; + } + + private Class getFieldType(Field field) { Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; diff --git a/src/main/java/com/thoughtworks/excelparser/annotations/ExcelObject.java b/src/main/java/com/thoughtworks/excelparser/annotations/ExcelObject.java index 1226d0c..3b06918 100644 --- a/src/main/java/com/thoughtworks/excelparser/annotations/ExcelObject.java +++ b/src/main/java/com/thoughtworks/excelparser/annotations/ExcelObject.java @@ -30,7 +30,7 @@ * * @return int. */ - int start(); + int start() default 1; /** * End of the block. eg. If you want to parse Rows 8 to 408 (400 rows), this @@ -38,7 +38,7 @@ * * @return int. */ - int end(); + int end() default 0; /** * Setting this field to true will return Zero for Number fields (Double, @@ -54,5 +54,5 @@ * * @return boolean. */ - boolean ignoreAllZerosOrNullRows() default false; + boolean ignoreAllZerosOrNullRows() default true; } \ No newline at end of file diff --git a/src/main/java/com/thoughtworks/excelparser/helper/HSSFHelper.java b/src/main/java/com/thoughtworks/excelparser/helper/HSSFHelper.java index cb8e706..fc7cc60 100644 --- a/src/main/java/com/thoughtworks/excelparser/helper/HSSFHelper.java +++ b/src/main/java/com/thoughtworks/excelparser/helper/HSSFHelper.java @@ -4,6 +4,7 @@ import java.text.MessageFormat; import java.util.Date; +import com.thoughtworks.excelparser.annotations.ParseType; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; @@ -175,5 +176,4 @@ private Double getNumberWithoutDecimals(Cell cell, boolean zeroIfNull, Object... } return doubleValue; } - } diff --git a/src/test/java/com/thoughtworks/example/domain/Section.java b/src/test/java/com/thoughtworks/example/domain/Section.java index 74a506e..51159e8 100644 --- a/src/test/java/com/thoughtworks/example/domain/Section.java +++ b/src/test/java/com/thoughtworks/example/domain/Section.java @@ -1,13 +1,13 @@ package com.thoughtworks.example.domain; -import java.util.List; - import com.thoughtworks.excelparser.annotations.ExcelField; -import com.thoughtworks.excelparser.annotations.MappedExcelObject; import com.thoughtworks.excelparser.annotations.ExcelObject; +import com.thoughtworks.excelparser.annotations.MappedExcelObject; import com.thoughtworks.excelparser.annotations.ParseType; -@ExcelObject(parseType = ParseType.COLUMN, start = 2, end = 2) +import java.util.List; + +@ExcelObject(parseType = ParseType.COLUMN, start = 2) public class Section { @ExcelField(position = 2) diff --git a/src/test/java/com/thoughtworks/example/domain/Student.java b/src/test/java/com/thoughtworks/example/domain/Student.java index 9792ffd..af00931 100644 --- a/src/test/java/com/thoughtworks/example/domain/Student.java +++ b/src/test/java/com/thoughtworks/example/domain/Student.java @@ -1,12 +1,12 @@ package com.thoughtworks.example.domain; -import java.util.Date; - import com.thoughtworks.excelparser.annotations.ExcelField; import com.thoughtworks.excelparser.annotations.ExcelObject; import com.thoughtworks.excelparser.annotations.ParseType; -@ExcelObject(parseType = ParseType.ROW, start = 6, end = 8) +import java.util.Date; + +@ExcelObject(parseType = ParseType.ROW, start = 6) public class Student { @ExcelField(position = 2) diff --git a/src/test/java/com/thoughtworks/excelparser/SheetParserTest.java b/src/test/java/com/thoughtworks/excelparser/SheetParserTest.java index f615f83..4b3ff46 100644 --- a/src/test/java/com/thoughtworks/excelparser/SheetParserTest.java +++ b/src/test/java/com/thoughtworks/excelparser/SheetParserTest.java @@ -1,21 +1,22 @@ package com.thoughtworks.excelparser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -import java.io.IOException; -import java.io.InputStream; -import java.text.SimpleDateFormat; -import java.util.List; - +import com.thoughtworks.example.domain.Section; +import com.thoughtworks.example.domain.Student; +import com.thoughtworks.excelparser.annotations.ParseType; +import com.thoughtworks.excelparser.exception.ExcelParsingException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.thoughtworks.example.domain.Section; -import com.thoughtworks.excelparser.exception.ExcelParsingException; +import java.io.IOException; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; public class SheetParserTest { @@ -54,4 +55,13 @@ public void shouldCreateEntityBasedOnAnnotation() throws ExcelParsingException { assertEquals("XYZ", section.getStudents().get(0).getAddress()); assertNull(section.getStudents().get(0).getTotalScore()); } + + @Test + public void testGetRowOrColumnEnd() { + int end = parser.getRowOrColumnEnd(sheet, sheetName, Section.class, ParseType.COLUMN); + assertEquals(end, 2); + + end = parser.getRowOrColumnEnd(sheet, sheetName, Student.class, ParseType.ROW); + assertEquals(end, 8); + } }