Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering cols #49

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
* @return column name/header.
*/
String value() default "";

int index() default 999999999;


// boolean isFormatted() default false;
// String format() default "dd/MM/yyyy";
//
// boolean isFromula() default false;



/**
* Setting this to <code>false</code> will enable the null check on the Column values, to ensure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects) {
}

public <EB> void addSheet(Class<EB> beanType, List<EB> rowObjects, List<String> headers) {
// SheetName
// SheetNameindex
Sheet sheet = beanType.getAnnotation(Sheet.class);
String sheetName = sheet != null ? sheet.value() : null;

Expand Down
78 changes: 75 additions & 3 deletions src/main/java/io/github/millij/poi/util/Spreadsheet.java
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the Tabs with Spaces in this file please.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -102,9 +103,41 @@ public static Map<String, String> getColumnToPropertyMap(Class<?> beanType) {
public static List<String> getColumnNames(Class<?> beanType) {
// Bean Property to Column Mapping
final Map<String, String> propToColumnMap = getPropertyToColumnNameMap(beanType);

final ArrayList<String> columnNames = new ArrayList<>(propToColumnMap.values());
return columnNames;
final Map<Integer,String> indexToPropMap = getColumnIndexToPropertyMap(beanType);

if(indexToPropMap.size() > 1) {

Set<Integer> indexes = indexToPropMap.keySet();
List<Integer> indexList = new ArrayList<Integer>(indexes);
Collections.sort(indexList);

ArrayList<String> columnNames = new ArrayList<>();

for(Integer index : indexToPropMap.keySet())
{
String colValue = propToColumnMap.get(indexToPropMap.get(index));
columnNames.add(colValue);
}

ArrayList<String> columnNamesDup = new ArrayList<>(propToColumnMap.values());

for(String str : columnNamesDup)
{
if(columnNames.contains(str)){
continue;
}
else{
columnNames.add(str);
}
}
return columnNames;
}
else {
ArrayList<String> columnNames = new ArrayList<>(propToColumnMap.values());
return columnNames;
}


}


Expand Down Expand Up @@ -199,6 +232,45 @@ public static <T> T rowAsBean(Class<T> beanClz, Map<String, String> cellProperie
return null;
}


public static Map<Integer,String> getColumnIndexToPropertyMap(Class<?> beanType) {
// Sanity checks
if (beanType == null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pp] Instead of check with == we prefer Objects.isNull
if (Objects.isNull(beanType){ //logic }

throw new IllegalArgumentException("getColumnToIndexMap :: Invalid ExcelBean type - " + beanType);
}

// Property to Column Index Mapping
final Map<Integer,String> mapping = new HashMap<Integer,String>();

// Fields
Field[] fields = beanType.getDeclaredFields();
for (Field f : fields) {
String fieldName = f.getName();

SheetColumn ec = f.getAnnotation(SheetColumn.class);

if (ec != null) {
int index = ec.index();
mapping.put(index,fieldName);
}
}

// Methods
Method[] methods = beanType.getDeclaredMethods();
for (Method m : methods) {
String fieldName = Beans.getFieldName(m);

SheetColumn ec = m.getAnnotation(SheetColumn.class);

if (ec != null) {
int index = ec.index();
mapping.put(index,fieldName);
}
}

LOGGER.info("Bean property to Excel Column of - {} : {}", beanType, mapping);
return Collections.unmodifiableMap(mapping);
}


}
44 changes: 44 additions & 0 deletions src/test/java/indexingCols/IndexedHeaderWriteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package indexingCols;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.millij.bean.Emp_indexed;
import io.github.millij.bean.Employee;
import io.github.millij.poi.ss.model.annotations.SheetColumn;
import io.github.millij.poi.ss.writer.SpreadsheetWriter;
import io.github.millij.poi.ss.writer.SpreadsheetWriterTest;

public class IndexedHeaderWriteTest
{
private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetWriterTest.class);

private final String _path_test_output = "test-cases/output/";



@Test
public void test_write_xlsx_single_sheet() throws IOException {
final String filepath_output_file = _path_test_output.concat("indexed_header_sample.xlsx");

// Excel Writer
LOGGER.info("test_write_xlsx_single_sheet :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new SpreadsheetWriter(filepath_output_file);

// Employees
List<Emp_indexed> employees = new ArrayList<Emp_indexed>();
employees.add(new Emp_indexed("1", "foo", 12, "MALE", 1.68,"Chennai"));
employees.add(new Emp_indexed("2", "bar", 24, "MALE", 1.98,"Banglore"));
employees.add(new Emp_indexed("3", "foo bar", 10, "FEMALE",2.0,"Kolkata"));

// Write
gew.addSheet(Emp_indexed.class, employees);
gew.write();
}

}
100 changes: 100 additions & 0 deletions src/test/java/io/github/millij/bean/Emp_indexed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.github.millij.bean;

import io.github.millij.poi.ss.model.annotations.SheetColumn;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please annotate class with @Sheet

public class Emp_indexed
{
private String id;
private String name;

@SheetColumn(value="Age",index=4)
private Integer age;

@SheetColumn(value="Gender")
private String gender;

@SheetColumn(value="Height (mts)",index=2)
private Double height;

@SheetColumn(value="Address")
private String address;


public Emp_indexed() {
// Default
}

public Emp_indexed(String id, String name, Integer age, String gender, Double height,String address) {
super();

this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.address = address;

}



@SheetColumn(value="ID")
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@SheetColumn(value="Name",index=5)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Double getHeight() {
return height;
}

public void setHeight(Double height) {
this.height = height;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Emp_indexed [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", height=" + height
+ ", address=" + address + "]";
}



}