springMVC - 页面excel 数据全量导出及按条件导出代码实战

1、JSP部分代码:

//==========================导出excel功能==================================

        <cas:havePerm url="contPageEpt.do">
        //导出功能时间控件
        $('#eptDate').daterangepicker({
            "showDropdowns": true,
            "showCustomRangeLabel": false,
            "alwaysShowCalendars": true,
            "startDate": moment().subtract('days', 7),
            "endDate": moment().subtract('days', 1),
            "opens": "left",
            "drops": "down"
        }, function (start, end, label) {//时间改变后执行该方法
            //alert(label);
        });

        /*搜索条件导出*/
        $("#search_export").on("click", function () {
            var dateRange1 = $('#eptDate').val().replace(/\s/g, "").split("");
            var startDate1 = dateRange1[0];
            var endDate1 = dateRange1[1];
            var srcContType = $("#srcContType").val();
            var searchKey = $("#searchKey").val();
            window.location = "<c:url value='contPageExport.do'/>" + "?startDate=" + startDate1 + "&endDate=" + endDate1 + "&srcContType=" + srcContType + "&searchKey=" + searchKey;
        })

        /*整体导出*/
        $("#export").on("click", function () {
            var dateRange1 = $('#eptDate').val().replace(/\s/g, "").split("");
            var startDate1 = dateRange1[0];
            var endDate1 = dateRange1[1];
            var appId = $("#appId").val();
            var srcContType = $("#srcContType").val();
            window.location = "<c:url value='contPageEpt.do'/>" + "?startDate=" + startDate1 + "&endDate=" + endDate1 + "&appId=" + appId + "&srcContType=" + srcContType;
        })
        </cas:havePerm>
//=============================导出功能结束====================================  

2、struts.xml 配置

<!--整体导出-->
<action name="contPageEpt" class="bigdata.action.ContPageDayAction" method="export">
</action>

<!--按搜索条件导出-->
<action name="contPageExport" class="bigdata.action.ContPageDayAction" method="searchExport">
</action>

3、action部分代码:

public void export() {
      Map<String, String> params = getParams();
      HttpServletResponse response = getResponse();
      response.setContentType("application/x-download");
      try {
         response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("内容受访明细", "UTF-8") + ".xls");
      } catch (UnsupportedEncodingException e1) {
         e1.printStackTrace();
      }
      ServletOutputStream outputStream = null;
      try {
         outputStream = response.getOutputStream();
         makeExcel(params, outputStream);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public void makeExcel(Map<String, String> params, ServletOutputStream outputStream) throws IOException {
      List<ContPageDay> list = this.contPageDayService.expList(params);
      HSSFWorkbook workbook = new HSSFWorkbook();
      HSSFSheet sheet = workbook.createSheet("内容受访明细");
      sheet.setColumnWidth(0, 20 * 256);
      sheet.setColumnWidth(1, 20 * 256);
      sheet.setColumnWidth(3, 20 * 256);
      sheet.setColumnWidth(4, 20 * 256);
      sheet.setColumnWidth(5, 20 * 256);
      sheet.setColumnWidth(6, 20 * 256);

      Row row;
      Cell cell;

      row = sheet.createRow(0);

      HSSFFont font = workbook.createFont();
      font.setColor(HSSFColor.RED.index);
      font.setFontHeightInPoints((short) 12);
      font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

      HSSFCellStyle style1 = workbook.createCellStyle();
      style1.setFont(font);
      style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

      row.setRowStyle(style1);
      row.setHeight((short) 350);

      cell = row.createCell(0);
      cell.setCellValue("日期");
      cell.setCellStyle(style1);

      cell = row.createCell(1);
      cell.setCellValue("a名称");
      cell.setCellStyle(style1);

      cell = row.createCell(2);
      cell.setCellValue("b名称");
      cell.setCellStyle(style1);

      cell = row.createCell(3);
      cell.setCellValue("c名称");
      cell.setCellStyle(style1);

      cell = row.createCell(4);
      cell.setCellValue("PV");
      cell.setCellStyle(style1);

      cell = row.createCell(5);
      cell.setCellValue("UV");
      cell.setCellStyle(style1);

      cell = row.createCell(6);
      cell.setCellValue("IP");
      cell.setCellStyle(style1);

      for (int i = 0; i < list.size(); i++) {
         row = sheet.createRow(i + 1);
         cell = row.createCell(0);
         cell.setCellValue(list.get(i).getStatDate());

         cell = row.createCell(1);
         cell.setCellValue(list.get(i).getAName());

         cell = row.createCell(2);
         cell.setCellValue(list.get(i).getBName());

         cell = row.createCell(3);
         cell.setCellValue(list.get(i).getCName());

         cell = row.createCell(4);
         cell.setCellValue(list.get(i).getPv());

         cell = row.createCell(5);
         cell.setCellValue(list.get(i).getUv());

         cell = row.createCell(6);
         cell.setCellValue(list.get(i).getIp());
      }

      workbook.write(outputStream);

      outputStream.flush();
      outputStream.close();
   }

   /*------------------------------------按搜索条件导出--start------------------------------------------------*/
   public void searchExport() {
      Map<String, String> params = getParams();
      HttpServletResponse response = getResponse();
      response.setContentType("application/x-download");
      try {
         response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("内容受访明细", "UTF-8") + ".xls");
      } catch (UnsupportedEncodingException e1) {
         e1.printStackTrace();
      }
      ServletOutputStream outputStream = null;
      try {
         outputStream = response.getOutputStream();
         searchExcel(params, outputStream);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public void searchExcel(Map<String, String> searchParams, ServletOutputStream outputStream) throws IOException {
      List<Map<String, Object>> list = this.contPageDayService.searchList(searchParams);
      HSSFWorkbook workbook = new HSSFWorkbook();
      HSSFSheet sheet = workbook.createSheet("内容受访明细");
      sheet.setColumnWidth(0, 20 * 256);
      sheet.setColumnWidth(1, 20 * 256);
      sheet.setColumnWidth(3, 20 * 256);
      sheet.setColumnWidth(4, 20 * 256);
      sheet.setColumnWidth(5, 20 * 256);
      sheet.setColumnWidth(6, 20 * 256);
      sheet.setColumnWidth(7, 20 * 256);
      sheet.setColumnWidth(8, 20 * 256);
      sheet.setColumnWidth(9, 20 * 256);

      Row row;
      Cell cell;

      row = sheet.createRow(0);

      HSSFFont font = workbook.createFont();
      font.setColor(HSSFColor.RED.index);
      font.setFontHeightInPoints((short) 12);
      font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

      HSSFCellStyle style1 = workbook.createCellStyle();
      style1.setFont(font);
      style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

      row.setRowStyle(style1);
      row.setHeight((short) 350);

      cell = row.createCell(0);
      cell.setCellValue("日期");
      cell.setCellStyle(style1);

      cell = row.createCell(1);
      cell.setCellValue("a名称");
      cell.setCellStyle(style1);

      cell = row.createCell(2);
      cell.setCellValue("b名称");
      cell.setCellStyle(style1);

      cell = row.createCell(3);
      cell.setCellValue("c名称");
      cell.setCellStyle(style1);

      cell = row.createCell(4);
      cell.setCellValue(" Android (PV) ");
      cell.setCellStyle(style1);

      cell = row.createCell(5);
      cell.setCellValue(" Android (UV) ");
      cell.setCellStyle(style1);

      cell = row.createCell(6);
      cell.setCellValue(" Android (IP) ");
      cell.setCellStyle(style1);

      cell = row.createCell(7);
      cell.setCellValue(" IOS (PV) ");
      cell.setCellStyle(style1);

      cell = row.createCell(8);
      cell.setCellValue(" IOS (UV) ");
      cell.setCellStyle(style1);

      cell = row.createCell(9);
      cell.setCellValue(" IOS (IP) ");
      cell.setCellStyle(style1);

      for (int i = 0; i < list.size(); i++) {
         row = sheet.createRow(i + 1);
         cell = row.createCell(0);
         cell.setCellValue(String.valueOf(list.get(i).get("statDate")));

         cell = row.createCell(1);
         cell.setCellValue(String.valueOf(list.get(i).get("aName")));

         cell = row.createCell(2);
         cell.setCellValue(String.valueOf(list.get(i).get("bName")));

         cell = row.createCell(3);
         cell.setCellValue(String.valueOf(list.get(i).get("cName")));

         cell = row.createCell(4);
         cell.setCellValue(String.valueOf(null == list.get(i).get("apv") ? "0" : list.get(i).get("apv")));

         cell = row.createCell(5);
         cell.setCellValue(String.valueOf(null == list.get(i).get("auv") ? "0" : list.get(i).get("auv")));

         cell = row.createCell(6);
         cell.setCellValue(String.valueOf(null == list.get(i).get("aip") ? "0" : list.get(i).get("aip")));

         cell = row.createCell(7);
         cell.setCellValue(String.valueOf(null == list.get(i).get("bpv") ? "0" : list.get(i).get("bpv")));

         cell = row.createCell(8);
         cell.setCellValue(String.valueOf(null == list.get(i).get("buv") ? "0" : list.get(i).get("buv")));

         cell = row.createCell(9);
         cell.setCellValue(String.valueOf(null == list.get(i).get("bip") ? "0" : list.get(i).get("bip")));
      }

      workbook.write(outputStream);

      outputStream.flush();
      outputStream.close();
   }
/*------------------------------------按搜索条件导出--end------------------------------------------------*/

   public Map<String, String> getParams() {
      Map<String, String> map = new HashMap<String, String>();
      String appId = getRequest().getParameter("appId");
      if (StringUtils.isNotBlank(appId)) {
         map.put("appId", appId);
      }
      String clientVersion = getRequest().getParameter("clientVersion");
      if (StringUtils.isNotBlank(clientVersion)) {
         map.put("clientVersion", clientVersion);
      }
      String platform = getRequest().getParameter("platform");
      if (StringUtils.isNotBlank(platform)) {
         map.put("platform", platform);
      }
      String clientChannel = getRequest().getParameter("clientChannel");
      if (StringUtils.isNotBlank(clientChannel)) {
         map.put("clientChannel", clientChannel);
      }
      String date = getRequest().getParameter("date");
      if (StringUtils.isNotBlank(date)) {
         map.put("date", date.replaceAll("-", ""));
      }
      String searchKey = getRequest().getParameter("searchKey");
      if (StringUtils.isNotBlank(searchKey)) {
         map.put("searchKey", searchKey);
      }
      String srcContType = getRequest().getParameter("srcContType");
      if (StringUtils.isNotBlank(srcContType)) {
         map.put("srcContType", srcContType);
      }
      String startDate = getRequest().getParameter("startDate");
      if (StringUtils.isNotBlank(startDate)) {
         map.put("startDate", startDate.replaceAll("-", ""));
      }
      String endDate = getRequest().getParameter("endDate");
      if (StringUtils.isNotBlank(endDate)) {
         map.put("endDate", endDate.replaceAll("-", ""));
      }
      String menuId = getRequest().getParameter("aId");
      if (StringUtils.isNotBlank(menuId)) {
         map.put("aId", menuId);
      }
      String areaId = getRequest().getParameter("bId");
      if (StringUtils.isNotBlank(areaId)) {
         map.put("bId", areaId);
      }
      String contId = getRequest().getParameter("cId");
      if (StringUtils.isNotBlank(contId)) {
         map.put("cId", contId);
      }
      return map;
   }

4、dao代码部分:

public interface ContPageDayDao {
   public List<ContPageDay> expList(Map<String, String> params);
   public List<Map<String, Object>> searchList(Map<String, String> searchParams);
 }

5、impl接口实现代码部分:

@Override
public List<ContPageDay> expList(Map<String, String> params) {
   List<Object> sqlParams = new ArrayList<Object>();
   StringBuffer queryString = new StringBuffer("select statdate,aName,bName,cName,sum(pv) pv,sum(uv) uv,sum(ip) ip from cont_page_day where 1=1 ");

   String startDate = params.get("startDate");
   if (StringUtils.isNotBlank(startDate)) {
      queryString.append("and statdate>=? ");
      sqlParams.add(Long.valueOf(startDate));
   }

   String endDate = params.get("endDate");
   if (StringUtils.isNotBlank(endDate)) {
      queryString.append("and statdate<=? ");
      sqlParams.add(Long.valueOf(endDate));
   }

   String appId = params.get("appId");
   if (StringUtils.isNotBlank(appId)) {
      queryString.append("and (md = ? or md = ? or md = ? )");
      sqlParams.add(appId + "-1");
      sqlParams.add(appId + "-2");
      sqlParams.add(appId + "-3");
   }

   String srcContType = params.get("srcContType");
   if (StringUtils.isNotBlank(srcContType)) {
      queryString.append("and srcContType=? ");
      sqlParams.add(Integer.valueOf(srcContType));
   }

   queryString.append("group by aId,aName,bId,bName,cId,cName,statdate order by statdate desc");

   List<ContPageDay> dtos = new ArrayList<ContPageDay>();
   List<Map<String, Object>> results = query(queryString.toString(), sqlParams.toArray());

   for (Map<String, Object> result : results) {
      ContPageDay dto = new ContPageDay();
      dto.setStatDate((Long) result.get("statdate"));
      dto.setAName((String) result.get("aName"));
      dto.setBName((String) result.get("bName"));
      dto.setCName((String) result.get("cName"));
      dto.setIp(Long.valueOf(result.get("ip").toString()));
      dto.setPv(Long.valueOf(result.get("pv").toString()));
      dto.setUv(Long.valueOf(result.get("uv").toString()));
      dtos.add(dto);
   }
   return dtos;
}

/*--------------------------------------------------按条件搜索--begin---------------------------------------------------------*/
@Override
public List<Map<String, Object>> searchList(Map<String, String> searchParams) {
   List<Object> sqlParams = new ArrayList<Object>();
   StringBuffer queryString = new StringBuffer(
         "SELECT * FROM ( " +
               " SELECT " +
               "a.statDate," +
               "a.aName," +
               "a.bName," +
               "a.cName," +
               "a.aId," +
               "a.bId," +
               "a.cId," +
               "a.pv apv," +
               "a.uv auv," +
               "a.ip aip," +
               "b.pv bpv," +
               "b.uv buv," +
               "b.ip bip " +
               "FROM ( " +
               "SELECT * FROM cont_page_day " +
               "WHERE statdate BETWEEN ? AND ? " +
               "AND md = '3-1' " +
               "AND ( aName LIKE ? " +
               "OR bName LIKE ? " +
               "OR cName LIKE ?)) a " +
               "LEFT JOIN(" +
               "SELECT * FROM cont_page_day " +
               "WHERE statdate BETWEEN ? AND ? " +
               "AND md = '3-2' " +
               "AND (" +
               "aName LIKE ? " +
               "OR bName LIKE ? " +
               "OR cName LIKE ? )) b " +
               "ON a.statDate = b.statDate " +
               "AND a.aName = b.aName " +
               "AND a.bName = b.bName " +
               "AND a.cName = b.cName " +
               "AND a.aId = b.aId " +
               "AND a.bId = b.bId " +
               "AND a.cId = b.cId " +
               "UNION ALL " +
               "SELECT " +
               "e.statDate," +
               "e.aName," +
               "e.bName," +
               "e.cName," +
               "e.aId," +
               "e.bId," +
               "e.cId," +
               "e.pv bpv," +
               "e.uv buv," +
               "e.ip bip," +
               "d.pv apv," +
               "d.uv auv," +
               "d.ip aip " +
               "FROM ( SELECT * FROM cont_page_day " +
               "WHERE statdate BETWEEN ? AND ? " +
               "AND md = '3-1' " +
               "AND ( aName LIKE ? " +
               "OR bName LIKE ? " +
               "OR cName LIKE ?" +
               ")) d " +
               "RIGHT JOIN (" +
               " SELECT * FROM cont_page_day " +
               "WHERE statdate BETWEEN ? AND ? " +
               "AND md = '3-2' " +
               "AND ( aName LIKE ? " +
               "OR bName LIKE ? " +
               "OR cName LIKE ?" +
               ")) e " +
               "ON d.statdate = e.statdate " +
               "AND d.aName = e.aName " +
               "AND d.bName = e.bName " +
               "AND d.cName = e.cName " +
               "AND d.aId = e.aId " +
               "AND d.bId = e.bId " +
               "AND d.cId = e.cId " +
               "WHERE d.aName IS NULL " +
               "AND d.bName IS NULL " +
               "AND d.cName IS NULL " +
               "AND d.aId IS NULL " +
               "AND d.bId IS NULL " +
               "AND d.cId IS NULL" +
               ") f " +
               "ORDER BY " +
               "statDate ASC");

   String startDateQuery = "";
   String endDateQuery = "";
   String searchKeyQuery = "";

   String startDate = searchParams.get("startDate");
   if (StringUtils.isNotBlank(startDate)) {
      startDateQuery = startDate;
   }

   String endDate = searchParams.get("endDate");
   if (StringUtils.isNotBlank(endDate)) {
      endDateQuery = endDate;
   }

   String searchKey = searchParams.get("searchKey");
   if (StringUtils.isNotBlank(searchKey)) {
      searchKeyQuery = searchKey;
   }

   /*sql中有几个问号对应几条数据-4*5-*/
   for (int i = 0; i < 4; i++) {
      sqlParams.add(Long.valueOf(startDateQuery));
      sqlParams.add(Long.valueOf(endDateQuery));
      for (int j = 0; j < 3; j++) {
         sqlParams.add("%" + String.valueOf(searchKeyQuery) + "%");
      }
   }

   List<Map<String, Object>> results = query(queryString.toString(), sqlParams.toArray());

   return results;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢子墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值