导出数据方法setDataList增加国际化配置参数

修改所有字典展示取值itemDesc为itemValue
This commit is contained in:
duandongmei
2018-06-13 16:29:11 +08:00
parent 613f19c145
commit 6960e1528e
26 changed files with 213 additions and 174 deletions

View File

@@ -94,6 +94,22 @@ public class ExportExcel {
getFields(list,cls.getSuperclass());
}
}
/**
* //递归获取cls实体对象及父级对象的method
* @param list
* @param cls
*/
public void getMethods(List<Method> list,Class<?> cls) {
Method[] methods=cls.getDeclaredMethods();
if(methods != null && methods.length > 0){
for (Method method : methods) {
list.add(method);
}
}
if(cls.getSuperclass() != null){
getMethods(list,cls.getSuperclass());
}
}
/**
* 构造函数
@@ -113,6 +129,7 @@ public class ExportExcel {
public ExportExcel(String title, Class<?> cls, int type, int... groups){
List<Field> list=new ArrayList<Field>();
// Get annotation field
//递归获取cls实体对象及父级对象的属性
getFields(list, cls);
if(!StringUtil.isEmpty(list)){
@@ -139,27 +156,31 @@ public class ExportExcel {
}
}
}
List<Method> ms=new ArrayList<Method>();
// Get annotation method
Method[] ms = cls.getDeclaredMethods();
for (Method m : ms){
ExcelField ef = m.getAnnotation(ExcelField.class);
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});
//递归获取cls实体对象及父级对象的属性
getMethods(ms, cls);
if(!StringUtil.isEmpty(ms)){
for (Method m : ms){
ExcelField ef = m.getAnnotation(ExcelField.class);
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});
}
}else{
annotationList.add(new Object[]{ef, m});
}
}
}
@@ -187,6 +208,7 @@ public class ExportExcel {
}
/**
* 构造函数
* @param msgProp 国际化配置
* @param title 表格标题,传“空值”,表示无标题
* @param cls 实体对象通过annotation.ExportField获取标题
* @param type 导出类型1:导出数据2导出模板
@@ -222,27 +244,31 @@ public class ExportExcel {
}
}
}
List<Method> ms=new ArrayList<Method>();
// Get annotation method
Method[] ms = cls.getDeclaredMethods();
for (Method m : ms){
ExcelField ef = m.getAnnotation(ExcelField.class);
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});
//递归获取cls实体对象及父级对象的属性
getMethods(ms, cls);
if(!StringUtil.isEmpty(ms)){
for (Method m : ms){
ExcelField ef = m.getAnnotation(ExcelField.class);
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});
}
}else{
annotationList.add(new Object[]{ef, m});
}
}
}
@@ -436,7 +462,7 @@ public class ExportExcel {
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
cell.setCellValue((Integer) val);
cell.setCellValue((String) val);
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
} else if (val instanceof Double) {
@@ -445,7 +471,7 @@ public class ExportExcel {
cell.setCellValue((Float) val);
} else if (val instanceof Date) {
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy-MM-dd"));
style.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellValue((Date) val);
} else {
if (fieldType != Class.class){
@@ -467,7 +493,7 @@ public class ExportExcel {
* 添加数据通过annotation.ExportField添加数据
* @return list 数据列表
*/
public <E> ExportExcel setDataList(List<E> list){
public <E> ExportExcel setDataList(Properties msgProp,List<E> list){
for (E e : list){
int colunm = 0;
Row row = this.addRow();
@@ -488,7 +514,7 @@ public class ExportExcel {
}
// If is dict, get dict label
if (StringUtils.isNotBlank(ef.dictType())){
val = DictUtils.getDictLabel(val==null?"":val.toString(), ef.dictType(), "");
val = DictUtils.getDictLabel(ef.dictType(),val==null?"":val.toString() , "");
}
}catch(Exception ex) {
// Failure to ignore
@@ -563,41 +589,41 @@ public class ExportExcel {
return this;
}
// /**
// * 导出测试
// */
// public static void main(String[] args) throws Throwable {
//
// List<String> headerList = Lists.newArrayList();
// for (int i = 1; i <= 10; i++) {
// headerList.add("表头"+i);
// }
//
// List<String> dataRowList = Lists.newArrayList();
// for (int i = 1; i <= headerList.size(); i++) {
// dataRowList.add("数据"+i);
// }
//
// List<List<String>> dataList = Lists.newArrayList();
// for (int i = 1; i <=1000000; i++) {
// dataList.add(dataRowList);
// }
//
// ExportExcel ee = new ExportExcel("表格标题", headerList);
//
// for (int i = 0; i < dataList.size(); i++) {
// Row row = ee.addRow();
// for (int j = 0; j < dataList.get(i).size(); j++) {
// ee.addCell(row, j, dataList.get(i).get(j));
// }
// }
//
// ee.writeFile("target/export.xlsx");
//
// ee.dispose();
//
// log.debug("Export success.");
//
// }
/**
* 导出测试
*/
public static void main(String[] args) throws Throwable {
List<String> headerList = Lists.newArrayList();
for (int i = 1; i <= 10; i++) {
headerList.add("表头"+i);
}
List<String> dataRowList = Lists.newArrayList();
for (int i = 1; i <= headerList.size(); i++) {
dataRowList.add("数据"+i);
}
List<List<String>> dataList = Lists.newArrayList();
for (int i = 1; i <=1000000; i++) {
dataList.add(dataRowList);
}
ExportExcel ee = new ExportExcel("表格标题", headerList);
for (int i = 0; i < dataList.size(); i++) {
Row row = ee.addRow();
for (int j = 0; j < dataList.get(i).size(); j++) {
ee.addCell(row, j, dataList.get(i).get(j));
}
}
ee.writeFile("target/export.xlsx");
ee.dispose();
log.debug("Export success.");
}
}