POI3.8组件研究(六)---struts2.0 视图层文件页面点击导出

    在struts2.0中点击导出按钮将信息导出为excel文件的实现,当点击按钮时候,需要将生成的excel的文件转换为输出流在页面下载提供用户保存。

在struts2.0中action的代码方法为:

public class  FaultAction extends BaseStruts2Action

/**
	 * 点击导出导出页面
	 * @return
	 */
	public String faultExport(){
		return SUCCESS;
	}
	/**
	 * 报表附件文件下载
	 * @return
	 * @throws IOException 
	 */
	public InputStream getInputStream() throws Exception{ 
		//读取相关的属性文件
		Properties properties=new Properties();
		properties.load(FaultAction.class.getResourceAsStream("/resource_excel.properties"));	
		String headerTitlesStr=properties.getProperty("headerTitles");
		//headerTitlesStr=new String(headerTitlesStr.getBytes(),"utf-8");
		String titlesStr=properties.getProperty("titles");
		String[]  headerTitltes=headerTitlesStr.split(":");
		String[] titles=titlesStr.split(":");
		if(logger.isDebugEnabled()){
			logger.debug("故障汇总统计报告:"+headerTitlesStr);
		}
		fileName =properties.getProperty("fileName");
		fileName=EncodeUtils.urlEncode(fileName);
		//fileName=new String(fileName.getBytes("utf-8"),"ISO8859-1");
		String title="故障反馈及处理跟踪表";
		List<Map<String, Object>> faultMapList=faultService.queryFalutReportMap(faultCon);
		//遍历并存储所有的项目
		Map<String,List<Map<String, Object>>> faultCollMap=new HashMap<String,List<Map<String, Object>>>();
		Map<String,String> projectCollMap=new HashMap<String,String>();
		if(CollectionUtils.isNotEmpty(faultMapList)){
			for(Map<String, Object> faultMap : faultMapList){
				String projectId=faultMap.get("projectId").toString();
				String projectName=faultMap.get("projectName").toString();
				List<Map<String, Object>> tempFaultMapList=null;
				if(faultCollMap.containsKey(projectId)){
					tempFaultMapList=faultCollMap.get(projectId);
				}else{
					//存储项目名称信息
					projectCollMap.put(projectId, projectName);
					//创建故障集合并存储故障
					tempFaultMapList=new ArrayList<Map<String, Object>>();
				}
				tempFaultMapList.add(faultMap);
				faultCollMap.put(projectId, tempFaultMapList);
			}
		}
		ExcelVO excelVO=new ExcelVO();
		excelVO.setPath(fileName);
		excelVO.setPrefix("xls");
		if(MapUtils.isNotEmpty(faultCollMap)){
			Set<Entry<String,List<Map<String, Object>>>> faultset=faultCollMap.entrySet();
			for (Entry<String,List<Map<String, Object>>> entry : faultset) {
				String projectId=entry.getKey();
				List<Map<String, Object>> faultInfoList=entry.getValue();
				String projectName=projectCollMap.get(projectId);
				SheetVO sheetVO=new SheetVO();
				 //表中中的表头
				sheetVO.setHeaderTitle(headerTitltes);
				//每行绑定的字段
				sheetVO.setTitles(titles);
				sheetVO.setTitle(title);
				//表中的数据
				sheetVO.setSheetContentMap(faultInfoList);
				//表头起始的行数
				sheetVO.setRowNum(1);
				//Sheet中标题
				sheetVO.setSheetName(projectName);
				List<SheetVO> sheets=null;
				if(excelVO.getSheets()!=null){
					sheets=excelVO.getSheets();
				}else{
					sheets=new ArrayList<SheetVO>();
				}
				sheets.add(sheetVO);
				excelVO.setSheets(sheets);
			}
		}
		InputStream in =excelService.createExcelService(excelVO);
		return in;
	} 

 

 

struts2.0的配置如下:

		<!-- 故障汇总统计功能 -->
		<action name="faultExport" method="faultExport" class="com.easyway.eamsg.faultmgt.action.FaultAction">
     		<result name="success" type="stream">
     			<param name="contentType">application/octet-stream;charset=ISO8859-1</param> 
				<param name="inputName">inputStream</param> 
				<param name="contentDisposition">attachment;filename="${fileName}"</param> 
				<param name="bufferSize">10240</param> 
     		</result>
     	</action> 

 

 

jsp页面一个简单的按钮的:

<td   width="10%" class="font9_cu_9" >
          <input type="button" value="导出Excel" class="btn01" onclick="javascript:excelInfo()">
          </td>

	function excelInfo(){
		//验证时间
		var beginTime = document.getElementById("beginTime").value;
		 beginTime =beginTime.replace(/(^\s*)|(\s*$)/g,"");
		 if(beginTime==''){
			 alert("请输入故障发生起始时间!");
			 return;
		 }
		 //截至时间
		 var endTime = document.getElementById("endTime").value;
		 endTime =endTime.replace(/(^\s*)|(\s*$)/g,"");
		 if(endTime==''){
			 alert("请输入故障截至时间!");
			 return;
		 }
		 var faultConitemId = document.getElementById("faultConitemId").value;
		 document.forms[0].action="${pageContext.request.contextPath }/faultMgt/faultExport.action?faultCon.startDate="+beginTime+"&faultCon.endDate="+endTime+"&faultCon.itemId="+faultConitemId+"&time="+new Date().getTime();
		 document.forms[0].submit();
	}

 

你可能感兴趣的:(struts2)