当前位置: 代码迷 >> Web前端 >> 导出到excel报表
  详细解决方案

导出到excel报表

热度:197   发布时间:2013-08-09 15:16:24.0
导出到excel表格

public void exportYffzjyExcel(){
		Map<String, Object> beans=new HashMap<String, Object>();
		beans.put("xlgzgl", xlgzgl);
		String wjmc="|";
		if(wjmcs!=null){
			for(int i=0;i<wjmcs.length;i++){
				wjmc+=wjmcs[i]+"|";
			}
		}
		beans.put("wjmcs", wjmc);
		String templateName=getTemplateName();		
		String filename=templateName+Tools.getNowTime()+".xls";	
		exportExcel(beans, templateName+".xls", filename);
	}

public String getTemplateName(){
		return "xlgzgl";
	}

public static String getNowTime(){
		Date date = new Date(System.currentTimeMillis());
		return FastDateFormat.getInstance("yyyyMMddHHmmss").format(date);
	}



public void exportExcel(Map beans,String templateName,String filename) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment; filename=\"" + filename);
			ServletOutputStream outStream = response.getOutputStream();
			ExcelUtils.exportExcel(beans, this.getText("template.path") + System.getProperty("file.separator") + templateName, outStream);			
			response.getOutputStream().flush();
			response.getOutputStream().close();			
			outStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}






public static void exportExcel(Map beans, String templateName, OutputStream out) {
		XLSTransformer transformer = new XLSTransformer();
		try {
			FileInputStream fis=new FileInputStream(templateName);
			transformer.transformXLS(fis, beans).write(out);
			
			fis.close();
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}
	}
  相关解决方案