Method export SAP database table to DBF File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
params:
IV_DBTABLE  Importing Type  TABNAME
IV_FILENAME Importing Type  LOCALFILE

METHOD export_table_to_dbf.
  TYPES: BEGIN OF ty_flditab,
          fldname TYPE dfies-fieldname,
         END OF ty_flditab.

  DATA: lt_dfies TYPE TABLE OF dfies,
        lt_dyn_table TYPE REF TO data,
        lt_fieldcat TYPE lvc_t_fcat,
        lv_filename TYPE string,
        lv_cpcodepage TYPE cpcodepage,
        lv_abap_codepage TYPE abap_encod,
        lt_flditab TYPE TABLE OF ty_flditab.

  FIELD-SYMBOLS:
                 <fs_flditab> LIKE LINE OF lt_flditab,
                 <fs_fieldcat> LIKE LINE OF lt_fieldcat,
                 <dyn_tab> TYPE STANDARD TABLE.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name   = iv_dbtable
      i_bypassing_buffer = 'X'
    CHANGING
      ct_fieldcat        = lt_fieldcat.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = lt_fieldcat
    IMPORTING
      ep_table        = lt_dyn_table.

  ASSIGN lt_dyn_table->* TO <dyn_tab>.

  SELECT *
    "UP TO 100 ROWS
    INTO TABLE <dyn_tab>
    FROM (iv_dbtable).

  IF sy-subrc = 0.
    LOOP AT lt_fieldcat ASSIGNING <fs_fieldcat>.
      APPEND <fs_fieldcat>-fieldname TO lt_flditab.
    ENDLOOP.
  ENDIF.

  CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
    EXPORTING
      external_name = 'windows-1251'
      kind          = 'H'
    IMPORTING
      sap_codepage  = lv_cpcodepage
    EXCEPTIONS
      not_found     = 1
      OTHERS        = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    WRITE lv_cpcodepage TO lv_abap_codepage.
  ENDIF.

  CONCATENATE iv_filename '.DBF' INTO lv_filename.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = lv_filename
      filetype                = 'DBF'
      codepage                = lv_abap_codepage
    TABLES
      data_tab                = <dyn_tab>
      fieldnames              = lt_flditab
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      OTHERS                  = 22.
ENDMETHOD.

Добавить комментарий

Ваш адрес email не будет опубликован.