java利用jxl导出excel表格
不多说,直接上代码
1、实体类Student
package com.model;
public class Student {
private int id;
private String userName;
private int age;
public Student() {
}
public Student(int id, String userName, int age) {
this.id = id;
this.userName = userName;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [age=" + age + ", id=" + id + ", userName=" + userName
+ "]";
}
}
2、数据访问对象类(采用List模拟数据库)StudentDao
package com.dao;
import java.util.ArrayList;
import java.util.List;
import com.model.Student;
/**
* @author kangkang.tao
*/
public class StudentDao {
public final static List<Student> allStudents = new ArrayList<Student>();
// 初始化数据,相当于数据库
static{
allStudents.add(new Student(1,"康康",23));
allStudents.add(new Student(2,"积木",12));
allStudents.add(new Student(3,"凯南",15));
allStudents.add(new Student(4,"gina",16));
allStudents.add(new Student(5,"jim",10));
allStudents.add(new Student(6,"tamy",14));
allStudents.add(new Student(7,"anni",20));
allStudents.add(new Student(8,"pile",10));
allStudents.add(new Student(9,"amy",23));
allStudents.add(new Student(10,"temy",22));
allStudents.add(new Student(11,"kill",15));
allStudents.add(new Student(12,"jane",16));
allStudents.add(new Student(13,"kang",17));
}
// 查询所有
public List<Student> getAll(){
return allStudents;
}
}
3、servlet
package com.servlet;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.dao.StudentDao;
import com.model.Student;
public class ExportServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentDao dao = new StudentDao();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename=studentList.xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
WritableWorkbook book = null;
try {
// 打开文件
book = Workbook.createWorkbook(os);
WritableCellFormat wcf = new WritableCellFormat();
wcf.setAlignment(jxl.format.Alignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
wcf.setBackground(Colour.GREY_25_PERCENT);
// 生成名为"学生"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("学生基本信息表", 0);
// 读取学生的数据信息
List<Student> studentList = dao.getAll();
Label head1 = new Label(0, 0, "编号");
head1.setCellFormat(wcf);
sheet.addCell(head1);
Label head2 = new Label(1, 0, "姓名");
head1.setCellFormat(wcf);
sheet.addCell(head2);
Label head3 = new Label(2, 0, "年龄");
head1.setCellFormat(wcf);
sheet.addCell(head3);
if(studentList != null && !studentList.isEmpty()){
for (int i = 0; i < studentList.size(); i++) {
sheet.addCell(new Label(0, i+1, String.valueOf(studentList.get(i).getId())));
sheet.addCell(new Label(1, i+1, studentList.get(i).getUserName()));
sheet.addCell(new Label(2, i+1, String.valueOf(studentList.get(i).getAge())));
}
}
// 写入数据并关闭文件
book.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
4、web.xml
<!-- 导出数据:配置export.do -->
<servlet>
<servlet-name>exportServlet</servlet-name>
<servlet-class>com.servlet.ExportServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exportServlet</servlet-name>
<url-pattern>/export.do</url-pattern>
</servlet-mapping>
5、jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="export.do">导出EXCEL</a>
</body>
</html>