struts2中有对导出excel表格的支持,所以开发起来比较容易,主要的步骤有三点:
1.配置文件:
<action name="excel" class="userAction" method="excel" >
<result name="excel" type="stream">
<!-- 注意这里的ContentType -->
<param name="contentType">application/vnd.ms-excel</param>
<!-- 这里需要和Action里的变量名一致 -->
<param name="inputName">excelStream</param>
<param name="contentDisposition">filename="user.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
2.action中的函数为excel如下:
public String excel() throws Exception {
StringBuffer excelBuf = new StringBuffer();
excelBuf.append("编号").append("\t").append("登录名称").append("\t").append("联系人").append("\t").append("电话").append("\t").append("Email").append("\t").append("所属单位").append("\n");
List list1 = mobjUserService.findAllUser();
for (int i = 0; i < list1.size(); i++) {
User user = (User) list1.get(i);
excelBuf.append(user.getUserId()).append("\t").append(user.getLoginName()).append("\t").append(user.getContactPerson()).append("\t").append(user.getPhone()).append("\t").append(user.getEmail()).append("\t").append(user.getRecommendCompany().getCompanyName()).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";
}
3.jsp中对应:
<td width="10%">
<input name="Submit" type="button" class="button"
onClick="javaScript:sysReturn('<c:out value="${ctx}"/>/excel.action');"
value="<s:text name="导出excel"/>">
</td>
比较简单好用就不详细描述了。
分享到:
相关推荐
本文档详细介绍了struts2整合apache的jxl导入导出excel表格的方法。包括代码,可以直接使用。
struts2+poi实现excel表格导出
自己做的一个完整导出功能,struts2+poi导出excel 经过自己严格测试通过的。
包含POI 的Jar包,利用反射机制写的导出报表类,报表实体类,测试类,struts 2配置文件及Action中调用
根据excel模板动态导出数据库数据 package text; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet....
公司正在使用的员工考勤系统,对打卡机生成的原始数据进行了一系列复杂的处理后生成标准...使用了Struts2,框架。对Excel的时间函数进行处理。虽然资源分高了点,但绝对超值,设计公司人员机密,请勿在公共场合讨论。
这是一个Struts2 Hibernate3 Spring2整合的小例子,里面包含了整合所需的jar文件,实现了增删改查,并且实现了根据表格导出excel文件的功能!
通常业务需求都是客户端一个导出按钮,发送请求到服务端,服务端写一个接口导出报表到客户端,客户可以自行下载。无论Struts或者springMVC均可。 @RequestMapping("Export") @ResponseBody public String getAll...
这是一个简单的javaweb项目例子,使用的是struts2技术,在后台的action中写好一个list,然后给这个list填充好内容,之后在jsp前台显示,并可以通过导出到excel文件,存储在本地指定的位置。
java版oa源码下载 S2SH Struts2,Hibernate,Spring三大框架的整合总是很麻烦,在此提供一个已经整合好三大框架的包,可以直接下载导入Myeclipse使用,项目基于Maven...7.包含了一个文件上传的功能和数据表导出为Excel下