Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Order the columns of an output Excel file

0 Kudos
830

Hi guys,

I have a problem with the creation of an excel file.

There is a database table where information about materials is stored (material number, description, price, weight, etc.).
I want to export a part of this table based on a customizing table.
In this customizing table I can mark the fields that shall be selected from the materials table and the order of the fields in excel file that shall be created.

For example, I mark material number, price and weight and set the order for the output file as 1.material number 2.weight 3.price.
Now I want to select the marked fields from the material table and create an output excel to export the data in the correct order.

I have no idea how to code this. Should this be done with a dynamic select statement?
Because the fields that shall be selected fields can vary from time to time.
And if I select all the material data, I dont know ho to get the wanted data from the result based on the customizing?

Could you help me please?
Thanks and best regards.

Chris

1 ACCEPTED SOLUTION
Read only

MateuszAdamus
Active Contributor
713

Hello mydigitalme

Sure, you can do it with a dynamic SELECT.

SELECT fieldname
  FROM zconfig
  WHERE export = @abap_true
  ORDER BY field_order
  INTO TABLE @DATA(lt_fields).
IF sy-subrc = 0.
  SELECT (lt_fields)
    FROM zmaterial
    INTO TABLE @DATA(lt_materials).

  " export material data to Excel
ENDIF.

If you select all data from DB you can then assign selected fields using the ASSIGN COMPONENT keyword.

FIELD-SYMBOLS:
  <lv_field> TYPE ANY.

LOOP AT lt_materials REFERENCE INTO DATA(ld_material).
  LOOP AT lt_fields INTO DATA(lv_field).
    ASSIGN COMPONENT lv_field OF STRUCTURE ld_material->* TO <lv_field>.
    IF sy-subrc = 0.
      " do something with the field value here
    ENDIF.
  ENDLOOP.
ENDLOOP.

I'd probably go with the first approach as it's not downloading data which is not used later on. But you should validate that LT_FIELDS contains column names only.

Kind regards,

Mateusz
4 REPLIES 4
Read only

MateuszAdamus
Active Contributor
714

Hello mydigitalme

Sure, you can do it with a dynamic SELECT.

SELECT fieldname
  FROM zconfig
  WHERE export = @abap_true
  ORDER BY field_order
  INTO TABLE @DATA(lt_fields).
IF sy-subrc = 0.
  SELECT (lt_fields)
    FROM zmaterial
    INTO TABLE @DATA(lt_materials).

  " export material data to Excel
ENDIF.

If you select all data from DB you can then assign selected fields using the ASSIGN COMPONENT keyword.

FIELD-SYMBOLS:
  <lv_field> TYPE ANY.

LOOP AT lt_materials REFERENCE INTO DATA(ld_material).
  LOOP AT lt_fields INTO DATA(lv_field).
    ASSIGN COMPONENT lv_field OF STRUCTURE ld_material->* TO <lv_field>.
    IF sy-subrc = 0.
      " do something with the field value here
    ENDIF.
  ENDLOOP.
ENDLOOP.

I'd probably go with the first approach as it's not downloading data which is not used later on. But you should validate that LT_FIELDS contains column names only.

Kind regards,

Mateusz
Read only

0 Kudos
713

Hi Mateusz,

thank you very much for your answer.

That dynamic select looks promissing, but unfortunately I am getting the following error message on the line

INTO TABLE @DATA(lt_materials).

"Inline data declarations are possible only if projection list and FROM clause are specified statically and not all host variables are typed generically."

Do oyu have any idea how I can fix that?

Kind regards
Christian

Read only

0 Kudos
713

In this case you will need to create the LT_MATERIALS table before selecting data into it based on the LT_FIELDS values.

You can do it using the RTSS as shown in this wiki entry: https://wiki.scn.sap.com/wiki/display/Snippets/Example%20-%20create%20a%20dynamic%20internal%20table


Kind regards,
Mateusz
Read only

713

I'll go with the ASSIGN COMPONENT solution, this is working fine.
Thanks a lot.

Kind Regards
Christian