public void resultsetToExcel()

in linkis-public-enhancements/linkis-pes-publicservice/src/main/java/org/apache/linkis/filesystem/restful/api/FsRestfulApi.java [875:986]


  public void resultsetToExcel(
      HttpServletRequest req,
      HttpServletResponse response,
      @RequestParam(value = "path", required = false) String path,
      @RequestParam(value = "charset", defaultValue = "utf-8") String charset,
      @RequestParam(value = "outputFileType", defaultValue = "csv") String outputFileType,
      @RequestParam(value = "csvSeperator", defaultValue = ",") String csvSeperator,
      @RequestParam(value = "csvSeparator", defaultValue = ",") String csvSeparator,
      @RequestParam(value = "quoteRetouchEnable", required = false) boolean quoteRetouchEnable,
      @RequestParam(value = "outputFileName", defaultValue = "downloadResultset")
          String outputFileName,
      @RequestParam(value = "sheetName", defaultValue = "result") String sheetName,
      @RequestParam(value = "nullValue", defaultValue = "NULL") String nullValue,
      @RequestParam(value = "limit", defaultValue = "0") Integer limit,
      @RequestParam(value = "autoFormat", defaultValue = "false") Boolean autoFormat,
      @RequestParam(value = "keepNewline", defaultValue = "false") Boolean keepNewline)
      throws WorkSpaceException, IOException {
    ServletOutputStream outputStream = null;
    FsWriter fsWriter = null;
    PrintWriter writer = null;
    FileSource fileSource = null;
    if (csvSeparator.equals(",") && !csvSeperator.equals(",")) {
      csvSeparator = csvSeperator;
    }
    LOGGER.info(
        "resultsetToExcel with outputFileType:{}, csvSeparator:{}, quoteRetouchEnable:{}, charset:{}",
        outputFileType,
        csvSeparator,
        quoteRetouchEnable,
        charset);
    try {
      String userName = ModuleUserUtils.getOperationUser(req, "resultsetToExcel " + path);
      LoggerUtils.setJobIdMDC("resultsetToExcelThread_" + userName);
      LOGGER.info("userName {} start to resultsetToExcel File {}", userName, path);
      FsPath fsPath = new FsPath(path);
      FileSystem fileSystem = fsService.getFileSystemForRead(userName, fsPath);
      boolean isLimitDownloadSize = RESULT_SET_DOWNLOAD_IS_LIMIT.getValue();
      Integer csvDownloadSize = RESULT_SET_DOWNLOAD_MAX_SIZE_CSV.getValue();
      Integer excelDownloadSize = RESULT_SET_DOWNLOAD_MAX_SIZE_EXCEL.getValue();
      if (limit > 0) {
        csvDownloadSize = limit;
        excelDownloadSize = limit;
      }

      if (StringUtils.isEmpty(path)) {
        throw WorkspaceExceptionManager.createException(80004, path);
      }
      if (!checkIsUsersDirectory(path, userName)) {
        throw WorkspaceExceptionManager.createException(80010, userName, path);
      }
      response.addHeader(
          "Content-Disposition",
          "attachment;filename="
              + new String(outputFileName.getBytes("UTF-8"), "ISO8859-1")
              + "."
              + outputFileType);
      response.setCharacterEncoding(charset);
      outputStream = response.getOutputStream();
      // 前台传""会自动转为null
      if (nullValue != null && BLANK.equalsIgnoreCase(nullValue)) nullValue = "";
      fileSource = FileSource$.MODULE$.create(fsPath, fileSystem).addParams("nullValue", nullValue);
      switch (outputFileType) {
        case "csv":
          if (FileSource$.MODULE$.isTableResultSet(fileSource)) {
            fsWriter =
                CSVFsWriter.getCSVFSWriter(
                    charset, csvSeparator, quoteRetouchEnable, outputStream, keepNewline);
          } else {
            fsWriter =
                ScriptFsWriter.getScriptFsWriter(new FsPath(outputFileType), charset, outputStream);
          }
          response.addHeader("Content-Type", "text/plain");
          if (isLimitDownloadSize) {
            fileSource = fileSource.page(1, csvDownloadSize);
          }
          break;
        case "xlsx":
          if (!FileSource$.MODULE$.isTableResultSet(fileSource)) {
            throw WorkspaceExceptionManager.createException(80024);
          }
          fsWriter =
              ExcelFsWriter.getExcelFsWriter(
                  charset, sheetName, DEFAULT_DATE_TYPE, outputStream, autoFormat);
          response.addHeader("Content-Type", XLSX_RESPONSE_CONTENT_TYPE);
          if (isLimitDownloadSize) {
            fileSource = fileSource.page(1, excelDownloadSize);
          }
          break;
        default:
          WorkspaceExceptionManager.createException(80015);
      }
      fileSource.write(fsWriter);
      fsWriter.flush();
      LOGGER.info("userName {} Finished to resultsetToExcel File {}", userName, path);
    } catch (Exception e) {
      LOGGER.error("output failed", e);
      response.reset();
      response.setCharacterEncoding(Consts.UTF_8.toString());
      response.setContentType("text/plain; charset=utf-8");
      writer = response.getWriter();
      writer.append("error(错误):" + e.getMessage());
      writer.flush();
    } finally {
      LoggerUtils.removeJobIdMDC();
      if (outputStream != null) {
        outputStream.flush();
      }
      IOUtils.closeQuietly(fsWriter);
      IOUtils.closeQuietly(fileSource);
      IOUtils.closeQuietly(writer);
    }
  }