不说话,上代码。
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";
}