extjs 导出简单的excel表格

js代码:

this.returnJnlExportBtn = new Ext.Button({
				text : "退单文件导出",
				id : "returnJnl_export",
				iconCls : "icon-excel",
				hidden: false,
				scope : this,
				handler : this.exportHandler
			});

exportHandler : function() {
			this.returnJnlExportBtn.setDisabled(true);
			var params = this.form_query.getFormDatas();
			formPost(params, "returnJnl/returnJnlExport1");					
			this.returnJnlExportBtn.setDisabled(false);
		},	


/* 导出excel */
function formPost(jsonObj, url) {
    var oForm = document.createElement("form");
    oForm.id = "sandpany-postForm";
    oForm.name = "sandpany-postForm";
    oForm.method = "post";
    oForm.action = url;
    oForm.target = "_blank";
    oForm.style.display = "none";

    for (var prop in jsonObj) {
        var oInput = document.createElement("input");
        oInput.name = prop;
        oInput.value = jsonObj[prop];
        oForm.appendChild(oInput);
    }
    document.body.appendChild(oForm);
    oForm.submit();
}
java poi 代码:

@RequestMapping("/returnJnlExport1")
	public void returnJnlExport1(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		Map<String, Object> params = ServletUtils.getParametersStartingWith(request, null);
		OutputStream outStream = null;
		try{
			HSSFWorkbook workbook = new HSSFWorkbook(); 
			HSSFSheet hssfSheet = workbook.createSheet("退单报表");
			HSSFRow hssfRow = null;
			HSSFCell hssfCell = null;
			HSSFRichTextString textString = null;
			HSSFCellStyle style = workbook.createCellStyle(); // 样式对象    
	        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
	        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平  
	        style.setBorderBottom((short)1);
	        style.setBorderLeft((short)1);
	        style.setBorderRight((short)1);
	        style.setBorderTop((short)1); 
	        style.setWrapText(true);//自动换行 
	        String columnTitle[] = {"退票流水号","结算划款通道","退单时间","退单原因","原收款户号","原收款账名","原联行行号",
					"原开户行名","原账户标识(1对私/0对公)","交易金额","交易备注","商户号","商户名称","签约分公司","新收款户号",
					"新收款账名","新联行行号","新开户行名","新账户标识(1对私/0对公)"};
	        //第一行(头)
	        hssfRow = hssfSheet.createRow(0);
	        hssfRow.setHeight((short)500);
			for(int i=0; i<columnTitle.length; i++){
				hssfSheet.setColumnWidth(i, 6000);
				hssfCell = hssfRow.createCell(i);
				textString = new HSSFRichTextString(columnTitle[i]);
				hssfCell.setCellValue(textString);
				//列头的样式
				 HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
				 HSSFFont columnHeadFont = workbook.createFont();  
				    columnHeadFont.setFontName("宋体");  
				    columnHeadFont.setFontHeightInPoints((short) 10);  
				    columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				    columnHeadStyle.setFont(columnHeadFont);  
				    columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
				    columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
				    columnHeadStyle.setLocked(true);  
				    columnHeadStyle.setWrapText(true);  
				    columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色  
				    columnHeadStyle.setBorderLeft((short) 1);// 边框的大小  
				    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色  
				    columnHeadStyle.setBorderRight((short) 1);// 边框的大小  
				    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
				    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色  
				    // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)  
				    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);  				  				    
				hssfCell.setCellStyle(columnHeadStyle);						
				}
			
			//第二行(内容)
			HSSFFont cfont = workbook.createFont();  
		    cfont.setFontName("宋体");  
		    cfont.setFontHeightInPoints((short) 10);  
			HSSFCellStyle contentstyle = workbook.createCellStyle();  
			contentstyle.setFont(cfont);  
			contentstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中  
			contentstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中  
			contentstyle.setWrapText(true);  
			contentstyle.setLeftBorderColor(HSSFColor.BLACK.index);  
			contentstyle.setBorderLeft((short) 1);  
			contentstyle.setRightBorderColor(HSSFColor.BLACK.index);  
			contentstyle.setBorderRight((short) 1);  
			contentstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
			contentstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.  
			contentstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
		    //数据集
		    List<Object[]> dataList = returnJnlFacade.returnJnlExport(params);
		    if(!dataList.isEmpty()){
		    	 for(int i=0; i<dataList.size(); i++){
						Object[] object = dataList.get(i);
						hssfRow = hssfSheet.createRow(i+1);
						for(int j=0; j<columnTitle.length; j++){					
							hssfCell = hssfRow.createCell(j);
							String v = object[j] == null ? null : object[j].toString();
							/*if(column != 2&&column != 3){ //2,3行是表示金额的
								hssfCell.setCellValue(v);
								hssfCell.setCellStyle(strStyle);
							}else{
								HSSFDataFormat format = workbook.createDataFormat();
								style.setDataFormat(format.getFormat("0.00"));
								style.setAlignment((short) 3);//右对齐
								hssfCell.setCellValue(Double.valueOf(v));
								hssfCell.setCellStyle(style);
							}*/
							hssfCell.setCellValue(v);
							hssfCell.setCellStyle(contentstyle);
						}
				    } 
		    }
		   
		    String filename = new String("退单报表".getBytes("gb2312"), "ISO8859-1");
			response.setContentType("application/vnd.ms-excel");   
			//当前日期的前一天
	        response.setHeader("content-disposition", "attachment;filename="+filename+DateUtils.getFrontDay(DateUtils.getCurrDate())+".xls");   
	        outStream = response.getOutputStream();
	        workbook.write(outStream);
			outStream.flush();   
    	    outStream.close(); 
		    
		}catch(Exception e){
			e.printStackTrace();
		}
	}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值