导出Excel功能的实现2

 <a href="xiaomi-export.action">导出购买用户数据(团购小米手机)</a><br/><br/>
 
	public InputStream getFileInputStream() {
		PipedInputStream in = new PipedInputStream(4096);
		try{
		
		 final DetachedCriteria dc = this.orderXiaomiService.createCriteria();
		 final PipedOutputStream out = new PipedOutputStream(in);
			new Thread(new Runnable() {
				public void run() {
						
					try {
						StringBuilder temp = new StringBuilder();
						temp.append("我们系统订单号").append(",");
						temp.append("送货地址").append(",");
						temp.append("收件人姓名").append(",");
						temp.append("联系手机号码").append(",");
						temp.append("订单创建时间").append(",");
						temp.append("付款状态").append(",");
						temp.append("淘宝订单号").append("\r\n");
						out.write(temp.toString().getBytes("gbk"));
						
						orderXiaomiService.execute(new HibernateCallback() {
								
								@Override
								
								public Object doInHibernate(Session session)throws HibernateException, SQLException {
									
									ScrollableResults objects = dc.getExecutableCriteria(session).setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
									SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
									int count = 0;
									while (objects.next()) {
										StringBuilder temp1 = new StringBuilder();
										
										OrderXiaomi vo = (OrderXiaomi) objects.get(0);
										
										temp1.append(vo.getOrderNo()).append(",");
										temp1.append(vo.getAddress()).append(",");
										temp1.append(vo.getRecipientName()).append(",");
										temp1.append(vo.getContactPhone()).append(",");
										temp1.append(vo.getCreateTime()!=null?df.format(vo.getCreateTime()):"").append(",");
										temp1.append(Boolean.TRUE.equals(vo.getPayStatus())?"已支付":"未支付").append(",");
										temp1.append(vo.getTaoNo()).append(",");
										
										temp1.append("").append("\r\n");
										
										try {
											out.write(temp1.toString()
													.getBytes("gbk"));
										} catch (IOException e) {
											e.printStackTrace();
										}
										if (++count % 20 == 0) {
											// flush a batch of updates and
											// release memory:
											session.flush();
											session.clear();
										}
									}
									return null;
								}
							});


						out.flush();
						out.close();// 必须在此线程关闭输出流
					} 
					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	
				}
				
			}).start();
		}
			catch (Exception e) {
			e.printStackTrace();
		}
		return in;
	}

	/**
	 * 定义"contentDisposition","attachment;filename=${fileName}.xls",中${fileName}
	 * 属性来源,需要转成8850编码
	 * 
	 * @return
	 */
	public String getFileName() {
		try {
			return new String(("export").getBytes(),
					"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "export";
	}

	@Override
	@Action(results = { @Result(name = "success", type = "stream", params = {
			"contentType", "application/octet-stream", "inputName",
			"fileInputStream", "contentDisposition",
			"attachment;filename=${fileName}.csv", "bufferSize", "1024" }) })
	public String execute() throws Exception {
		
		return SUCCESS;
	}
}
 

你可能感兴趣的:(导出Excel)