直接贴代码了,就不讲概念了。。。。
先列出项目工程结构:
1.先创建一个web项目,这里我使用maven构建
pom.xml文件需要导入的jar包
org.jxls
jxls
2.4.6
org.jxls
jxls-poi
1.0.15
org.jxls
jxls-jexcel
1.0.7
2.创建实体类,这里创建Employee类
private Integer id;
private String empName;
private Integer age;
private String address;
public Employee(){}
public Employee(Integer id, String empName, Integer age, String address) {
this.id = id;
this.empName = empName;
this.age = age;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
3.创建数据访问层类,这里就不连接数据库了,直接初始化数据。(创建EmpDao类)
public List getEmployee(){
List empList = new ArrayList();
Employee emp1 = new Employee(1,"张三",16,"xxx区");
Employee emp2 = new Employee(2,"李四",20,"xxxx家湾");
Employee emp3 = new Employee(3,"王五",36,"xxx区");
Employee emp4 = new Employee(4,"赵柳",39,"xxx北");
Employee emp5 = new Employee(5,"田七",21,"xxx区");
Employee emp6 = new Employee(6,"王八",42,"xx科技园");
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);
empList.add(emp6);
return empList;
}
4.创建Servlet类,处理数据导入
package com.kys.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import com.kys.dao.EmpDao;
public class EmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private EmpDao empDao = new EmpDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* InputStream is = EmployeeServlet.class.getResourceAsStream("/report/emp.xlsx")
* 这行代码主要是读取excel模块(excel模版下面在介绍),模版我放在根路径下 : /report/
*/
/**
* OutputStream os = new FileOutputStream("target/emp.xlsx")
* 主要导出的路径、路径存在maven工程下target文件下
*/
/**
* context.putVar("emps",empDao.getEmployee());
* 把获取到的数据保存到作用域中
*/
/**
* JxlsHelper.getInstance().processTemplate(is, os, context);
* 生成到excel表格中
*/
try(InputStream is = EmployeeServlet.class.getResourceAsStream("/report/emp.xlsx")) {
try (OutputStream os = new FileOutputStream("target/emp.xlsx")) {
Context context = new Context();
context.putVar("emps",empDao.getEmployee());
JxlsHelper.getInstance().processTemplate(is, os, context);
}
}
}
}
5.页面层index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Employee
导出
6.Excel模版我保存到src/main/resources/report目录下
下面介绍excel表格需要怎么写:
先看一下官网的解释:
简单的解释就是:
在excel表格中,比如name,birthDate,Payment,Bonus的数据区域为A1:D4
如何标明数据区域?
在A1单元格中点击鼠标右键,插入批注:写入 jx:area(lastCell="D4") 意思就是从插入批注的单元格到lastCell=D4都是属于数据区域
接着在A1单元格中循环保存到作用域的集合对象,写入 jx:each(items="emps" var="emp" lastCell="D4")
查看完整的批注:
那么 jx:area 和 jx:each分别是使用的是Apache JEXL表达式,和jsp中jstl表达式一样
excel表格使用${}获取bean对象的值,和jsp页面的差不多一样
最后运行项目,点击页面的导出将会得到如下数据:
导出的表格下载在maven项目下的target目录中:双击打开得到如下数据
以上将是所有步骤,只是一个简单的例子,需要其他功能还需要继续美化,加强
还有这是我第一次写的博客,大家有什么问题可以提出来,写的不是很好,不要见怪。以后会慢慢提升的