导出Excel Java 使用Opi

导出按钮

<button type="button"  class="btn green btn-sm" id ="exportExcel">导出</button>

表单

		<form id="search_form" action="productController.do?product" method="post">
						<input type="hidden" id ="batch" name="batch" value="">
						<input type="hidden" id ="batchId" name="batchId" value="">
						<input type="hidden" id ="pageNo" name="pageNo" value="${pageNo}">
						<div class="clearfix">
							<div class="span14">

								<div class="span2">
									审核状态:
										<select id="status" name="status" class="span6">
											<option value="">全部</option>
											<option value="0">审核驳回</option>
											<option value="5">草稿</option>
											<option value="10">已取消</option>
											<option value="20">待审核</option>
											<option value="50">审核通过</option>
										</select>
								</div>
								<div class="span3">
									发布版块:
										<select id="catId" name="catId" class="span8" >
											<option value="0">全部</option>
										</select>
								</div>
								<div class="span4">
									发布时间:
									<input type="text" id="publishedStart" name="publishedStart" value="${searchMap['publishedStart']?if_exists[0]}" class="Wdate span4" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\'publishedEnd\')}'})" >
									<span style="margin: 0 10px">--</span>
									<input type="text" id="publishedEnd" name="publishedEnd" value="${searchMap['publishedEnd']?if_exists[0]}" class="Wdate span4" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'publishedStart\')}'})" >
								</div>
								<div class="span3">
									关键词:
									<input id="title" name="title" class="form-control span7" type="text" value="${searchMap["title"]?if_exists[0]}" style="margin-bottom:0px;">
								</div>

							</div>
							<div class="span3">
								来源:
								<select id="userType" name="userType" class="span8" >
									<option value="">全部</option>
									<option value="0">前台</option>
									<option value="1">后台</option>
								</select>
							</div>
							<div class="pull-right" style="margin-right: 20px;">

								<button type="button" class="btn yellow btn-sm" onclick="toSearch('productController.do?product', 'search_form');">搜索  <i class="icon-search m-icon-white"></i></button>
								<button type="button" class="btn yellow btn-sm" onclick="resetForm('search_form');">重置<i class="icon-repeat m-icon-white"></i></button>
								<button type="button"  class="btn green btn-sm" id ="exportExcel">导出</button>

								<input type="hidden" name="Expires" id="Expires" value="0" />
							</div>
						</div>
					</form>
<script type="text/javascript">

	$("#exportExcel").click(function () {
		$("#Expires").val("1");
		$("#search_form").submit();
		$("#Expires").val("0");
	})

</script>

控制层

/**
	 * Excel 导出
	 * @param resultList
	 * @param response
	 */
	public void exportProduct(List<Map<String, Object>> resultList,HttpServletResponse response){
		String[] titles ={"标题","发布版块","发布人","发布时间","浏览数","点赞数","状态"};
		String[] params = {"title","catName","author","time","pv","supportCount","contentStatusStr"};
		String sheetName = "产品成果与交流";
		String fileName = "产品成果与交流"+DateUtil.getStringDateShort()+".xls";

		HSSFWorkbook wb = ExcelUtils.exportExcel(resultList, titles, params, sheetName);
		//响应到客户端
		try {
			setResponseHeader(response, fileName);
			OutputStream os = response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

工具类

 /**
     *
     * @param resultList 数据集
     * @param titles excel标题
     * @param params excel标题对应参数值
     * @param fileName excel文件名
     * @param sheetName sheet名
     */
    public static HSSFWorkbook exportExcel(List<Map<String,Object>> resultList, String[] titles, String[] params , String sheetName) {
        HSSFWorkbook wb = null;

        if (resultList != null && resultList.size() > 0) {
            String content[][] = new String[resultList.size()][params.length];
            for (int i = 0; i < resultList.size(); i++) {
                Map<String, Object> map = resultList.get(i);
                    for (int j = 0; j < params.length; j++) {
                        if(map!= null){
                            if (map.get(params[j])!=null){
                                content[i][j] = map.get(params[j]).toString();
                            }
                        }
                }
            }
            //创建HSSFWorkbook
            wb = getHSSFWorkbook(sheetName, titles, content, null);

        }
        return wb;
    }
	public List<Map<String, Object>>  fullDataForExport(List<Map<String, Object>> list){
		for (Map<String,Object> item :list){
			String contentStatus = (String)item.get("contentStatus");
			Object support = item.get("support");
			Object oppose = item.get("oppose");
			Integer supportInt = 0;
			Integer opposeInt = 0;

			if( support !=null){
				supportInt =(Integer)support;
			}

			if(oppose !=null){
				opposeInt = (Integer)oppose;
			}

			item.put("supportCount",supportInt-opposeInt);
			String contentStatusStr = "";
			if (StringUtils.isBlank(contentStatus)){
				continue;
			}
			switch(contentStatus){
				case ContentStatus.CONTENT_PUBLISHED: contentStatusStr = "审核通过";break;
				case ContentStatus.CONTENT_PENDING_TRIAL: contentStatusStr = "待审核";break;
				case ContentStatus.CONTENT_DRAFT: contentStatusStr = "草稿";break;
				case ContentStatus.DEMAND_PASS: contentStatusStr = "审核驳回";break;
				case ContentStatus.CONTENT_CANCEL: contentStatusStr = "已取消";break;
			}
			item.put("contentStatusStr",contentStatusStr);

		}
		return list;
	}

响应方法

/**
	 * 发送响应流方法
	 */
	public  void setResponseHeader(HttpServletResponse response, String fileName) {

		if (StringUtils.isBlank(fileName)){
			return;
		}
		if (response == null){
			return;
		}
		try {
			response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值