`##Excel 格式报表生成 POI ##
注意:这个案例用的是SSH框架,此框架之后会有介绍,敬请期待…
1.准备一个页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../js/jquery-1.8.3.js" type="text/javascript" charset="utf-8"></script>
<title>做一个课程表</title>
<style type="text/css">
body {
width: 340px;
height: 800px;
}
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<!--表格-->
<table width="80%" align="center" height="40" border="1px" cellspacing="0px">
<!--表单-->
<form action="" method="post">
<tr align="center">
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<tr align="center">
<td>张三</td>
<td>17</td>
<td>男</td>
</tr>
<tr align="center">
<td>李四</td>
<td>17</td>
<td>男</td>
</tr>
<tr align="center">
<td>小红</td>
<td>17</td>
<td>女</td>
</tr>
</form>
</table>
</body>
</html>
2.给按钮添加一个导出事件
添加在</style>下面
<script type="text/javascript">
// 添加一个导出Excel事件
$(function() {
// 获取按钮
$("#exportXlsBtn").click(function() {
// 下载效果
});
});
</script>
3.创建一个实体类
package com.bsw.demo.domain;
public class Person {
private String name;
private String age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
// 测试时方便打印信息
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
4.编写action代码(因为只是一个案例,所以不需要查询数据库,此处只写一个action方便接收请求)
action代码:
package com.bsw.demo.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.zm.bos.utils.FileUtils;
import com.bsw.demo.domain.Person;
@ParentPackage("json-default")
@Namespace("/baobiaoguanliDemo")
@Controller
@Scope("prototype")
public class PersonAction {
@Action(value = "person_find")
public String findAll() throws IOException {
// 创建一个list集合用于存储person
List<Person> persons = new ArrayList<Person>();
// 创建三个person并赋值
Person person1 = new Person();
person1.setName("张三");
person1.setAge("17");
person1.setSex("男");
Person person2 = new Person();
person2.setName("李四");
person2.setAge("17");
person2.setSex("男");
Person person3 = new Person();
person3.setName("小红");
person3.setAge("17");
person3.setSex("女");
// 将创建的三个person放入list集合中
persons.add(person1);
persons.add(person2);
persons.add(person3);
/*
* 生成Excel文件-POI 生成 Excel 步骤写 Excel 过程一样, 新建 Excel 文档 -- 新建 Sheet -- 新建
* Row --新建 Cell 单元格 -- 写单元格数据
*
* POI 生成 HSSF (xls)和 XSSF (xlsx) 此案例我们使用HSSF
*/
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("数据表");
// 表头
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("姓名");
headRow.createCell(1).setCellValue("年龄");
headRow.createCell(2).setCellValue("性别");
// 表格数据
for (Person person : persons) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(person.getName());
dataRow.createCell(1).setCellValue(person.getAge());
dataRow.createCell(2).setCellValue(person.getSex());
}
// 下载导出文件
// 设置头信息
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
String filename = "信息数据.xls";
// 获取浏览器的类型
String agent = ServletActionContext.getRequest().getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream outputStream = ServletActionContext.getResponse()
.getOutputStream();
hssfWorkbook.write(outputStream);
return "none";
}
}
此处是上述代码中所需的工具类
package cn.zm.bos.utils;
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class FileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}
最终案例完成