注意事项:
1.excel创建和下载都用到流,所以要注意流冲突
2.导出文件的时候如果用到map集合,导出的数据会乱序,需要进一步处理
@RequestMapping("/xxx/exportExcels.do")
public String exportExcel(HttpServletRequest request,
HttpServletResponse response, ModelMap model,String replyStartTime,String replyEndTime) throws Exception {
//接受数据库数据的集合
Map<String, List<String>> memberMap = getMember(request, response, replyStartTime, replyEndTime);
//xinzeng
//处理后的map集合
Map<String, List<String>> myMap = new LinkedHashMap<String, List<String>>();
List<String> keyList = new ArrayList<String>();
//获取key值放到list中
Iterator<String> it =memberMap.keySet().iterator();
while(it.hasNext()){
keyList.add(it.next());
}
Collections.sort(keyList);
//修改keyList为int并顺序排序
List<Integer> keyLists = new ArrayList<Integer>();
for (String str : keyList) {
Integer valueOf = Integer.valueOf(str);
keyLists.add(valueOf);
}
//对list进行排序
//Collections.sort(keyLists);
//
Iterator<Integer> it2 = keyLists.iterator();
while(it2.hasNext()){
String key =Integer.valueOf(it2.next()).toString();
myMap.put(key, memberMap.get(key));
}
//修改
//xinzeng
String[] strArray = excelTitle();
BaseAppUtils.createExcel(request, response, myMap, strArray);
BaseAppUtils.createExcels(request, response, myMap, strArray);
//BaseAppUtils.responseStr(response, "导出成功");
return "redirect:v_register.do";
}
public static String[] excelTitle() {
String[] strArray = { "序号", "姓名", "邮箱", "电话","描述","类型" };
return strArray;
}
工具
public Map<String, List<String>> getMember(HttpServletRequest request,
HttpServletResponse response,String replyStartTime,String replyEndTime) throws Exception {
List<Member> list = new ArrayList<Member>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
//SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
List<UserRegister> u = userRegisterMng.findByTime(replyStartTime,replyEndTime);
if(u==null){
BaseAppUtils.responseStrError(response, "无数据");
}else{
for (int i = 0; i < u.size(); i++) {
Member user1 = new Member();
user1.setCode(u.get(i).getId());
user1.setName(u.get(i).getName()+"");
user1.setEmail(u.get(i).getEmails());
user1.setNumber(u.get(i).getNumber());
user1.setDesc(u.get(i).getDescription());
int typeId = u.get(i).getTypeId();
if(typeId==1){
user1.setType("");
}else if(typeId==2){
user1.setType("");
}else if(typeId==3){
user1.setType("");
}else if(typeId==4){
user1.setType("");
}else{
user1.setType("");
}
list.add(user1);
}
//BaseAppUtils.responseStr(response, "成功导出");
for (int i = 0; i < list.size(); i++) {
ArrayList<String> members = new ArrayList<String>();
members.add(list.get(i).getCode() + "");
members.add(list.get(i).getName());
members.add(list.get(i).getEmail() + "");
members.add(list.get(i).getNumber());
members.add(list.get(i).getDesc());
members.add(list.get(i).getType());
map.put(list.get(i).getCode() + "", members);
}
return map;
}
return map;
}
实体类
public class Member {
private Integer code;
private String name;
private String email;
private String number;
private String type;
private String desc;
public Member() {
super();
}
public Member(Integer code, String name, String email, String number,
String type) {
super();
this.code = code;
this.name = name;
this.email = email;
this.number = number;
this.type = type;
}
public Member(Integer code, String name, String email, String number,
String type, String desc) {
super();
this.code = code;
this.name = name;
this.email = email;
this.number = number;
this.type = type;
this.desc = desc;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}