POI导出excel项目(webwork)实例

 

后台action:

public String exportExcel(){			
		this.setUserList(this.getUserService().findUserInfosByGroupID(this.getGroupID(), "regTime", 1));
		HSSFWorkbook wb = new HSSFWorkbook();		
		HSSFSheet sheet = wb.createSheet("用户表");		
		HSSFRow row = sheet.createRow(0);
		// 第四步,创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式	
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("账号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("部门");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("注册时间");
		cell.setCellStyle(style);
		

		// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
		
		for (int i = 0; i < this.getUserList().size(); i++)
		{
			row = sheet.createRow(i + 1);
			UserInfo user = this.getUserList().get(i);
			// 第四步,创建单元格,并设置值
			row.createCell(0).setCellValue(user.getUserName());
			row.createCell(1).setCellValue(user.getNickName());
			row.createCell(2).setCellValue(user.getCompanyName());
			row.createCell( 3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(user.getRegTime()));;
			
		}
		// 第六步,将文件存到指定位置
		if(this.getGroupID()==0){
		this.setExportfilename("全部用户"); //设置fileName
		}else if (this.getGroupID()==8) {
			this.setExportfilename("未审核用户");
		}else if(this.getGroupID()==9){
			this.setExportfilename("已审核用户");
		}		
		try
		{
			this.setExportfilename(new String(this.getExportfilename().getBytes(),"ISO8859-1"));
			this.workbook2InputStream(wb);
		}
		catch (Exception e)
		{
			logger.error(e);
			return ERROR;
		}
		return "exportExcel";
	}
	
	 private void workbook2InputStream(HSSFWorkbook workbook) throws Exception{         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         workbook.write(baos); 
         baos.flush(); 
         byte[] aa = baos.toByteArray();
         excelStream = new ByteArrayInputStream(aa, 0, aa.length);
         baos.close();
  }

  struts.xml 配置

<action name="exportexcel" class="egusermanagerAction" method="exportExcel">		
		    <result name="exportExcel" type="stream"> 
                <param name="contentType">application/vnd.ms-excel</param>
                <param name="inputName">excelStream</param>
                <param name="contentDisposition">attachment;filename="${exportfilename}.xls"</param>
                <param name="bufferSize">4024</param>
             </result> 
            <result name="error">/WEB-INF/egpage/intercepthtml.jsp</result>
        </action>

  

转载于:https://www.cnblogs.com/estellez/p/4110656.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值