Java POI导出Excel
JavaScript 脚本
<script type="text/javascript">
function exportExcel(){
var d = document.forms[0];
d.action = "excelAction.action";
d.submit;
}
</script>
Jsp页面
<form action="">
<input type="image" src="images/export.png" onclick="exportExcel()" />
</form>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>javapoi</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Action 类
package com.zhanggaosong.poi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.opensymphony.xwork2.ActionSupport;
import com.zhanggaosong.poi.domain.Student;
public class ExcelDownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
/**
* @功能:手工构建一个简单格式的Excel
*/
private static List<Student> getStudent() throws Exception {
List<Student> list = new ArrayList<Student>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
Student user4 = new Student(4, "周六", 26, df.parse("1985-11-12"));
Student user5 = new Student(5, "赵七", 26, df.parse("1985-11-12"));
Student user6 = new Student(6, "关羽", 26, df.parse("1985-11-12"));
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
list.add(user6);
return list;
}
public InputStream getExcelFile() {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生信息表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
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);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<Student> list = null;
try {
list = ExcelDownloadAction.getStudent();
} catch (Exception e1) {
e1.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
//设置自适应宽度 中文列无效
// sheet.autoSizeColumn(i+1);
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell(0).setCellValue(stu.getId());
row.createCell(1).setCellValue(stu.getName());
row.createCell(2).setCellValue(stu.getAge());
row.createCell(3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
wb.write(baos);
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
public String execute() throws Exception {
return SUCCESS;
}
}
Strudent实体
package com.zhanggaosong.poi.domain;
import java.util.Date;
public class Student
{
private int id;
private String name;
private int age;
private Date birth;
public Student(){}
public Student(int id, String name, int age, Date birth)
{
this.id = id;
this.name = name;
this.age = age;
this.birth = birth;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public Date getBirth()
{
return birth;
}
public void setBirth(Date birth)
{
this.birth = birth;
}
}
Struts 2 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="excelAction" class="com.zhanggaosong.poi.ExcelDownloadAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="zhanggaosong.xls"</param>
<!-- <param name="contentDisposition">attachment;filename="${fileName}"</param> -->
<param name="inputName">excelFile</param>
</result>
</action>
</package>
</struts>