-
Notifications
You must be signed in to change notification settings - Fork 31
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
Closed
Ordering cols #49
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pp] Instead of check with == we prefer Objects.isNull |
||
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please annotate class with |
||
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 + "]"; | ||
} | ||
|
||
|
||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.