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

+)针对使用注解的字段,按照字段index顺序导入和导出 #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<slf4j-api.version>1.7.25</slf4j-api.version>
<poi.version>3.17</poi.version>
<common.beanutils.version>1.9.3</common.beanutils.version>
</properties>

<dependencies>
Expand All @@ -45,6 +46,11 @@
<version>${poi.version}</version>
</dependency>

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

<build>
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/xuxueli/poi/excel/ExcelCommonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.xuxueli.poi.excel;


import com.xuxueli.poi.excel.annotation.ExcelField;
import com.xuxueli.poi.excel.util.FieldForSort;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.collections4.comparators.ComparableComparator;
import org.apache.commons.collections4.comparators.ComparatorChain;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ExcelCommonUtil {
public static List<FieldForSort> sortFieldByAnno(Class<?> clazz) {
Field[] fieldsArr = clazz.getDeclaredFields();
List<FieldForSort> fields = new ArrayList<FieldForSort>();
for (Field field : fieldsArr) {
ExcelField ec = field.getAnnotation(ExcelField.class);
if (ec == null) {
// 没有ExcelCell Annotation 视为不汇入
continue;
}
int id = ec.index();
fields.add(new FieldForSort(field, id));
}
sortByProperties(fields, true, false, "index");
return fields;
}

@SuppressWarnings("unchecked")
public static void sortByProperties(List<? extends Object> list, boolean isNullHigh, boolean isReversed,
String... props) {
if (CollectionUtils.isNotEmpty(list)) {
Comparator<?> typeComp = ComparableComparator.INSTANCE;
if (isNullHigh == true) {
typeComp = ComparatorUtils.nullHighComparator(typeComp);
} else {
typeComp = ComparatorUtils.nullLowComparator(typeComp);
}
if (isReversed) {
typeComp = ComparatorUtils.reversedComparator(typeComp);
}

List<Object> sortCols = new ArrayList<Object>();

if (props != null) {
for (String prop : props) {
sortCols.add(new BeanComparator(prop, typeComp));
}
}
if (sortCols.size() > 0) {
Comparator<Object> sortChain = new ComparatorChain(sortCols);
Collections.sort(list, sortChain);
}
}
}
}
18 changes: 11 additions & 7 deletions src/main/java/com/xuxueli/poi/excel/ExcelExportUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.xuxueli.poi.excel.annotation.ExcelField;
import com.xuxueli.poi.excel.annotation.ExcelSheet;
import com.xuxueli.poi.excel.util.FieldForSort;
import com.xuxueli.poi.excel.util.FieldReflectionUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
Expand All @@ -11,7 +12,6 @@
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -85,13 +85,17 @@ private static void makeSheet(Workbook workbook, List<?> sheetDataList){

// sheet field
List<Field> fields = new ArrayList<Field>();
if (sheetClass.getDeclaredFields()!=null && sheetClass.getDeclaredFields().length>0) {
for (Field field: sheetClass.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
List<FieldForSort> fieldSorts = ExcelCommonUtil.sortFieldByAnno(sheetClass);
/* if (sheetClass.getDeclaredFields()!=null && sheetClass.getDeclaredFields().length>0) {
for (Field field: sheetClass.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
fields.add(field);
}
fields.add(field);
}
}*/
for (FieldForSort fieldForSort:fieldSorts){
fields.add(fieldForSort.getField());
}

if (fields==null || fields.size()==0) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/xuxueli/poi/excel/ExcelImportUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xuxueli.poi.excel;

import com.xuxueli.poi.excel.annotation.ExcelSheet;
import com.xuxueli.poi.excel.util.FieldForSort;
import com.xuxueli.poi.excel.util.FieldReflectionUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
Expand All @@ -11,7 +12,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -44,13 +44,17 @@ public static List<Object> importSheet(Workbook workbook, Class<?> sheetClass) {

// sheet field
List<Field> fields = new ArrayList<Field>();
if (sheetClass.getDeclaredFields()!=null && sheetClass.getDeclaredFields().length>0) {
List<FieldForSort> fieldSorts = ExcelCommonUtil.sortFieldByAnno(sheetClass);
/* if (sheetClass.getDeclaredFields()!=null && sheetClass.getDeclaredFields().length>0) {
for (Field field: sheetClass.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
fields.add(field);
}
}*/
for (FieldForSort fieldForSort:fieldSorts){
fields.add(fieldForSort.getField());
}

if (fields==null || fields.size()==0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
*/
int width() default 0;

/**
* 字段显示顺序序号
*/
int index();

/**
* 水平对齐方式
*
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/xuxueli/poi/excel/util/FieldForSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.xuxueli.poi.excel.util;

import java.lang.reflect.Field;

/**
* @author liuyanjun
*/
public class FieldForSort {

private Field field;
private int index;

/**
* @param field
*/
public FieldForSort(Field field) {
super();
this.field = field;
}

/**
* @param field
* @param index
*/
public FieldForSort(Field field, int index) {
super();
this.field = field;
this.index = index;
}

/**
* @return the field
*/
public Field getField() {
return field;
}

/**
* @param field
* the field to set
*/
public void setField(Field field) {
this.field = field;
}

/**
* @return the index
*/
public int getIndex() {
return index;
}

/**
* @param index
* the index to set
*/
public void setIndex(int index) {
this.index = index;
}

}
2 changes: 1 addition & 1 deletion src/test/java/com/xuxueli/poi/excel/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) {
ShopDTO shop = new ShopDTO(true, "商户"+i, (short) i, 1000+i, 10000+i, (float) (1000+i), (double) (10000+i), new Date());
shopDTOList.add(shop);
}
String filePath = "/Users/xuxueli/Downloads/demo-sheet.xls";
String filePath = "E:/demo-sheet.xls";

/**
* Excel导出:Object 转换为 Excel
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/xuxueli/poi/excel/test/model/ShopDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
@ExcelSheet(name = "商户列表", headColor = HSSFColor.HSSFColorPredefined.LIGHT_GREEN)
public class ShopDTO {

@ExcelField(name = "是否VIP商户")
@ExcelField(name = "是否VIP商户",index=1)
private boolean vip;

@ExcelField(name = "商户名称", align = HorizontalAlignment.CENTER)
@ExcelField(name = "商户名称",index=2, align = HorizontalAlignment.CENTER)
private String shopName;

@ExcelField(name = "分店数量")
@ExcelField(name = "分店数量",index=3)
private short branchNum;

@ExcelField(name = "商户ID")
@ExcelField(name = "商户ID",index=5)
private int shopId;

@ExcelField(name = "浏览人数")
@ExcelField(name = "浏览人数",index=4)
private long visitNum;

@ExcelField(name = "当月营业额")
@ExcelField(name = "当月营业额",index=6)
private float turnover;

@ExcelField(name = "历史营业额")
@ExcelField(name = "历史营业额",index=7)
private double totalTurnover;

@ExcelField(name = "开店时间", dateformat = "yyyy-MM-dd HH:mm:ss")
@ExcelField(name = "开店时间",index=8, dateformat = "yyyy-MM-dd HH:mm:ss")
private Date addTime;


Expand Down