`
everlxq
  • 浏览: 103114 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

导出到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();
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics