excel输出

参考:

http://boonya.iteye.com/blog/1768376

http://www.iteye.com/problems/64614


jsp页面-此form只用于excel输出:

 <form id="form1" name="form1" method="post"  accept-charset="GBK" οnsubmit="if(!!window.ActiveXObject){document.charset='GBK';}"  ></form>

 var url='/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name; 
$("#form1").attr("action",url); 
$("#form1").submit(); 



或者:

  jQuery("form")//选择form    
 .first()//选择第一个 第二个用eq(1) 最后一个 last()    
 .attr("action",'/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name)//更改属性    
 .submit();//提交 


或者:

 form1.action='/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name,
 form1.submit();


action后台代码:

response.setContentType("applicationnd.ms-excel;");
//		response.setHeader("Content-Disposition", "attachment;filename=" + templateName + ".xls");



详细:
// 弹出式导出Excel文件
	public void exportExcel() {
		HttpServletResponse response = this.getResponse();
//		response.setContentType("applicationnd.ms-excel; charset=UTF-8");
		response.setContentType("applicationnd.ms-excel;");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		Date date = new Date();
		String fileName = sdf.format(date);
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		
		// ******************************************集合
		String sort = this.getRequest().getParameter("sort");
		String order = this.getRequest().getParameter("order");
		String rygs = this.getRequest().getParameter("rygs");
		String ssjd = this.getRequest().getParameter("ssjd");
		String rybh = this.getRequest().getParameter("rybh");
		String name = this.getRequest().getParameter("name");
		// 查询条件
		StringBuffer contions = new StringBuffer();
		if (null != rygs && !"".equals(rygs)) {
			contions.append(" and ").append(" CZ_RYGS='").append(rygs)
					.append("' ");
		}
		if (null != ssjd && !"".equals(ssjd)) {
			contions.append(" and ").append(" CZ_SSJD='").append(ssjd)
					.append("' ");
		}
		if (null != rybh && !"".equals(rybh)) {
			contions.append(" and ").append(" CZ_NUMBER = '").append(rybh)
					.append("' ");
		}
		if (null != name && !"".equals(name)) {
			contions.append(" and ").append(" CZ_NAME like '%")
					.append(name).append("%' ");
		}
		Map map = new HashMap();
		if (sort == null || order == null) {
			map.put("sort", "rygs");
			map.put("order", "asc");
		} else {
			map.put("sort", sort);
			map.put("order", order);
		}
		map.put("contions", contions.toString());

		CaptialService capitalService = new CaptialService();

		long time1 = new Date().getTime();
		System.out.println("开始时间:"+new Date());
		//写入Excel的数据list
		List<CzzjInfo> list = capitalService.findAll(map);
		//List<CzzjInfo> list=new ArrayList<CzzjInfo>();
		System.out.println("结束时间:"+new Date());
		//数据列名
		String[] headers = { "序号", "人员归属", "所属街道", "编号", "姓名", "商保(元)",
				"商保补差(元)", "区补(元)", "生补(元)", "开户银行", "卡号" };
		//写入Excel
		try {
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet("sheet_1");
			sheet.setDefaultColumnWidth((short) 15);
			//设置格式
			int iFontSize = 12;
			String sFontname = "宋体";

			HSSFFont Headfont = wb.createFont();
			Headfont.setFontHeightInPoints((short) iFontSize); // 表头字体大小
			Headfont.setFontName(sFontname); // 表头字体名称
			Headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 表头字体加粗

			HSSFCellStyle HeadStyle = wb.createCellStyle(); // 表头格式
			HeadStyle.setFont(Headfont); // 表头字体
			HeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 表头位置
			
			HSSFFont bodyfont = wb.createFont();
			bodyfont.setFontHeightInPoints((short) 10); // 字体大小
			bodyfont.setFontName(sFontname); // 字体名称
			bodyfont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 字体加粗
			HSSFCellStyle bodyStyle = wb.createCellStyle(); // 格式
			bodyStyle.setFont(bodyfont); // 字体
			bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 位置
			
			
			int i = 0;
			while (i <= list.size()) {
				HSSFRow row = sheet.createRow(i);
				if (i == 0) {
					for (int j = 0; j < headers.length; j++) {
						HSSFCell cell = row.createCell((short) j);
						cell.setCellStyle(HeadStyle);
						cell.setCellValue(headers[j]);
					}
				} else {
					CzzjInfo cz = list.get(i-1);
					HSSFCell cell = row.createCell((short) 0);
					cell.setCellStyle(bodyStyle);
					cell.setCellValue(i);
					cell = row.createCell((short) 1);
					cell.setCellValue(cz.getRygs());
					cell = row.createCell((short) 2);
					cell.setCellValue(cz.getSsjd());
					cell = row.createCell((short) 3);
					cell.setCellValue(cz.getBh());
					cell = row.createCell((short) 4);
					cell.setCellValue(cz.getXm());
					cell = row.createCell((short) 5);
					cell.setCellValue(cz.getSbs());
					cell = row.createCell((short) 6);
					cell.setCellValue(cz.getSbbcs());
					cell = row.createCell((short) 7);
					cell.setCellValue(cz.getQbs());
					cell = row.createCell((short) 8);
					cell.setCellValue(cz.getSbs());
					cell = row.createCell((short) 9);
					cell.setCellValue(cz.getKhyh());
					cell = row.createCell((short) 10);
					cell.setCellValue(cz.getKh());
					
	
				}
				i++;
			}

			java.io.OutputStream out = response.getOutputStream();
			wb.write(out);
			out.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值