Application Development 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: 

Is it possible that the column layout of an ALV grid matches the excel file of it?

walkerist
Participant
0 Kudos
665

Hi, assuming that my ALV grid display is like this and I want to download it as an excel file. Can the excel file have the same layout with the ALV grid?

Layout of customer 1.

Plant | Material | Description | Delivery Address | Delivery Date |

Layout of customer 2.

Material | Description | Plant | Delivery Address | Delivery Date | Postal Code | Quantity |

Can the columns in the excel file be dynamic as the columns in ALV grid?

6 REPLIES 6

jens_michaelsen
Participant
542

I tested with an ALV Grid and with a SALV - same result: The sequence of the columns in excel file was the same like in the ALV grid.

Sandra_Rossi
Active Contributor
0 Kudos
542

It's not clear whether you want to code your own download or use the standard download. If you do some custom code, you can do whatever you want. If you use standard download, see Jens comment.

walkerist
Participant
0 Kudos
542

I want custom download. I want that when my program is executed, aside from it generates an ALV grid, it automatically sends the ALV grid in excel file to an email.

Sandra_Rossi
Active Contributor
542

As I said, you can do whatever you want in a custom program. Now

So yes it is possible.

Although people have already asked such questions, which were answered in the forum, if you need a detailed answer, please explain what ALV technology you use, how the "customer" decides of the layout and the moment the export is triggered (because if it happens as you explain, I think that you don't need to do a custom export because standard export to Excel xstring exists).

jens_michaelsen
Participant
0 Kudos
542

For example, you can use the methods cl_salv_ex_util=>factory_result_data_table and cl_salv_bs_lex=>export_from_result_data_table to create an Excel xstring. The first method gets an ALV-fieldcatalog. If you have the list of fields of given layout, you can describe the sequence of columns for Excel in the ALV-fieldcatalog. SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S70 { color: #808080; }SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S70 { color: #808080; }

jens_michaelsen
Participant
0 Kudos
542
START-OF-SELECTION.
* Get data
  SELECT * FROM spfli INTO TABLE @gt_spfli.
* Get list of columns for layout
  SELECT SINGLE * FROM ltdx INTO gs_ltdx
    WHERE relid = 'LT'
    AND report = sy-repid
    AND variant = p_vari.  "Name of layout
  MOVE-CORRESPONDING gs_ltdx TO gs_varkey.
  CALL FUNCTION 'LT_DBDATA_READ_FROM_LTDX'
    EXPORTING
*     I_TOOL       = 'LT'
      is_varkey    = gs_varkey
    TABLES
      t_dbfieldcat = gt_dbfcat
*     T_DBSORTINFO =
*     T_DBFILTER   =
*     T_DBLAYOUT   =
    EXCEPTIONS
      not_found    = 1
      wrong_relid  = 2
      OTHERS       = 3.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
  DELETE gt_dbfcat WHERE param NE 'COL_POS' AND param NE 'NO_OUT'.
  LOOP AT gt_dbfcat ASSIGNING FIELD-SYMBOL(<dbfcat>).
    READ TABLE gt_fieldcat ASSIGNING FIELD-SYMBOL(<fcat>)  "<-- LVC-Fieldcatalog for ALV-Grid
    WITH KEY fieldname = <dbfcat>-key1.
    IF sy-subrc EQ 0.
      ASSIGN COMPONENT <dbfcat>-param OF STRUCTURE <fcat> TO <target>.
      <target> = <dbfcat>-value.
    ENDIF.
  ENDLOOP.
  TRY.
* Create model
      DATA(o_salv_ex_res) = cl_salv_ex_util=>factory_result_data_table( r_data          = REF #( gt_spfli )
                                                                        t_fieldcatalog  = gt_fieldcat ).
      DATA: lv_xls TYPE xstring.
* Create xls from model
      cl_salv_bs_lex=>export_from_result_data_table( EXPORTING
                                                       is_format            = if_salv_bs_lex_format=>mc_format_xlsx
                                                       ir_result_data_table = o_salv_ex_res
                                                     IMPORTING
                                                       er_result_file       = lv_xls ).
      CASE 'X'.
        WHEN p_down.
          PERFORM download_xls USING lv_xls.
        WHEN p_send.
          PERFORM send_mail USING p_email 'Hello world' lv_xls .
      ENDCASE.
    CATCH cx_root INTO DATA(e_txt).
      MESSAGE e_txt->get_text( ) TYPE 'S'.
  ENDTRY.