导出数据方法setDataList增加国际化配置参数
修改所有字典展示取值itemDesc为itemValue
This commit is contained in:
@@ -55,7 +55,7 @@ public class BaseCfg<T> extends BaseEntity<T> implements Cloneable{
|
||||
/**
|
||||
* GK类型
|
||||
*/
|
||||
@ExcelField(title="action")
|
||||
@ExcelField(title="action",dictType="SERVICE_ACTION")
|
||||
protected Integer action;
|
||||
/**
|
||||
* 有效标识
|
||||
|
||||
@@ -126,6 +126,12 @@ public class DictUtils {
|
||||
return getDictLabel(dictKey, itemCode, "默认");
|
||||
}
|
||||
|
||||
/*public static String getDictLabel(String dictKey, String itemCode, String itemValue){
|
||||
String itemLabel = getDictOption(dictKey).get(itemCode);
|
||||
|
||||
return StringUtil.isBlank(itemLabel) ? defaultValue : itemLabel;
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
public static String getDictCode(String dictKey, String itemValue, String defaultValue){
|
||||
|
||||
@@ -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.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -503,7 +503,7 @@ public class AvContentController extends BaseController {
|
||||
}
|
||||
|
||||
Page<CfgIndexInfo> page = avContentCfgService.findPage(pageInfo, entity);
|
||||
new ExportExcel(msgProp,null, CfgIndexInfo.class,1).setDataList(page.getList()).write(response, fileName).dispose();
|
||||
new ExportExcel(msgProp,null, CfgIndexInfo.class,1).setDataList(msgProp,page.getList()).write(response, fileName).dispose();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -144,7 +144,7 @@ public class SpecificServiceHostCfgController extends BaseController {
|
||||
String fileName = msgProp.getProperty("agreement_ip_configuration").replaceAll(" ", "_")+".xlsx";
|
||||
List<SpecificServiceHostCfg> list = Lists.newArrayList();
|
||||
list.add(new SpecificServiceHostCfg());
|
||||
new ExportExcel(msgProp,msgProp.getProperty("agreement_ip_configuration"), SpecificServiceHostCfg.class, 2).setDataList(list).
|
||||
new ExportExcel(msgProp,msgProp.getProperty("agreement_ip_configuration"), SpecificServiceHostCfg.class, 2).setDataList(msgProp,list).
|
||||
write(request,response, fileName).dispose();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -199,7 +199,7 @@ public class UserController extends BaseController{
|
||||
try {
|
||||
String fileName = "用户数据-"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
|
||||
Page<SysUser> page = userService.findUser(new Page<SysUser>(request, response, -1), user);
|
||||
new ExportExcel("用户数据", SysUser.class).setDataList(page.getList()).write(response, fileName).dispose();
|
||||
new ExportExcel("用户数据", SysUser.class).setDataList(getMsgProp(),page.getList()).write(response, fileName).dispose();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
addMessage(redirectAttributes, "导出用户失败!失败信息:"+e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user