导入 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xingxue.poi</groupId>
<artifactId>poi-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>80</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
页面 poidemo.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/4/28
Time: 9:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="scripts/jquery-3.2.1.js"></script>
<script>
$(function () {
$("#but").click(function () {
// 返回的是 原生的 Dom 对象
var dowloadExcleIdObj = $("#dowloadExcleId").get(0);
var url = "getPoi";
dowloadExcleIdObj.src = url;
return false;
});
});
</script>
</head>
<body>
<button id="but">excle导出</button>
<%--用 display 属性隐藏标签不会影响整个html文档的结构 没有什么改变,如果 visibility 属性来隐藏标签就会影响整个html 结构 内容全部掉下来--%>
<iframe id="dowloadExcleId" style="display: none"></iframe>
</body>
</html>
在webapp下面导入jQuery
E:\excelfinal\src\main\webapp\scripts\jquery-3.2.1.js 自己下载
三。写model 这里没有连接数据库 如果连接数据库了,要和数据库一致
package com.xingxue.poi.model;
public class UserModel {
private String logname;
private String password;
public String getLogname() {
return logname;
}
public void setLogname(String logname) {
this.logname = logname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
写接口
package com.xingxue.poi.web.controller;
import com.xingxue.poi.model.UserModel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.event.DocumentEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(value = "/getPoi")
public class PoiController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("请求来了...");
List<UserModel> users = new ArrayList<UserModel>();
for (int i = 1; i <= 10; i++) {
UserModel u = new UserModel();
u.setLogname("test" + i);
u.setPassword("88888");
users.add(u);
}
//==============================================================
doExcel(users,resp);
}
private void doExcel(List<UserModel> users,HttpServletResponse response) throws IOException {
//HSSFWorkbook 处理的 .xls 文件
Workbook wb = new HSSFWorkbook();
//wb 对象的操作的 工具类型对象 CreationHelper
CreationHelper createHelper = wb.getCreationHelper();
//创建工作表 Sheet 对象 创建表名“用户信息统计”
Sheet s = wb.createSheet("用户信息统计");
//声明表头 占一行 行从下标 0 开始
Row headRow = s.createRow(0);
//声明单元格 第一个单元格 下标从 0 开始
Cell cellUserName = headRow.createCell(0);
Cell cellPassword = headRow.createCell(1);
cellUserName.setCellValue("用户名");//将第一个单元格设置成用户名
cellPassword.setCellValue("密码");//将第二个单元格设置成为密码
int index = 1;//将user遍历循环出来
for (UserModel u : users ) {
Row r = s.createRow(index);
r.createCell(0).setCellValue(u.getLogname());
r.createCell(1).setCellValue(u.getPassword());
index++;
}
// 保存文件的名字
String filename = "用户信息表.xls";
//防止中文乱码
filename = new String(filename.getBytes(),"iso-8859-1");
//创建一个 用户信息表.xls 文件,并把输出流管道插入到 用户信息表.xls 这个文件准备向外进行输出
//FileOutputStream out = new FileOutputStream(filename);
response.setHeader("content-disposition", "attachment;filename="+filename);
ServletOutputStream out = response.getOutputStream();
wb.write(out);
out.close();
}
}