Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build
.gradle
.idea
.settings
excel-parser.iml
excel-parser.iml
*.iml
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ First we will see the steps to annotate Section object:

@ExcelField(position = 3)
private String section;

@MappedExcelObject
private List <Student> students;
}
Expand All @@ -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.
Expand All @@ -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.
Thats all it requires. Parser would populate all the fields based on the annotation for you.
173 changes: 173 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.thoughtworks</groupId>
<artifactId>excelparser</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Annotated Excel parsing library to simplify parsing excel sheet in JAVA</description>
<organization>
<name>thoughtworks</name>
<url>http://www.thoughtworks.com</url>
</organization>

<profiles>
<!-- mvn clean package -->
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.active>development</profile.active>
</properties>
</profile>

<!-- mvn clean package -Ptest -->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>

<!-- mvn clean package -Pproduction -->
<profile>
<id>production</id>
<properties>
<profile.active>production</profile.active>
</properties>
</profile>
</profiles>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>

<!--plugins-->
<maven.compiler.plugin.version>3.2</maven.compiler.plugin.version>
<maven.deploy.plugin.version>2.8.1</maven.deploy.plugin.version>
<maven.jar.plugin.version>2.4</maven.jar.plugin.version>
<maven.dependency.plugin.version>2.8</maven.dependency.plugin.version>
<maven.assembly.plugin.version>2.3</maven.assembly.plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>

<commons.lang.version>2.6</commons.lang.version>
<commons-logging.version>1.1.1</commons-logging.version>
<log4j.version>1.2.17</log4j.version>
<poi.version>3.8</poi.version>
<junit.version>4.12</junit.version>
</properties>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>

<repository>
<id>com.springsource.repository.bundles.release</id>
<name>Spring Maven Repository Repository</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>

<repository>
<id>jboss</id>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>

<repository>
<id>sonatype</id>
<name>Sonatype Repository</name>
<url>http://repository.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven.deploy.plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven.dependency.plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
68 changes: 55 additions & 13 deletions src/main/java/com/thoughtworks/excelparser/SheetParser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -32,7 +28,20 @@ public <T> List<T> createEntity(Sheet sheet, String sheetName, Class<T> clazz)
throws ExcelParsingException {
List<T> list = new ArrayList<T>();
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<Field> mappedExcelFields = getMappedExcelObjects(clazz);
for (Field mappedField : mappedExcelFields) {
Expand All @@ -50,7 +59,40 @@ public <T> List<T> createEntity(Sheet sheet, String sheetName, Class<T> clazz)
return list;
}

private Class<?> getFieldType(Field field) {
public <T> int getRowOrColumnEnd(Sheet sheet, String sheetName, Class<T> clazz, ParseType parseType) {
Set<Integer> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
*
* @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
* field should be set as 408.
*
* @return int.
*/
int end();
int end() default 0;

/**
* Setting this field to true will return Zero for Number fields (Double,
Expand All @@ -54,5 +54,5 @@
*
* @return boolean.
*/
boolean ignoreAllZerosOrNullRows() default false;
boolean ignoreAllZerosOrNullRows() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,5 +176,4 @@ private Double getNumberWithoutDecimals(Cell cell, boolean zeroIfNull, Object...
}
return doubleValue;
}

}
8 changes: 4 additions & 4 deletions src/test/java/com/thoughtworks/example/domain/Section.java
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/thoughtworks/example/domain/Student.java
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading