1.提交表单
<form name="form1" id="form1" method="post" action="${pageContext.request.contextPath}/dor/dor_export.do" >
function excel(){
$("#form1").submit();
}
2.controller层
/**
* 导出数据
* */
@RequestMapping("/dor_export")
public ModelAndView export(ModelMap map,String fileName,String index,StudentBean stu,HttpSession session,HttpServletRequest request) throws Exception{
List<Map<String,String>> list =dormitoryService.selectAllAssetInlibraryInfo(stu,index);
//教师导出
if(index.equals("00d")){
String[] titles={"编号","学号","姓名","联系电话","寝室地址","寝室类型","寝室性质","剩余床位","寝室收费","住宿情况"};
ViewExcel excel=new ViewExcel(titles,fileName);
map.put("excelList", list);
return new ModelAndView(excel,map);
}//管理员导出
else if(index.equals("11d")){
String[] titles={"编号","学号","姓名","专业","班级","联系电话","寝室地址","寝室类型","寝室性质","剩余床位","寝室收费","住宿情况"};
ViewExcel excel=new ViewExcel(titles,fileName);
map.put("excelList", list);
return new ModelAndView(excel,map);
}
return null;
}
3.service层
/**
* 导出学生宿舍信息
* */
@Override
public List<Map<String, String>> selectAllAssetInlibraryInfo(StudentBean stu,String index) {
// TODO Auto-generated method stub
StudentBean[] list=null;
if(index.equals("00d")){//教师导出
list = dormitoryDao.getCheckDor(stu);
}else if(index.equals("11d")){//管理员导出
list=dormitoryDao.getAllCheckDor(stu);
}
List<StudentBean> ld = new ArrayList<StudentBean>();
for(int i=0;i<list.length;i++){
ld.add(list[i]);
}
List<Map<String, String>> mapList=new ArrayList<Map<String,String>>();
/* for (StudentBean assetInlibrary : ld) {*/
for(int j=0;j<ld.size();j++){
Map<String, String> map=new HashMap<String, String>();
int start=1;
map.put("编号",start+j+"" );
map.put("学号", ld.get(j).getStu_no());
map.put("姓名",ld.get(j).getStu_name() );
if(index.equals("11d")){
map.put("专业", ld.get(j).getMaj_name());
map.put("班级", ld.get(j).getClass_id());
}
map.put("联系电话", ld.get(j).getStu_phone());
map.put("寝室地址", ld.get(j).getDor_on());
String dor_max=ld.get(j).getDor_max()+"";
if(dor_max.equals("4")){
map.put("寝室类型", "4人间");
}else if(dor_max.equals("6")){
map.put("寝室类型", "6人间");
}else if(dor_max.equals("8")){
map.put("寝室类型", "8人间");
}
String sex=ld.get(j).getDor_sex()+"";
if(sex.equals("1"))
map.put("寝室性质", "男寝");
if(sex.equals("0"))
map.put("寝室性质", "女寝");
map.put("剩余床位", ld.get(j).getDor_surplus());
map.put("寝室收费", ld.get(j).getDor_money());
String ds_stu= ld.get(j).getDs_statu()+"";
if(ds_stu.equals("1")||ds_stu.equals("null"))
map.put("住宿情况", "待审核");
if(ds_stu.equals("null"))
map.put("住宿情况", "未选择");
if(ds_stu.equals("2"))
map.put("住宿情况", "已审核");
mapList.add(map);
}
return mapList;
}
5.工具类ViewExcel
package org.hgsf.util;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ViewExcel extends AbstractExcelView {
private String[] titles;
private String fileName;
//传入指定的标题头
public ViewExcel(String[] titles,String fileName) {
this.titles=titles;
this.fileName=fileName;
}
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获取数据
List<Map<String, String>> list = (List<Map<String, String>>) model.get("excelList");
//在workbook添加一个sheet
HSSFSheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth(15);
HSSFCell cell=null;
//遍历标题
for (int i = 0; i < titles.length; i++) {
//获取位置
cell = getCell(sheet, 0, i);
setText(cell, titles[i]);
}
//数据写出
for (int i = 0; i < list.size(); i++) {
//获取每一个map
Map<String, String> map=list.get(i);
//一个map一行数据
HSSFRow row = sheet.createRow(i+1);
for (int j = 0; j < titles.length; j++) {
//遍历标题,把key与标题匹配
String title=titles[j];
//判断该内容存在mapzhong
if(map.containsKey(title)){
row.createCell(j).setCellValue(map.get(title));
}
}
}
//设置下载时客户端Excel的名称
String filename = fileName+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode((filename),"UTF-8"));
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
}
模板下载
//点击下载模板按钮
this.onloadBtn = function(i){
var url="${pageContext.request.contextPath}/view//load/student.xls";//文件存放路径
window.open(url);
};