协议IP配置增加导入功能
This commit is contained in:
@@ -15,7 +15,9 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -161,7 +163,84 @@ public class ExportExcel {
|
||||
}
|
||||
initialize(title, headerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param cls 实体对象,通过annotation.ExportField获取标题
|
||||
* @param type 导出类型(1:导出数据;2:导出模板)
|
||||
* @param groups 导入分组
|
||||
*/
|
||||
public ExportExcel(Properties msgProp,String title, Class<?> cls, int type, int... groups){
|
||||
// Get annotation field
|
||||
Field[] fs = cls.getDeclaredFields();
|
||||
for (Field f : fs){
|
||||
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});
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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});
|
||||
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));
|
||||
}
|
||||
initialize(title, headerList);
|
||||
}
|
||||
/**
|
||||
* 构造函数
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
@@ -427,7 +506,28 @@ public class ExportExcel {
|
||||
this.write(os);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出到客户端
|
||||
* @param fileName 输出文件名
|
||||
*/
|
||||
public ExportExcel write(HttpServletRequest request,HttpServletResponse response, String fileName) throws IOException{
|
||||
|
||||
final String userAgent = request.getHeader("USER-AGENT");
|
||||
String finalFileName = null;
|
||||
if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
|
||||
finalFileName = Encodes.urlEncode(fileName);
|
||||
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
|
||||
finalFileName = new String(fileName.getBytes(), "ISO8859-1");
|
||||
}else{
|
||||
finalFileName = Encodes.urlEncode(fileName);//其他浏览器
|
||||
}
|
||||
|
||||
response.reset();
|
||||
response.setContentType("application/octet-stream; charset=utf-8");
|
||||
response.setHeader("Content-Disposition", "attachment; filename="+finalFileName);
|
||||
write(response.getOutputStream());
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 清理临时文件
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user