导出到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) {

//templateName  是实例excel表格的存放地址
//import net.sf.jxls.transformer.XLSTransformer;

		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();
		}
	}

你可能感兴趣的:(Excel)