ABAP EXCEL 转 PDF

DATA: application TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object.
IF iv_pdf IS NOT INITIAL.
CREATE OBJECT application ‘EXCEL.APPLICATION’.
CALL METHOD OF application ‘WORKBOOKS’ = workbook.
CALL METHOD OF workbook ‘OPEN’
EXPORTING
#1 = output_path_xlsx.
CALL METHOD OF application ‘WORKSHEETS’ = sheet
EXPORTING
#1 = 1. "Sheet1
CALL METHOD OF sheet ‘ACTIVATE’.

  output_path_pdf = output_path_xlsx.
  REPLACE '.xlsx' WITH '.pdf' INTO output_path_pdf.
  REPLACE '.XLSX' WITH '.pdf' INTO output_path_pdf.
  CALL METHOD OF sheet 'ExportAsFixedFormat'
    EXPORTING
      #1 = 0
      #2 = output_path_pdf.


  CALL METHOD OF workbook 'CLOSE'.
  CALL METHOD OF application 'QUIT'.
  FREE OBJECT: application,workbook,sheet.

  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 = output_path_pdf
    RECEIVING
      result               = DATA(l_res)
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF sy-subrc <> 0 OR l_res = space.
    MESSAGE |PDF另存失败| TYPE 'E'.
  ELSE.
    MESSAGE |PDF另存为{ output_path_pdf }| TYPE 'S'.
  ENDIF.


  CALL METHOD cl_gui_frontend_services=>file_delete
    EXPORTING
      filename             = output_path_xlsx
    CHANGING
      rc                   = l_dummy_rc
    EXCEPTIONS
      file_delete_failed   = 1
      cntl_error           = 2
      error_no_gui         = 3
      file_not_found       = 4
      access_denied        = 5
      unknown_error        = 6
      not_supported_by_gui = 7
      wrong_parameter      = 8
      OTHERS               = 9.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDIF.

ENDMETHOD.

你可能感兴趣的:(excel,ABAP)