在利用网页展示查询结果,经常会遇到要求导出成Excel的需求。采用这种方法可以定制输出的格式和内容(还不支持合并单元格和公式),生成真正的Excel格式(不是csv)的Excel。
一、struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="demo" extends="struts-default">
<action name="excel" method="execute" class="demo.ExcelAction">
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param> <!-- 注意这里的ContentType -->
<param name="inputName">excelStream</param> <!-- 这里需要和Action里的变量名一致 -->
<param name="contentDisposition">filename="standard.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
二、Struts2的 Action
package demo;
public class ExcelAction {
private InputStream excelStream; // 需要生成getter和setter
public String execute() throws Exception {
StringBuffer excelBuf = new StringBuffer();
excelBuf.append("BookName").append("\t").append("Year").append("\t").append("author").append("\n");
excelBuf.append("Thinking in Java").append("\t").append("2001").append("\t").append("Eckel").append("\n");
excelBuf.append("Spring in action").append("\t").append("2005").append("\t").append("Rod").append("\n");
String excelString = excelBuf.toString();
logger.debug("result excel String: " + excelString);
excelStream = new ByteArrayInputStream(excelString.getBytes(), 0, excelString.length());
return "excel";
}
// getter and setter
...
}
三、Jsp页面
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<s:head />
</head>
<body>
<s:form action="" method="post">
<s:submit key="button.submit"/>
</s:form>
</body>
</html>
分享到:
相关推荐
struts2中利用poi导出excel的简单例子
struts2导出excel java 导出excel: JXL_excel: 可以直接在elipse下运行的例子 可以直接导出excel文件(包含所需的jar包) CSharpJExcel: 是JXL的官方下载的 包括详细的文档 和jar包
在struts中利用poi组件导出excel
struts1.2+extjs+导出excel项目 struts1.2+extjs+导出excel项目
Struts2文件流方式导出下载excel、Txt、image图片,直接返回给浏览器提示下载,不生成临时文件,直接保存在客户端
struts2实现excel导出struts2实现excel导出struts2实现excel导出struts2实现excel导出
利用纯struts2.0+poi插件 实现导出excel
利用struts2和poi把数据导出到excel
主要是利用struts2导出excel,格式良好,jar包已添加,可以直接导入运行,有什么问题请联系我
Web开发技术,excel导出的详解,对POI导出EXCEL代码实现的详细解释
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
整合struts2 ssh导出excel,绝对可以用。
struts2+poi实现excel表格导出
使用JSP、Struts2和jquery实现简单查询,分页,导出到Excel并保存到本地计算机。(数据库相关内容在webroot下db文件夹内)
Struts2 poi 实现从数据库中导出excel表
本文档详细介绍了struts2整合apache的jxl导入导出excel表格的方法。包括代码,可以直接使用。
自己做的一个完整导出功能,struts2+poi导出excel 经过自己严格测试通过的。
完整的导出excel了,非常好的代码,看看不吃亏,完整的代码!
首先说明这不是个项目不能直接运行,需要自己集成到自己项目中,文件包括 struts2 Excel的导入和导出。
K8 struts2利用工具