voip界面去掉无用的检索条件
摘要文件类型增加音视频文件类 ip和http增加配置导出
This commit is contained in:
@@ -9,14 +9,17 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -39,11 +42,16 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.nis.domain.FunctionRegionDict;
|
||||
import com.nis.domain.SysDataDictionaryItem;
|
||||
import com.nis.domain.basics.ServiceDictInfo;
|
||||
import com.nis.util.DictUtils;
|
||||
import com.nis.util.Encodes;
|
||||
import com.nis.util.Reflections;
|
||||
import com.nis.util.StringUtil;
|
||||
import com.sun.xml.internal.rngom.ast.builder.CommentList;
|
||||
|
||||
import sun.util.logging.resources.logging;
|
||||
|
||||
/**
|
||||
* 导出Excel文件(导出“XLSX”格式,支持大数据量导出 @see org.apache.poi.ss.SpreadsheetVersion)
|
||||
@@ -64,6 +72,11 @@ public class ExportExcel {
|
||||
*/
|
||||
private Sheet sheet;
|
||||
|
||||
/**
|
||||
* 工作表对象
|
||||
*/
|
||||
private Map<String,Sheet> sheets;
|
||||
|
||||
/**
|
||||
* 样式列表
|
||||
*/
|
||||
@@ -74,10 +87,16 @@ public class ExportExcel {
|
||||
*/
|
||||
private int rownum;
|
||||
|
||||
/**
|
||||
* 当前行号
|
||||
*/
|
||||
private Map<String, Integer> rownums;
|
||||
|
||||
/**
|
||||
* 注解列表(Object[]{ ExcelField, Field/Method })
|
||||
*/
|
||||
List<Object[]> annotationList = Lists.newArrayList();
|
||||
Map<String, List<Object[]>> annotationMap=new HashMap<>();
|
||||
|
||||
/**
|
||||
* //递归获取cls实体对象及父级对象的属性
|
||||
@@ -111,6 +130,159 @@ public class ExportExcel {
|
||||
getFields(list,cls.getSuperclass());
|
||||
}
|
||||
}
|
||||
public String[] validRegionFieldAndSetComment(String headerStr,String commentStr,FunctionRegionDict region,Properties msgProp){
|
||||
String[] titleInfo=new String[2];
|
||||
if(region.getIsMaat().equals(1)){//maat
|
||||
if(region.getRegionType().equals(1)){//IP配置
|
||||
//ip配置需要导入的信息:srcIp srcPort destIp destPort Protocol Direction
|
||||
//确定需要导入的srcIp srcPort destIp destPort信息
|
||||
if(StringUtil.isEmpty(region.getConfigIpPortShow())){//无源目的ip和端口
|
||||
if("client_ip".equals(headerStr)
|
||||
|| "server_ip".equals(headerStr)
|
||||
|| "client_port".equals(headerStr)
|
||||
|| "server_port".equals(headerStr)
|
||||
){
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}
|
||||
}else{
|
||||
if("client_ip".equals(headerStr)){
|
||||
if(((","+region.getConfigIpPortShow()+",").indexOf(",1,") > -1) ){
|
||||
commentStr="";
|
||||
if((","+region.getConfigIpType()+",").indexOf(",4,") > -1){
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",3,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0-1"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0/16"+"\n";
|
||||
}
|
||||
}
|
||||
if((","+region.getConfigIpType()+",").indexOf(",6,") > -1){
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",3,") > -1){
|
||||
commentStr=commentStr+"::"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"::-::"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"::/128"+"\n";
|
||||
}
|
||||
}
|
||||
}else{
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}
|
||||
}
|
||||
if("server_ip".equals(headerStr)){
|
||||
if((","+region.getConfigIpPortShow()+",").indexOf(",3,") > -1){
|
||||
commentStr="";
|
||||
if((","+region.getConfigIpType()+",").indexOf(",4,") > -1){
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",3,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0-1"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"0.0.0.0/16"+"\n";
|
||||
}
|
||||
}
|
||||
if((","+region.getConfigIpType()+",").indexOf(",6,") > -1){
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",3,") > -1){
|
||||
commentStr=commentStr+"::"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"::-::"+"\n";
|
||||
}
|
||||
if((","+region.getConfigIpPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"::/128"+"\n";
|
||||
}
|
||||
}
|
||||
}else{
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}
|
||||
}
|
||||
if("client_port".equals(headerStr)){
|
||||
if(((","+region.getConfigIpPortShow()+",").indexOf(",2,") > -1)){
|
||||
commentStr="";
|
||||
if((","+region.getConfigPortPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"0["+msgProp.getProperty("val_src_port")+"]"+"\n";
|
||||
}
|
||||
if((","+region.getConfigPortPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"0/65535["+msgProp.getProperty("val_src_port_mask")+"]"+"\n";
|
||||
}
|
||||
}else{
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}
|
||||
}
|
||||
if("server_port".equals(headerStr)){
|
||||
if((","+region.getConfigIpPortShow()+",").indexOf(",4,") > -1){
|
||||
commentStr="";
|
||||
if((","+region.getConfigPortPattern()+",").indexOf(",1,") > -1){
|
||||
commentStr=commentStr+"0["+msgProp.getProperty("val_src_port")+"]"+"\n";
|
||||
}
|
||||
if((","+region.getConfigPortPattern()+",").indexOf(",2,") > -1){
|
||||
commentStr=commentStr+"0/65535["+msgProp.getProperty("val_src_port_mask")+"]"+"\n";
|
||||
}
|
||||
}else{
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}
|
||||
}
|
||||
}
|
||||
//导入的Protocol
|
||||
if("protocol".equals(headerStr)){
|
||||
if(StringUtil.isEmpty(region.getConfigProtocol())){
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}else{
|
||||
commentStr="";
|
||||
List<SysDataDictionaryItem> protocol=DictUtils.getDictList("PROTOCOL");
|
||||
if(protocol !=null && protocol.size()>0){
|
||||
for (SysDataDictionaryItem sysDataDictionaryItem : protocol) {
|
||||
if((","+region.getConfigProtocol()+",").indexOf(","+sysDataDictionaryItem.getItemCode()+",") >0){
|
||||
if(StringUtil.isEmpty(msgProp.getProperty(sysDataDictionaryItem.getItemValue()))){
|
||||
commentStr=commentStr+sysDataDictionaryItem.getItemCode()+":"+sysDataDictionaryItem.getItemValue()+"\n";
|
||||
}else{
|
||||
commentStr=commentStr+sysDataDictionaryItem.getItemCode()+":"+msgProp.getProperty(sysDataDictionaryItem.getItemValue())+"\n";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//导入的Direction
|
||||
if("direction".equals(headerStr)){
|
||||
if(StringUtil.isEmpty(region.getConfigProtocol())){
|
||||
headerStr="";
|
||||
commentStr="";
|
||||
}else{
|
||||
commentStr="";
|
||||
List<SysDataDictionaryItem> direction=DictUtils.getDictList("DIRECTION");
|
||||
if(direction !=null && direction.size()>0){
|
||||
for (SysDataDictionaryItem sysDataDictionaryItem : direction) {
|
||||
if(StringUtil.isEmpty(msgProp.getProperty(sysDataDictionaryItem.getItemValue()))){
|
||||
commentStr=commentStr+sysDataDictionaryItem.getItemCode()+":"+sysDataDictionaryItem.getItemValue()+"\n";
|
||||
}else{
|
||||
commentStr=commentStr+sysDataDictionaryItem.getItemCode()+":"+msgProp.getProperty(sysDataDictionaryItem.getItemValue())+"\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
titleInfo[0]=headerStr;
|
||||
titleInfo[1]=commentStr;
|
||||
return titleInfo;
|
||||
}
|
||||
/**
|
||||
* //递归获取cls实体对象及父级对象的method
|
||||
* @param list
|
||||
@@ -225,18 +397,116 @@ public class ExportExcel {
|
||||
});
|
||||
// Initialize
|
||||
List<String> headerList = Lists.newArrayList();
|
||||
List<String> commentList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String t = ((ExcelField)os[0]).title();
|
||||
String titleStr = ((ExcelField)os[0]).title();
|
||||
String commentStr = ((ExcelField)os[0]).comment();
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
String[] ss = StringUtils.split(t, "**", 2);
|
||||
if (ss.length==2){
|
||||
t = ss[0];
|
||||
commentStr="";
|
||||
}
|
||||
headerList.add(titleStr);
|
||||
commentList.add(commentStr);
|
||||
}
|
||||
initialize(title, headerList,commentList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
* @param FunctionRegionDict region信息
|
||||
* @param msgProp 国际化配置
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param cls 实体对象,通过annotation.ExportField获取标题
|
||||
* @param type 导出类型(1:导出数据;2:导出模板)
|
||||
* @param groups 导入分组
|
||||
*/
|
||||
public ExportExcel(FunctionRegionDict region,Properties msgProp,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)){
|
||||
for (Field f : list){
|
||||
ExcelField ef = f.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, f});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
}
|
||||
}
|
||||
headerList.add(t);
|
||||
}
|
||||
initialize(title, headerList);
|
||||
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);
|
||||
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();
|
||||
List<String> commentList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String titleStr = ((ExcelField)os[0]).title();
|
||||
String commentStr = ((ExcelField)os[0]).comment();
|
||||
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
commentStr="";
|
||||
}else{
|
||||
//判断此业务导出模板内容,设置导入提示信息
|
||||
String[] titleInfo=validRegionFieldAndSetComment(titleStr,commentStr,region,msgProp);
|
||||
titleStr=titleInfo[0];
|
||||
commentStr=titleInfo[1];
|
||||
}
|
||||
if(!StringUtil.isEmpty(titleStr)){
|
||||
headerList.add(msgProp.getProperty(titleStr)==null?titleStr:msgProp.getProperty(titleStr));
|
||||
commentList.add(commentStr);
|
||||
}
|
||||
|
||||
}
|
||||
initialize(title, headerList,commentList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
@@ -283,9 +553,6 @@ public class ExportExcel {
|
||||
if(!StringUtil.isEmpty(ms)){
|
||||
for (Method m : ms){
|
||||
ExcelField ef = m.getAnnotation(ExcelField.class);
|
||||
if(m.getName().equals("getSrcIpAddress")) {
|
||||
System.out.println(ef.title());
|
||||
}
|
||||
if (ef != null && (ef.type()==0 || ef.type()==type)){
|
||||
if (groups!=null && groups.length>0){
|
||||
boolean inGroup = false;
|
||||
@@ -316,18 +583,22 @@ public class ExportExcel {
|
||||
});
|
||||
// Initialize
|
||||
List<String> headerList = Lists.newArrayList();
|
||||
List<String> commentList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String t = ((ExcelField)os[0]).title();
|
||||
String titleStr = ((ExcelField)os[0]).title();
|
||||
String commentStr = ((ExcelField)os[0]).comment();
|
||||
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
String[] ss = StringUtils.split(t, "**", 2);
|
||||
if (ss.length==2){
|
||||
t = ss[0];
|
||||
}
|
||||
commentStr="";
|
||||
}
|
||||
headerList.add(msgProp.getProperty(t)==null?t:msgProp.getProperty(t));
|
||||
if(!StringUtil.isEmpty(titleStr)){
|
||||
headerList.add(msgProp.getProperty(titleStr)==null?titleStr:msgProp.getProperty(titleStr));
|
||||
commentList.add(commentStr);
|
||||
}
|
||||
|
||||
}
|
||||
initialize(title, headerList);
|
||||
initialize(title, headerList,commentList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
@@ -347,34 +618,39 @@ public class ExportExcel {
|
||||
if(!StringUtil.isEmpty(list)){
|
||||
for (Field f : list){
|
||||
ExcelField ef = f.getAnnotation(ExcelField.class);
|
||||
/*if(ef==null || StringUtil.isEmpty(ef.title())){
|
||||
break;
|
||||
}
|
||||
String titleP=StringUtil.isEmpty(msgProp.get(ef.title())) ? ef.title().toString():msgProp.get(ef.title()).toString();
|
||||
boolean export=false;
|
||||
for(String column:cloumnArray) {
|
||||
if(ef!=null&&column.equals(ef.title())) {
|
||||
|
||||
if(ef!=null&&column.equals(titleP)) {
|
||||
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){
|
||||
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;
|
||||
}
|
||||
for (int efg : ef.groups()){
|
||||
if (g == efg){
|
||||
inGroup = true;
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
}else{
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
}
|
||||
/*}*/
|
||||
}
|
||||
}
|
||||
List<Method> ms=new ArrayList<Method>();
|
||||
@@ -423,43 +699,46 @@ public class ExportExcel {
|
||||
});
|
||||
// Initialize
|
||||
List<String> headerList = Lists.newArrayList();
|
||||
List<String> commentList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String t = ((ExcelField)os[0]).title();
|
||||
String titleStr = ((ExcelField)os[0]).title();
|
||||
String commentStr = ((ExcelField)os[0]).comment();
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
String[] ss = StringUtils.split(t, "**", 2);
|
||||
if (ss.length==2){
|
||||
t = ss[0];
|
||||
}
|
||||
commentStr="";
|
||||
}
|
||||
headerList.add(msgProp.getProperty(t)==null?t:msgProp.getProperty(t));
|
||||
headerList.add(msgProp.getProperty(titleStr)==null?titleStr:msgProp.getProperty(titleStr));
|
||||
commentList.add(commentStr);
|
||||
}
|
||||
initialize(title, headerList);
|
||||
initialize(title, headerList,commentList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param headers 表头数组
|
||||
*/
|
||||
public ExportExcel(String title, String[] headers) {
|
||||
initialize(title, Lists.newArrayList(headers));
|
||||
public ExportExcel(String title, String[] headers, String[] comments) {
|
||||
initialize(title, Lists.newArrayList(headers), Lists.newArrayList(comments));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param headerList 表头列表
|
||||
* @param commentList 注释列表
|
||||
*/
|
||||
public ExportExcel(String title, List<String> headerList) {
|
||||
initialize(title, headerList);
|
||||
public ExportExcel(String title, List<String> headerList, List<String> commentList) {
|
||||
initialize(title, headerList,commentList);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param headerList 表头列表
|
||||
*/
|
||||
private void initialize(String title, List<String> headerList) {
|
||||
private void initialize(String title, List<String> headerList, List<String> commentList) {
|
||||
this.wb = new SXSSFWorkbook(500);
|
||||
this.sheet = wb.createSheet("Export");
|
||||
this.styles = createStyles(wb);
|
||||
@@ -482,12 +761,12 @@ public class ExportExcel {
|
||||
for (int i = 0; i < headerList.size(); i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
cell.setCellStyle(styles.get("header"));
|
||||
String[] ss = StringUtils.split(headerList.get(i), "**", 2);
|
||||
if (ss.length==2){
|
||||
cell.setCellValue(ss[0]);
|
||||
String commentStr = commentList.get(i);
|
||||
if (!StringUtil.isEmpty(commentStr)){
|
||||
cell.setCellValue(headerList.get(i));
|
||||
Comment comment = this.sheet.createDrawingPatriarch().createCellComment(
|
||||
new XSSFClientAnchor(0, 0, 0, 0, (short) i, 0, (short) i+4, 3));
|
||||
comment.setString(new XSSFRichTextString(ss[1]));
|
||||
comment.setString(new XSSFRichTextString(commentStr));
|
||||
cell.setCellComment(comment);
|
||||
}else{
|
||||
cell.setCellValue(headerList.get(i));
|
||||
@@ -574,6 +853,15 @@ public class ExportExcel {
|
||||
public Row addRow(){
|
||||
return sheet.createRow(rownum++);
|
||||
}
|
||||
/**
|
||||
* 添加一行
|
||||
* @return 行对象
|
||||
*/
|
||||
public Row addRow(Sheet sheet,Integer rownum,String key){
|
||||
rownum++;
|
||||
rownums.put(key, rownum);
|
||||
return sheet.createRow(rownum);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -612,9 +900,8 @@ public class ExportExcel {
|
||||
} else if (val instanceof Float) {
|
||||
cell.setCellValue((Float) val);
|
||||
} else if (val instanceof Date) {
|
||||
DataFormat format = wb.createDataFormat();
|
||||
style.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
cell.setCellValue((Date) val);
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
cell.setCellValue(sdf.format(val));
|
||||
} else {
|
||||
if (fieldType != Class.class){
|
||||
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
|
||||
@@ -681,6 +968,80 @@ public class ExportExcel {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 添加数据(通过annotation.ExportField添加数据)
|
||||
* @return list 数据列表
|
||||
*/
|
||||
public <E> ExportExcel setDataList(Properties msgProp, Map<String,List> dataMap, Map<Object,Object> map){
|
||||
Set<String> keyList=dataMap.keySet();
|
||||
for (String key : keyList) {
|
||||
List<E> list=dataMap.get(key);
|
||||
for (E e : list){
|
||||
int colunm = 0;
|
||||
Row row = this.addRow(sheets.get(key),rownums.get(key),key);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object[] os : annotationMap.get(key)){
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
if(ef.title().equals("is_hex") && !StringUtil.isEmpty(val)){
|
||||
Integer isHex=Integer.parseInt(val.toString());
|
||||
if(isHex.equals(0) || isHex.equals(2)){
|
||||
val = msgProp.getProperty("not_hex","not_hex");
|
||||
}else if(isHex.equals(1)){
|
||||
val = msgProp.getProperty("hex","hex");
|
||||
}else{
|
||||
val ="";
|
||||
}
|
||||
}
|
||||
if(ef.title().equals("is_case_insenstive") && !StringUtil.isEmpty(val)){
|
||||
Integer isCaseSenstive=Integer.parseInt(val.toString());
|
||||
if(isCaseSenstive.equals(0) || isCaseSenstive.equals(1)){
|
||||
val = msgProp.getProperty("case_insenstive","case_insenstive");
|
||||
}else if(isCaseSenstive.equals(1)){
|
||||
val = msgProp.getProperty("case_senstive","case_senstive");
|
||||
}else{
|
||||
val ="";
|
||||
}
|
||||
}
|
||||
}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 数据列表
|
||||
@@ -795,6 +1156,177 @@ public class ExportExcel {
|
||||
}
|
||||
return basicInfo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/************************************************************/
|
||||
/**
|
||||
* 构造函数
|
||||
* @param msgProp 国际化配置
|
||||
* @param title sheet名称
|
||||
* @param cls 实体对象,通过annotation.ExportField获取标题
|
||||
* @param type 导出类型(1:导出数据;2:导出模板)
|
||||
* @param groups 导入分组
|
||||
* @return
|
||||
*/
|
||||
public ExportExcel(Properties msgProp,List<String> titleList,Map<String,String> noExportMap,Map<String, Class<?>> clsMap, int type, int... groups){
|
||||
Map<String, List<String>> headerMap=new HashMap<String, List<String>>();
|
||||
Map<String, List<String>> commentMap=new HashMap<String, List<String>>();
|
||||
for (String title : titleList) {
|
||||
String noExportField=noExportMap.get(title);
|
||||
List<Object[]> annotationList = Lists.newArrayList();
|
||||
List<Field> list=new ArrayList<Field>();
|
||||
// Get annotation field
|
||||
//递归获取cls实体对象及父级对象的属性
|
||||
getFields(list, clsMap.get(title));
|
||||
if(!StringUtil.isEmpty(list)){
|
||||
for (Field f : list){
|
||||
ExcelField ef = f.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;
|
||||
if(!StringUtil.isEmpty(ef.title()) && !(noExportField.indexOf(","+ef.title()+",") > -1)){
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(!StringUtil.isEmpty(ef.title()) && !(noExportField.indexOf(","+ef.title()+",") > -1)){
|
||||
annotationList.add(new Object[]{ef, f});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<Method> ms=new ArrayList<Method>();
|
||||
// Get annotation method
|
||||
//递归获取cls实体对象及父级对象的属性
|
||||
getMethods(ms, clsMap.get(title));
|
||||
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;
|
||||
if(!StringUtil.isEmpty(ef.title()) && !(noExportField.indexOf(","+ef.title()+",") > -1)){
|
||||
annotationList.add(new Object[]{ef, m});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(!StringUtil.isEmpty(ef.title()) && !(noExportField.indexOf(","+ef.title()+",") > -1)){
|
||||
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();
|
||||
List<String> commentList = Lists.newArrayList();
|
||||
for (Object[] os : annotationList){
|
||||
String titleStr = ((ExcelField)os[0]).title();
|
||||
String commentStr = ((ExcelField)os[0]).comment();
|
||||
|
||||
// 如果是导出,则去掉注释
|
||||
if (type==1){
|
||||
commentStr="";
|
||||
}
|
||||
if(!StringUtil.isEmpty(titleStr)){
|
||||
//去掉不需要展示的header
|
||||
if(!(noExportField.indexOf(","+titleStr+",") > -1)){
|
||||
titleStr=msgProp.getProperty(titleStr)==null?titleStr:msgProp.getProperty(titleStr);
|
||||
headerList.add(titleStr);
|
||||
commentList.add(commentStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
headerMap.put(title, headerList);
|
||||
commentMap.put(title, commentList);
|
||||
annotationMap.put(title, annotationList);
|
||||
}
|
||||
initializeMultiSheet(msgProp,titleList, headerMap,commentMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param headerList 表头列表
|
||||
*/
|
||||
private void initializeMultiSheet(Properties msgProp,List<String> titleList,Map<String, List<String>> headerMap,Map<String,List<String>> commentMap) {
|
||||
this.wb = new SXSSFWorkbook(500);
|
||||
int j=0;
|
||||
sheets=new HashMap<>();
|
||||
rownums=new HashMap<>();
|
||||
for (String title : titleList) {
|
||||
List<String> headerList=headerMap.get(title);
|
||||
List<String> commentList=commentMap.get(title);
|
||||
Sheet sheet = wb.createSheet(StringUtil.isEmpty(msgProp.get(title)) ? title:msgProp.get(title).toString());
|
||||
rownums.put(title, 0);
|
||||
sheets.put(title, sheet);
|
||||
this.styles = createStyles(wb);
|
||||
// Create header
|
||||
if (headerList == null){
|
||||
throw new RuntimeException("headerList not null!");
|
||||
}
|
||||
Integer rownum=rownums.get(title);
|
||||
Row headerRow = sheet.createRow(rownum++);
|
||||
headerRow.setHeightInPoints(16);
|
||||
for (int i = 0; i < headerList.size(); i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
cell.setCellStyle(styles.get("header"));
|
||||
String commentStr = commentList.get(i);
|
||||
if (!StringUtil.isEmpty(commentStr)){
|
||||
cell.setCellValue(headerList.get(i));
|
||||
Comment comment = sheet.createDrawingPatriarch().createCellComment(
|
||||
new XSSFClientAnchor(0, 0, 0, 0, (short) i, 0, (short) i+4, 3));
|
||||
comment.setString(new XSSFRichTextString(commentStr));
|
||||
cell.setCellComment(comment);
|
||||
}else{
|
||||
cell.setCellValue(headerList.get(i));
|
||||
}
|
||||
sheet.autoSizeColumn(i);
|
||||
}
|
||||
for (int i = 0; i < headerList.size(); i++) {
|
||||
int colWidth = sheet.getColumnWidth(i)*2;
|
||||
sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
log.debug("Initialize success.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 输出数据流
|
||||
* @param os 输出数据流
|
||||
|
||||
Reference in New Issue
Block a user