Copying an Internal table to the Clipboard

Nov 14th, 2009 | By Ole | Category: Featured, SAP ABAP

In the following example we select some material data from the transparent table MARA and copy the result to the clipboard.

The result is transferred to the clipboard with TABs as delimiters, which makes it possible to paste the result directly into Excel.

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
*&---------------------------------------------------------------------*
*& Report Z0OV_IT_TO_CLIPBOARD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT Z0OV_IT_TO_CLIPBOARD.

* Tabulator for separating fields.
CONSTANTS c_tab TYPE X VALUE '09'.

* Used in conversion to uni-code
FIELD-SYMBOLS: TYPE any.

TYPES:
t_line(1000) TYPE C.

DATA:
  it_query TYPE STANDARD TABLE OF mara,
  wa_query TYPE mara,
  it_mara TYPE STANDARD TABLE OF mara,
  wa_mara TYPE mara,
  it_table TYPE TABLE OF t_line,
  wa_table TYPE t_line,
  l_tab TYPE C,
  l_lines TYPE INT2,
  w_rc TYPE I.

* Populate the internal table it_query with the material numbers
* we will search for in the following query of the MARA table.
wa_query-matnr = '000000000000300007'.
APPEND wa_query TO it_query.
wa_query-matnr = '000000000000300008'.
APPEND wa_query TO it_query.

* Delete duplicates before running query (optimize the query)
DELETE ADJACENT DUPLICATES FROM it_query COMPARING matnr.

* Check if it_query is populated before running query.
IF NOT it_query[] IS INITIAL.
  SELECT *
  FROM mara
  INTO CORRESPONDING FIELDS OF TABLE it_mara
  FOR ALL ENTRIES IN it_query
  WHERE matnr = it_query-matnr.

* Build the delimiter
  ASSIGN l_tab TO CASTING TYPE X.
  <fs> = c_tab.

* Build the header row
  CONCATENATE 'Material' 'Material Type' 'Material Group' 'Base Unit of Measure'
  INTO wa_table SEPARATED BY l_tab.
  APPEND wa_table TO it_table.

* Loop the result set and append lines to the final result set
  LOOP AT it_mara INTO wa_mara.

    CONCATENATE wa_mara-matnr wa_mara-mtart wa_mara-matkl wa_mara-meins
    INTO wa_table SEPARATED BY l_tab.

    APPEND wa_table TO it_table.
  ENDLOOP.

* Export the result to clipboard
  CALL METHOD cl_gui_frontend_services=>clipboard_export
  IMPORTING
    data = it_table[]
  CHANGING
    rc = w_rc
  EXCEPTIONS
    cntl_error = 1
    error_no_gui = 2
    not_supported_by_gui = 3
    OTHERS = 4.

* Check and print how many rows we found.
  DESCRIBE TABLE it_mara LINES l_lines.
  WRITE:/ l_lines, ' lines pasted to clipboard.'.

ELSE.
  WRITE:/ 'Your query is empty!'.
ENDIF.

Keywords: ABAP, Internal table, Clipboard

     

Leave Comment

You must be logged in to post a comment.