Excel 기능 내보내기

4696 단어 Excel 내보내기
	function exportExcel(){
			if(confirm(' ')){
				$("#form1").attr("action","export-excel.action").submit();

				}else{
					return false;
					}
		
	}
 
ExportExcel에서java 클래스에서
@Action(results={@Result(name="success",type="stream",params={
			"contentType","application/octet-stream",
				"inputName","fileInputStream",
					"contentDisposition","attachment;filename=${fileName}.xls",
						"bufferSize","1024"
							}
						)
					}
	)
	
	@Override
	public String execute() throws Exception{
		System.out.println("aaaaaaaaaaaaaa");
		System.out.println(lotteryId);
		LotteryPeriod lotteryPeriod = this.lotteryPeriodService.findById(lotteryId);
		DetachedCriteria dc = this.lotteryPrizeService.createCriteria();
		dc.add(Restrictions.eq("lotteryPeriod", lotteryPeriod));
		List<LotteryPrize> lotPrize = this.lotteryPrizeService.findByDetachedCriteria(dc);
		
		DetachedCriteria dc2 = this.prizeListService.createCriteria();
		dc2.add(Restrictions.in("lotteryPrize", lotPrize));
		if(!StringUtils.isEmpty(user_num)){
			dc2.add(Restrictions.ilike("userNum",user_num.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(user_name)){
			dc2.add(Restrictions.ilike("userName",user_name.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(weibo_url)){
			dc2.add(Restrictions.ilike("weiboUrl",weibo_url.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(contact_phone)){
			dc2.add(Restrictions.ilike("contactPhone",contact_phone.trim(),MatchMode.ANYWHERE ));
		}
		dc.addOrder(Order.desc("createTime"));
		List<PrizeList> priList=this.prizeListService.findByDetachedCriteria(dc2);
		
		Sheet sheet = workBook.createSheet();
	    HSSFCellStyle style=workBook.createCellStyle();
	    style.setFillForegroundColor(HSSFColor.BLACK.index);
	    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		 
		Row metaRow = sheet.createRow(0);
		//Date nowDate = new Date();
		// 
		metaRow.createCell(0).setCellValue(" ");
		metaRow.createCell(1).setCellValue(" ");
		metaRow.createCell(2).setCellValue(" ");
		metaRow.createCell(0).setCellValue(" ");
		metaRow.createCell(1).setCellValue(" ");
		metaRow.createCell(2).setCellValue(" ");
		metaRow.createCell(0).setCellValue(" ");
		metaRow.createCell(1).setCellValue(" ");
		// excel 
		if(priList!=null&&priList.size()>0){
			int rowNum=1;
			for(int i=0;i<priList.size();i++){
				PrizeList obj = (PrizeList) priList.get(i);
				Row row = sheet.createRow(rowNum);
				
				row.createCell(0).setCellValue(rowNum);
				row.createCell(1).setCellValue(obj.getLotteryNo());
				row.createCell(2).setCellValue(obj.getUserNum());
				row.createCell(3).setCellValue(obj.getUserName());
				row.createCell(4).setCellValue(obj.getWeiboUrl());
				row.createCell(5).setCellValue(obj.getContactPhone());
				rowNum++;
			}
		}else{
			System.out.println(" ");
		}
		return super.execute();
	}
	
	/**
	 *  "contentDisposition","attachment;filename=${fileName}.xls",
	 *  ${fileName} , 8850 
	 * @return
	 * @throws ParseException 
	 */
	public String getFileName() throws ParseException {
		try {
				DateFormat formatSuffix = new SimpleDateFormat(
						"yyyy-MM-dd");
				this.fileNameSuffix = 
					formatSuffix.format(new Date());
			return new String((" ").getBytes("GBK"), "ISO8859-1" );	// GBK
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "export";
	}
	
	
	public InputStream getFileInputStream(){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] result = null;
		try {
			workBook.write(baos);
			baos.flush(); 
	        result = baos.toByteArray();
	        baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		return new ByteArrayInputStream(result, 0, result.length);
	}

좋은 웹페이지 즐겨찾기