struts2导出数据为excel

今天需要为一个很久的struts2项目做一个导出为EXcel的功能,通过百度实现了功能,特此记录下来,做个笔记

前台页面显示

	<s:form action="mobileListForSelf.ce" validate="true" name="searchTab" id="searchTab">
		<div class="int">
			用户名:<s:textfield name="username" id="username" size="12" cssClass="addinput"/> 手机:<s:textfield name="mobile" id="mobile" size="12" cssClass="addinput"/>
					
		</div>
		<div class="table03_btn01">
			<s:submit id="selectBut" value="查询" cssClass="small" />
			<s:submit id="selectBut" value="导出" cssClass="small" method="exportExcel"/>
						
		</div>
	</s:form>

struts.xml文件中用到的action

	<action name="mobileListForSelf" class="ContactAction" method="mobileListForSelf">
             <result>/WEB-INF/jsp/version2/contact_mobile.jsp</result>
             <result name="excel" type="stream">
	             <param name="contentType">application/vnd.ms-excel</param>    <!-- 注意这里的ContentType -->
	             <param name="inputName">excelStream</param>                   <!-- 这里需要和Action里的变量名一致 -->
	             <param name="contentDisposition">attachment;filename="download.xls"</param> <!-- 下载文件的名字 -->
             </result>

</action>

java代码

private InputStream excelStream;
//ge和set方法
public String exportExcel(){
		System.out.println("导出excel");
		FileOutputStream out=null;
		try{
			if (contactCode == null || "".equals(contactCode)) {
				contactCode = (String) session.get("contactCode");
			}
			if (deptNo == null || "".equals(deptNo)) {
				deptNo = (String) session.get("deptNo");
			}
			if (sourceId == null || "".equals(sourceId)) {
				sourceId = (String) session.get("sourceId");
			}
			logger.info("contactCode=[" + contactCode + "]");
			logger.info("deptNo=[" + deptNo + "]");
			logger.info("sourceId=[" + sourceId + "]");
	
			session.put("contactCode", contactCode);
			session.put("deptNo", deptNo);
			session.put("sourceId", sourceId);
			
			if (pageNo == 0) {
	//			ActionContext ctx = ActionContext.getContext();
	//			HttpServletRequest request = (HttpServletRequest) ctx
	//					.get(ServletActionContext.HTTP_REQUEST);
	//
	//			tourl = request.getHeader("Referer");
				pageNo = 1;
			}
			PagingQuery PagingQuery = new PagingQuery();
			PagingQuery.setPageNo(pageNo);
			PagingQuery.setPageSize(50);
	
			// System.out.println("mobilelist contactCode====="+contactCode);
	
			PagingQuery = contactService.getMobileListForSelf(PagingQuery, contactCode,
					username, mobile);
			mobileListBySelf = PagingQuery.getResults();
			 // 声明一个工作薄
	        HSSFWorkbook wb = new HSSFWorkbook();
	        //声明一个单子并命名
	        HSSFSheet sheet = wb.createSheet("员工通讯录");
	        //给单子名称一个长度
	        sheet.setDefaultColumnWidth((short)15);
	        // 生成一个样式  
	        HSSFCellStyle style = wb.createCellStyle();
	        //创建第一行(也可以称为表头)
	        HSSFRow row = sheet.createRow(0);
	        //样式字体居中
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	        //给表头第一行一次创建单元格
	        HSSFCell cell = row.createCell( (short) 0); ;
	        
            cell.setCellValue("员工姓名");  
            cell.setCellStyle(style);  
            cell = row.createCell((short) 1);  
            cell.setCellValue("电话号码");  
            cell.setCellStyle(style); 
	         
	 
           //向单元格里填充数据
           for (short i = 0; i < mobileListBySelf.size(); i++) {
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(((BMobiles)(mobileListBySelf.get(i))).getMobileName());
            row.createCell(1).setCellValue(((BMobiles)(mobileListBySelf.get(i))).getMobileNumber());
           }
           ByteArrayOutputStream output = new ByteArrayOutputStream();

           wb.write(output);
           byte[] ba = output.toByteArray();
           excelStream = new ByteArrayInputStream(ba);
           output.flush();

	   return "excel";
	}catch (Exception e) {
		return "error";
	}finally{
		if(out != null){
			 try {
				out.close();
				} catch (IOException e) {
						// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	
}
切记,前台提交一定是submit提交,否则不会出现下载效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值