Struts2+JXL 下载 Excel 文档
程序员文章站
2022-06-09 17:11:23
...
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>
效果图:
推荐阅读
-
教你一招 让你轻松下载百度文库/豆丁网的文档
-
TXT文档复制Excel中数字全变成科学计数法如何将数字显示成原样
-
php导出word文档与excel电子表格的简单示例代码
-
免费下载豆丁网文档通过冰点文库轻松做到
-
让谷歌浏览器打开Word2003, Excel和PPT2003文档的设置方法
-
如何打开比较合并工作簿功能以便知道Excel文档被其他人修改过
-
jsp页面中显示word/excel格式的文档的方法
-
教你一招免费下载百度文库文档资料
-
3DSMAX2017怎么下载并安装离线帮助文档?
-
基于DevExpress的SpreadsheetControl实现对Excel的打开、预览、保存、另存为、打印(附源码下载)