IP通用配置导出,只导出配置列表上展示的列,界面上的列表需要指定exportColumn,与后台注解ExcelField 的title一致
This commit is contained in:
@@ -297,6 +297,113 @@ public class ExportExcel {
|
||||
}
|
||||
initialize(title, headerList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
* @param msgProp 国际化配置
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param cls 实体对象,通过annotation.ExportField获取标题
|
||||
* @param type 导出类型(1:导出数据;2:导出模板)
|
||||
* @param groups 导入分组
|
||||
*/
|
||||
public ExportExcel(String columns,Properties msgProp,String title, Class<?> cls, int type, int... groups){
|
||||
String[] cloumnArray=columns.split(",");
|
||||
List<Field> list=new ArrayList<Field>();
|
||||
// Get annotation field
|
||||
//递归获取cls实体对象及父级对象的属性
|
||||
getFields(list, cls);
|
||||
|
||||
if(!StringUtil.isEmpty(list)){
|
||||
for (Field f : list){
|
||||
ExcelField ef = f.getAnnotation(ExcelField.class);
|
||||
boolean export=false;
|
||||
for(String column:cloumnArray) {
|
||||
if(ef!=null&&column.equals(ef.title())) {
|
||||
export=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(export) {
|
||||
if (ef != null && (ef.type()==0 || ef.type()==type)){
|
||||
if (groups!=null && groups.length>0){
|
||||
boolean inGroup = false;
|
||||
for (int g : groups){
|
||||
if (inGroup){
|
||||
break;
|
||||
}
|
||||
for (int efg : ef.groups()){
|
||||
if (g == efg){
|
||||
inGroup = true;
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<Method> ms=new ArrayList<Method>();
|
||||
// Get annotation method
|
||||
//递归获取cls实体对象及父级对象的属性
|
||||
getMethods(ms, cls);
|
||||
if(!StringUtil.isEmpty(ms)){
|
||||
for (Method m : ms){
|
||||
ExcelField ef = m.getAnnotation(ExcelField.class);
|
||||
boolean export=false;
|
||||
for(String column:cloumnArray) {
|
||||
if(ef!=null&&column.equals(ef.title())) {
|
||||
export=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(export) {
|
||||
if (ef != null && (ef.type()==0 || ef.type()==type)){
|
||||
if (groups!=null && groups.length>0){
|
||||
boolean inGroup = false;
|
||||
for (int g : groups){
|
||||
if (inGroup){
|
||||
break;
|
||||
}
|
||||
for (int efg : ef.groups()){
|
||||
if (g == efg){
|
||||
inGroup = true;
|
||||
annotationList.add(new Object[]{ef, m});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
annotationList.add(new Object[]{ef, m});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Field sorting
|
||||
Collections.sort(annotationList, new Comparator<Object[]>() {
|
||||
public int compare(Object[] o1, Object[] o2) {
|
||||
return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
||||
new Integer(((ExcelField)o2[0]).sort()));
|
||||
};
|
||||
});
|
||||
// Initialize
|
||||
List<String> headerList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String t = ((ExcelField)os[0]).title();
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
String[] ss = StringUtils.split(t, "**", 2);
|
||||
if (ss.length==2){
|
||||
t = ss[0];
|
||||
}
|
||||
}
|
||||
headerList.add(msgProp.getProperty(t)==null?t:msgProp.getProperty(t));
|
||||
}
|
||||
initialize(title, headerList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
@@ -497,48 +604,116 @@ public class ExportExcel {
|
||||
* @return list 数据列表
|
||||
*/
|
||||
public <E> ExportExcel setDataList(Properties msgProp,List<E> list,Map map){
|
||||
for (E e : list){
|
||||
int colunm = 0;
|
||||
Row row = this.addRow();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (E e : list){
|
||||
int colunm = 0;
|
||||
Row row = this.addRow();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object[] os : annotationList){
|
||||
ExcelField ef = (ExcelField)os[0];
|
||||
Object val = null;
|
||||
// Get entity value
|
||||
try{
|
||||
if (StringUtils.isNotBlank(ef.value())){
|
||||
val = Reflections.invokeGetter(e, ef.value());
|
||||
}else{
|
||||
if (os[1] instanceof Field){
|
||||
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
|
||||
}else if (os[1] instanceof Method){
|
||||
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
|
||||
}
|
||||
}
|
||||
// If is dict, get dict label
|
||||
if (StringUtils.isNotBlank(ef.dictType())){
|
||||
String valStr=val==null?"":val.toString();
|
||||
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|
||||
|| "label".equals(ef.dictType())){
|
||||
// Get basic info
|
||||
val = getBasicInfo(ef.dictType(),map,valStr);
|
||||
}else{
|
||||
//字典数据已做国际化处理
|
||||
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
|
||||
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
|
||||
val = msgProp.getProperty(dict,dict);
|
||||
}
|
||||
|
||||
}
|
||||
}catch(Exception ex) {
|
||||
// Failure to ignore
|
||||
log.error("Get entity value failed",ex);
|
||||
val = "";
|
||||
}
|
||||
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
|
||||
sb.append(val + ", ");
|
||||
}
|
||||
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 添加数据(通过annotation.ExportField添加数据)
|
||||
* @return list 数据列表
|
||||
*/
|
||||
public <E> ExportExcel setDataList(String columns,Properties msgProp,List<E> list,Map map){
|
||||
if(StringUtils.isBlank(columns)) {
|
||||
return setDataList(msgProp,list,map);
|
||||
}else {
|
||||
Map<String,Boolean> exportMap=new HashMap<>();
|
||||
String[] columnArray=columns.split(",");
|
||||
for (Object[] os : annotationList){
|
||||
ExcelField ef = (ExcelField)os[0];
|
||||
Object val = null;
|
||||
// Get entity value
|
||||
try{
|
||||
if (StringUtils.isNotBlank(ef.value())){
|
||||
val = Reflections.invokeGetter(e, ef.value());
|
||||
}else{
|
||||
if (os[1] instanceof Field){
|
||||
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
|
||||
}else if (os[1] instanceof Method){
|
||||
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
|
||||
}
|
||||
for(String column:columnArray) {
|
||||
if(column.equals(ef.title())) {
|
||||
exportMap.put(column, true);
|
||||
break;
|
||||
}
|
||||
// If is dict, get dict label
|
||||
if (StringUtils.isNotBlank(ef.dictType())){
|
||||
String valStr=val==null?"":val.toString();
|
||||
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|
||||
|| "label".equals(ef.dictType())){
|
||||
// Get basic info
|
||||
val = getBasicInfo(ef.dictType(),map,valStr);
|
||||
}else{
|
||||
//字典数据已做国际化处理
|
||||
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
|
||||
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
|
||||
val = msgProp.getProperty(dict,dict);
|
||||
}
|
||||
|
||||
}
|
||||
}catch(Exception ex) {
|
||||
// Failure to ignore
|
||||
log.error("Get entity value failed",ex);
|
||||
val = "";
|
||||
}
|
||||
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
|
||||
sb.append(val + ", ");
|
||||
}
|
||||
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
|
||||
for (E e : list){
|
||||
int colunm = 0;
|
||||
Row row = this.addRow();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object[] os : annotationList){
|
||||
ExcelField ef = (ExcelField)os[0];
|
||||
if(exportMap.containsKey(ef.title())) {
|
||||
Object val = null;
|
||||
// Get entity value
|
||||
try{
|
||||
|
||||
if (StringUtils.isNotBlank(ef.value())){
|
||||
val = Reflections.invokeGetter(e, ef.value());
|
||||
}else{
|
||||
if (os[1] instanceof Field){
|
||||
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
|
||||
}else if (os[1] instanceof Method){
|
||||
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
|
||||
}
|
||||
}
|
||||
// If is dict, get dict label
|
||||
if (StringUtils.isNotBlank(ef.dictType())){
|
||||
String valStr=val==null?"":val.toString();
|
||||
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|
||||
|| "label".equals(ef.dictType())){
|
||||
// Get basic info
|
||||
val = getBasicInfo(ef.dictType(),map,valStr);
|
||||
}else{
|
||||
//字典数据已做国际化处理
|
||||
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
|
||||
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
|
||||
val = msgProp.getProperty(dict,dict);
|
||||
}
|
||||
|
||||
}
|
||||
}catch(Exception ex) {
|
||||
// Failure to ignore
|
||||
log.error("Get entity value failed",ex);
|
||||
val = "";
|
||||
}
|
||||
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
|
||||
sb.append(val + ", ");
|
||||
}
|
||||
}
|
||||
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user