e代剑客——温柔一刀

生活就像海洋,只有意志坚强的人,才能到达彼岸

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  76 随笔 :: 7 文章 :: 215 评论 :: 0 Trackbacks
ExcelBean.java文件用于生成Excel

package com.zhupan.util;

import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.ctgusec.model.Course_info;
import com.ctgusec.model.Student_info;

public class ExcelBean {

    
public String expordExcel(OutputStream os, List courseList,List studentList)
            
throws Exception {

        WritableWorkbook wbook 
= Workbook.createWorkbook(os); // 建立excel文件
        String tmptitle = "课程“"+((Course_info)courseList.get(0)).getCource_name()+"”的选课学生列表"// 标题
        WritableSheet wsheet = wbook.createSheet("第一页"0); // sheet名称
        
// 设置excel标题
        WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
                WritableFont.BOLD, 
false, UnderlineStyle.NO_UNDERLINE,
                Colour.BLACK);
        WritableCellFormat wcfFC 
= new WritableCellFormat(wfont);
        wsheet.addCell(
new Label(10, tmptitle, wcfFC));
        wfont 
= new jxl.write.WritableFont(WritableFont.ARIAL, 14,
                WritableFont.BOLD, 
false, UnderlineStyle.NO_UNDERLINE,
                Colour.BLACK);
        wcfFC 
= new WritableCellFormat(wfont);
        
// 开始生成主体内容                
        wsheet.addCell(new Label(02"课程名称"));
        wsheet.addCell(
new Label(12"学 号"));
        wsheet.addCell(
new Label(22"姓 名"));
        wsheet.addCell(
new Label(32"性 别"));
        wsheet.addCell(
new Label(42"学 院"));
        wsheet.addCell(
new Label(52"班 级"));
        wsheet.addCell(
new Label(62"专 业"));
        wsheet.addCell(
new Label(72"备 注"));
        
for(int i=3;i<studentList.size()+3;i++)
        
{
            wsheet.addCell(
new Label(0, i, ((Course_info)courseList.get(0)).getCource_name()));
            wsheet.addCell(
new Label(1, i, ((Student_info)studentList.get(0)).getStudentID()));
            wsheet.addCell(
new Label(2, i, ((Student_info)studentList.get(0)).getName()));
            wsheet.addCell(
new Label(3, i, ((Student_info)studentList.get(0)).getSex()));
            wsheet.addCell(
new Label(4, i, ((Student_info)studentList.get(0)).getUnit()));
            wsheet.addCell(
new Label(5, i, ((Student_info)studentList.get(0)).getClass_()));
            wsheet.addCell(
new Label(6, i, ((Student_info)studentList.get(0)).getSpecialty()));
            wsheet.addCell(
new Label(7, i, ((Student_info)studentList.get(0)).getRemark()));
        }
        
        
// 主体内容生成结束        
        wbook.write(); // 写入文件
        wbook.close();
        os.close();
        
return "success";
    }

}


控制器:

package com.ctgusec.spring;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ctgusec.service.ICourse_infoManage;
import com.zhupan.util.ExcelBean;

public class EExcelDownController extends AbstractController {
    
    
private ICourse_infoManage courseManage;

    
public void setCourseManage(ICourse_infoManage courseManage) {
        
this.courseManage = courseManage;
    }


    @Override
    
protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) 
throws Exception {    
        Integer course_id
=new Integer(request.getParameter("course_id"));
        List courseList
=this.courseManage.getCourseById(course_id);        
        List studentList 
= this.courseManage.getStudentByCourseId(course_id);
         
try {    
            OutputStream os 
= response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            response.setHeader("Content-disposition""attachment; filename=student.xls");// 设定输出文件头
            response.setContentType("application/msexcel");// 定义输出类型
            ExcelBean excelBean = new ExcelBean();
            excelBean.expordExcel(os,courseList,studentList);
// 调用生成excel文件bean
        }
 catch (Exception e) {
            System.out.println(e);
        }
            
        
return null;    
    }

}

posted on 2006-10-08 18:12 温柔一刀 阅读(1966) 评论(4)  编辑  收藏 所属分类: 开源框架

评论

# re: sping 、jxl 生成excel文件下载 2006-10-27 15:27 junitfans
thanks   回复  更多评论
  

# re: sping 、jxl 生成excel文件下载 2006-10-27 15:28 junitfans
非常感谢,正需要  回复  更多评论
  

# re: sping 、jxl 生成excel文件下载 2008-03-16 16:14 杨晓满
网上发了那么多下载Excel文件的例子,只有楼主这个看的最明白了,谢谢楼主啊.有机会向你多多请教.  回复  更多评论
  

# re: sping 、jxl 生成excel文件下载 2009-10-19 14:35 javas
对我很有帮助! 谢谢  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航:
 
联系偶 zhupanjava@gmail.com 温柔一刀