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>
效果图:
相关推荐
完整的导出excel了,非常好的代码,看看不吃亏,完整的代码!
完整的struts2+jxl文件上传!很好!
NULL 博文链接:https://chaoyi.iteye.com/blog/2174168
NULL 博文链接:https://heisetoufa.iteye.com/blog/1932093
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
jsp+jspsmart上传+poi3.8读取excel2007+jxl读取excel2003
struts2、poi.jar、jxl.jar
S2SH+jxl.jar实现导出Excel文档
NULL 博文链接:https://chaoyi.iteye.com/blog/2174163
运用了Struts和hibernate框架,使用jxl实现工资代发,log4j系统日志记录。
本文档详细介绍了struts2整合apache的jxl导入导出excel表格的方法。包括代码,可以直接使用。
JXL+IText把excel转换为PDF JXL+IText把excel转换为PDF JXL+IText把excel转换为PDF JXL+IText把excel转换为PDF
struts2导出excel java 导出excel: JXL_excel: 可以直接在elipse下运行的例子 可以直接导出excel文件(包含所需的jar包) CSharpJExcel: 是JXL的官方下载的 包括详细的文档 和jar包
使用jxl可以快速进行excel文档的建立,读写和建立Sheet页等操作.
android excel poi+jxl在android的环境下实现对excel的操作。
一个简单的struts2框架,使用poi和jxl由前台向excel表中添加数据的一个小程序源码,下载下来部署到服务器就能使用,execl的路径在action中修改成你的路径就能使用了
通过给出URL,用JXL进行下载 可以直接存到指定的目录
java中使用jxl打印excel报表,java中使用jxl打印excel报表,java中使用jxl打印excel报表
JXL开发excel文档(中文) 做java报表excel方面的必需品