struts2 利用 POI 导出 excel

第一种方式:在action中设置header等

1. 根据数据生成 HSSFWorkbook, 并将 HSSFWorkbook  写入到输出流

private void createExcel(List<String[]> data) throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		//excel 文件的 MIME 类型
		response.setContentType("application/vnd.ms-excel");
		String fileName = "导出数据";//需要使用<span style="font-family: Arial, Helvetica, sans-serif;">URLEncoder编码一下, 否则中文名字不会显示</span>
		response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8") +".xls");
		
		HSSFWorkbook wb = new HSSFWorkbook();
		if(data == null || data.size() < 1){
			return;
		}
		
		String sheetTitle = "sheet1";
		HSSFSheet sheet = wb.createSheet();
		wb.setSheetName(0,sheetTitle);

		HSSFRow row=null;
		HSSFCell cell = null;
		
		for(int i=0; i<data.size(); i++){
			row = sheet.createRow(i);
			for(int j=0; j<data.get(i).length; j++){
				
				cell = row.createCell(j);
				cell.setCellValue(data.get(i)[j]);
			}
		}
		
		OutputStream os = ServletActionContext.getResponse().getOutputStream();
		
		wb.write(os);
		
		if(os != null){
			os.flush();
			os.close();
		}
	}

2. 在 action 中调用该方法即可

public String export() throws Exception{
		
			
		oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true);
			
		List<String[]> data = new ArrayList<String[]>();
		List<OauthInfoBean> result = pageInfo.getSearchResult();
		String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"};
		data.add(rowName);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		for(OauthInfoBean bean : result){
			rowName = new String[4];
			rowName[0] = bean.getReqSource() == null?"":bean.getReqSource();
			if(bean.getAuthTime() != null){
				rowName[1] = sdf.format(bean.getAuthTime());
			}else{
				rowName[1] = "";
			}
			rowName[2] = bean.getReqTime()==null?"":bean.getReqTime();
			rowName[3] = bean.getPhone()==null?"":bean.getPhone();
			data.add(rowName);
		}
		
		this.createExcel(data);
		
		
		return NONE;
	}

3. struts2的配置文件中的配置方式

<action name="export" class="oauthInfoJsonAction" method="export">
			
		</action>


第二种方式,在struts2的配置文件中配置

代码
	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String export() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		//excel 文件的 MIME 类型
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename=授权信息数据.xls");
		exportFileName = "导出数据";
			
		oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true);
			
		List<String[]> data = new ArrayList<String[]>();
		List<OauthInfoBean> result = pageInfo.getSearchResult();
		String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"};
		data.add(rowName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for(OauthInfoBean bean : result){
			rowName = new String[4];
			rowName[0] = bean.getReqSource() == null?"":bean.getReqSource();
			if(bean.getAuthTime() != null){
				//rowName[1] = sdf.format(bean.getAuthTime());
			}else{
				rowName[1] = "";
			}
			rowName[2] = bean.getReqTime()==null?"":bean.getReqTime();
			rowName[3] = bean.getPhone()==null?"":bean.getPhone();
			data.add(rowName);
		}
		
		this.createExcel(data);
		
		
		return SUCCESS;
	}
	
	private void createExcel(List<String[]> data) throws Exception{
		HSSFWorkbook wb = new HSSFWorkbook();
		if(data == null || data.size() < 1){
			return;
		}
		
		String sheetTitle = "sheet1";
		HSSFSheet sheet = wb.createSheet();
		wb.setSheetName(0,sheetTitle);

		HSSFRow row=null;
		HSSFCell cell = null;
		
		for(int i=0; i<data.size(); i++){
			row = sheet.createRow(i);
			for(int j=0; j<data.get(i).length; j++){
				
				cell = row.createCell(j);
				cell.setCellValue(data.get(i)[j]);
			}
		}
		
		this.exportFileName="导出数据";//设置fileName
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos);
		baos.flush();
		byte[]aa=baos.toByteArray();
		this.excelFileStream = new ByteArrayInputStream(aa,0,aa.length);
		baos.close();
	}

xml配置文件
<action name="export" class="oauthInfoJsonAction" method="export">
			<result type="stream">
				
				<param name="contentType">application/vnd.ms-excel</param>  
	
				<param name="contentDisposition">attachment;filename=123.xls</param> 
	            
	            <param name="bufferSize">1024</param>
	
	            <param name="inputName">excelFileStream</param>   
			</result>
		</action>
当然在action中必须要有 excelFileStream这个属性和set,get方法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值