Ejemplo n.º 1
0
  public CopyOfExportTask(
      String connectUrls,
      String queryProcedure,
      Map<String, String> params,
      String resultProcedure,
      HttpServletRequest request) {
    this.connectUrls = connectUrls;

    this.params = new HashMap<String, String>();
    Object[] keys = params.keySet().toArray();
    this.keys = new String[keys.length];
    for (int i = 0; i < keys.length; i++) { // 把map里所有key转成小写,与执行的存储过程保持一致
      String key = keys[i].toString();
      this.keys[i] = key.toLowerCase();
      this.params.put(key.toLowerCase(), MapUtils.getString(params, key, ""));
    }
    this.queryProcedure = handleSql(queryProcedure.toLowerCase(), this.params);
    this.resultProcedure = resultProcedure.toLowerCase();
    this.fileName =
        MapUtils.getString(params, "modulename", "导出文件")
            + "_"
            + new SimpleDateFormat(Constant.TIMESTAMP_FORMAT_yyyyMMddHHmmssSSS).format(new Date())
            + Constant.EXCEL;
    this.ip = request.getServerName();
    this.port = request.getServerPort();
    this.filePath =
        request.getRealPath(Constant.EXPORT_PATH_FOLDER) + File.separatorChar + this.fileName;
    this.jt = DBUtil.getReadJdbcTemplate(this.connectUrls);
  }
Ejemplo n.º 2
0
 private void updateExportResult() {
   JdbcTemplate writeJt = DBUtil.getWriteJdbcTemplate(connectUrls);
   writeResult(
       writeJt,
       resultProcedure,
       this.params,
       "http://" + this.ip + ":" + this.port + "/export/download/" + fileName,
       "");
 }
Ejemplo n.º 3
0
  @Override
  public void run() {
    try {
      int total = getCount(jt, queryProcedure); // 总记录数
      if (total == 0) {
        throw new Exception("没有能导出的数据!");
      }

      double totalPages = getPages(total);

      SXSSFWorkbook wb = null;
      FileInputStream fis = null;

      for (int i = 0; i < totalPages; i++) { // 总共要查询数据库的次数
        List<Map<String, Object>> list = query(jt, queryProcedure, pageCount, i + 1); // 查询分页数据
        if (list != null && list.size() != 0) {
          if (this.titles == null) { // 设置标题数组,方便以后使用
            handleTitles(list); // 处理标题数组
          }

          File file = new File(filePath);
          if (!file.exists()) {
            wb = createWorkbook(); // 生成excel对象
          }
          POIUtil.exportPerpareShit(list, wb, this.titles); // 将本分页数据写入excel对象
        }
      }
      writeToFile(wb); // 将excel对象写入硬盘的excel文件

      updateExportResult(); // 更新导出结果

    } catch (Exception e) {
      JdbcTemplate writeJt = DBUtil.getWriteJdbcTemplate(connectUrls);
      writeResult(writeJt, resultProcedure, this.params, "", e.getMessage());
    }
  }