sping 、jxl 生成excel文件下载

ExcelBean.java文件用于生成Excel

java 代码
  1. package com.zhupan.util;   
  2.   
  3. import java.io.OutputStream;   
  4. import java.util.List;   
  5.   
  6. import jxl.Workbook;   
  7. import jxl.format.Colour;   
  8. import jxl.format.UnderlineStyle;   
  9. import jxl.write.Label;   
  10. import jxl.write.WritableCellFormat;   
  11. import jxl.write.WritableFont;   
  12. import jxl.write.WritableSheet;   
  13. import jxl.write.WritableWorkbook;   
  14.   
  15. import com.ctgusec.model.Course_info;   
  16. import com.ctgusec.model.Student_info;   
  17.   
  18. public class ExcelBean {   
  19.   
  20.     public String expordExcel(OutputStream os, List courseList, List studentList)   
  21.             throws Exception {   
  22.   
  23.         WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件   
  24.         String tmptitle = "课程“"  
  25.                 + ((Course_info) courseList.get(0)).getCource_name()   
  26.                 + "”的选课学生列表"// 标题   
  27.         WritableSheet wsheet = wbook.createSheet("第一页"0); // sheet名称   
  28.         // 设置excel标题   
  29.         WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,   
  30.                 WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,   
  31.                 Colour.BLACK);   
  32.         WritableCellFormat wcfFC = new WritableCellFormat(wfont);   
  33.         wsheet.addCell(new Label(10, tmptitle, wcfFC));   
  34.         wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,   
  35.                 WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,   
  36.                 Colour.BLACK);   
  37.         wcfFC = new WritableCellFormat(wfont);   
  38.         // 开始生成主体内容   
  39.         wsheet.addCell(new Label(02"课程名称"));   
  40.         wsheet.addCell(new Label(12"学 号"));   
  41.         wsheet.addCell(new Label(22"姓 名"));   
  42.         wsheet.addCell(new Label(32"性 别"));   
  43.         wsheet.addCell(new Label(42"学 院"));   
  44.         wsheet.addCell(new Label(52"班 级"));   
  45.         wsheet.addCell(new Label(62"专 业"));   
  46.         wsheet.addCell(new Label(72"备 注"));   
  47.         for (int i = 3; i < studentList.size() + 3; i++) {   
  48.             wsheet.addCell(new Label(0, i, ((Course_info) courseList.get(0))   
  49.                     .getCource_name()));   
  50.             wsheet.addCell(new Label(1, i, ((Student_info) studentList.get(0))   
  51.                     .getStudentID()));   
  52.             wsheet.addCell(new Label(2, i, ((Student_info) studentList.get(0))   
  53.                     .getName()));   
  54.             wsheet.addCell(new Label(3, i, ((Student_info) studentList.get(0))   
  55.                     .getSex()));   
  56.             wsheet.addCell(new Label(4, i, ((Student_info) studentList.get(0))   
  57.                     .getUnit()));   
  58.             wsheet.addCell(new Label(5, i, ((Student_info) studentList.get(0))   
  59.                     .getClass_()));   
  60.             wsheet.addCell(new Label(6, i, ((Student_info) studentList.get(0))   
  61.                     .getSpecialty()));   
  62.             wsheet.addCell(new Label(7, i, ((Student_info) studentList.get(0))   
  63.                     .getRemark()));   
  64.         }   
  65.         // 主体内容生成结束   
  66.         wbook.write(); // 写入文件   
  67.         wbook.close();   
  68.         os.close();   
  69.         return "success";   
  70.     }   
  71. }  

控制器:

java 代码
  1. package com.ctgusec.spring;   
  2.   
  3. import java.io.OutputStream;   
  4. import java.util.List;   
  5.   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import javax.servlet.http.HttpServletResponse;   
  8.   
  9. import org.springframework.web.servlet.ModelAndView;   
  10. import org.springframework.web.servlet.mvc.AbstractController;   
  11.   
  12. import com.ctgusec.service.ICourse_infoManage;   
  13. import com.zhupan.util.ExcelBean;   
  14.   
  15. public class EExcelDownController extends AbstractController {   
  16.   
  17.     private ICourse_infoManage courseManage;   
  18.   
  19.     public void setCourseManage(ICourse_infoManage courseManage) {   
  20.         this.courseManage = courseManage;   
  21.     }   
  22.   
  23.     @Override  
  24.     protected ModelAndView handleRequestInternal(HttpServletRequest request,   
  25.             HttpServletResponse response) throws Exception {   
  26.         Integer course_id = new Integer(request.getParameter("course_id"));   
  27.         List courseList = this.courseManage.getCourseById(course_id);   
  28.         List studentList = this.courseManage.getStudentByCourseId(course_id);   
  29.         try {   
  30.             OutputStream os = response.getOutputStream();// 取得输出流   
  31.             response.reset();// 清空输出流   
  32.             response.setHeader("Content-disposition",   
  33.                     "attachment; filename=student.xls");// 设定输出文件头   
  34.             response.setContentType("application/msexcel");// 定义输出类型   
  35.             ExcelBean excelBean = new ExcelBean();   
  36.             excelBean.expordExcel(os, courseList, studentList);// 调用生成excel文件bean   
  37.         } catch (Exception e) {   
  38.             System.out.println(e);   
  39.         }   
  40.         return null;   
  41.     }   
  42. }  

你可能感兴趣的:(spring,mvc,servlet,Excel,OS)