ExcelService 业务类
package cn.service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelService {
/**
* 调用 JXL 生成 Excel 文件
* */
public InputStream generateExcel(){
Label label = null;
WritableWorkbook workbook = null;
//字节数组的输出流
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("第 1 页", 0);
label = new jxl.write.Label(0, 0, "标题");
sheet.addCell(label);
label = new jxl.write.Label(0, 1, "数据");
sheet.addCell(label);
workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
InputStream is = new ByteArrayInputStream(os.toByteArray());
return is;
}
}
ExcelAction 控制类
package cn.action;
import java.io.InputStream;
import cn.service.ExcelService;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ExcelAction extends ActionSupport {
//建立一个输入流对象,用于 Excel 文件下载
private InputStream inputStream;
//业务类
private ExcelService excelService = new ExcelService();
//调用业务类生成 Excel
public String execute(){
this.inputStream = excelService.generateExcel();
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public ExcelService getExcelService() {
return excelService;
}
public void setExcelService(ExcelService excelService) {
this.excelService = excelService;
}
}
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_3_0.xsd"
version="3.0">
<display-name>Spring13Struts2Jxl</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
applicationContext.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts
PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="cn.action" extends="struts-default" namespace="/">
<action name="excel" class="cn.action.ExcelAction">
<result type="stream" name="success">
<!-- 返回类型是 Excel -->
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<!-- 指定下载的文件名 -->
<param name="contentDisposition">attachment;filename="export.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
index.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>下载文档</title>
</head>
<body>
下载文档:
<a href="excel.action">内容</a>
</body>
</html>
效果图: