SpringMVC 使用poi导出excel简单小例子

不说话,上代码。

student.java

package com.cn.hnust.domain;

import java.io.Serializable;

public class Student implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Integer id;

    private String name;

    private String birthday;

    private Short sex;

    private String photoUrl;

    private Integer classId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }


    public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public Short getSex() {
        return sex;
    }

    public void setSex(Short sex) {
        this.sex = sex;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl == null ? null : photoUrl.trim();
    }

    public Integer getClassId() {
        return classId;
    }

    public void setClassId(Integer classId) {
        this.classId = classId;
    }
}

controller层(中间有点错误,不影响输出)
@RequestMapping("exportExcel")
	 public void  exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{
	        HSSFWorkbook wb = new HSSFWorkbook();  
	        HSSFSheet sheet = wb.createSheet("学生表一");  
	        HSSFRow row = sheet.createRow(0);  
	        HSSFCellStyle style = wb.createCellStyle();  
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
	  
	        HSSFCell cell = row.createCell(0);  
	        cell.setCellValue("学号");  
	        cell.setCellStyle(style);  
	        cell = row.createCell( 1);  
	        cell.setCellValue("姓名");  
	        cell.setCellStyle(style);  
	        cell = row.createCell(2);  
	        cell.setCellValue("年龄");  
	        cell.setCellStyle(style);  
	        cell = row.createCell(3);  
	        cell.setCellValue("班级");  
	        cell.setCellStyle(style);  
	        cell = row.createCell(4);  
	        cell.setCellValue("url");  
	        cell.setCellStyle(style);  
	  
	        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
	        List<Student> list = getStudent();  
	  
	        for (int i = 0; i < list.size(); i++)  
	        {  
	            row = sheet.createRow((int) i + 1);  
	            // 第四步,创建单元格,并设置值  
	            row.createCell(0).setCellValue(list.get(i).getId());
	            row.createCell(1).setCellValue(list.get(i).getName());
	            row.createCell(2).setCellValue(list.get(i).getBirthday());
	            row.createCell(3).setCellValue(list.get(i).getClassId());
	            row.createCell(4).setCellValue(list.get(i).getPhotoUrl());
	            row.createCell(5).setCellValue(list.get(i).getSex());
	        }  
	        // 第六步,将文件存到指定位置  
	        try  
	        {  
	        	String fileName = "Excel.xls";
	        	fileName = URLEncoder.encode(fileName, "UTF-8");
	        	response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");  
	        	response.setContentType("application/vnd.ms-excel;charset=UTF-8");  
	           OutputStream fout =  response.getOutputStream();
	            wb.write(fout);  
	            fout.flush();       
	            fout.close();  
	        }  
	        catch (Exception e){  
	            e.printStackTrace();  
	        }  
	    }  
	  private  List<Student> getStudent() throws Exception  
	    {  
	        List<Student> list = new List<Student>();  
	  
	        Student user1 = new Student();  
	        user1.setId(1);
	        user1.setName("ad");
	        user1.setBirthday("1992");
	        user1.setPhotoUrl("12");
	        user1.setSex((short) 12);
	        user1.setClassId(1);
	        list.add(user1);  
	  
	        return list;  
	    }  

jsp。

function exportExcel(){
		window.location.href=pageUrl+"/student/exportExcel";
	}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值