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: 

Two different conditions for ALV and Excel output

0 Kudos

Hello.

I have a job to make Selection screen with two radio buttons for ALV and Excel output and there are two different conditions for every output with different columns. How to make two different outputs? Now it`s making all of two outputs in one viev.

Here`s my code:



*&---------------------------------------------------------------------*

*& Report  ZTASK2_REPORT

*&---------------------------------------------------------------------*



REPORT ztask2_report.



TABLES: aufm, caufv.



TYPES: BEGIN OF my_tab,

  mblnr TYPE aufm-mblnr,

  matnr TYPE aufm-matnr,

  shkzg TYPE aufm-shkzg,

  menge TYPE aufm-menge,

  dmbtr TYPE aufm-dmbtr,

  ktext TYPE caufv-ktext,

  erdat TYPE caufv-erdat,

  auart TYPE caufv-auart,

END OF my_tab.



DATA: gt_aufmcaufv TYPE TABLE OF my_tab.



***Excel

DATA:

  lo_table TYPE REF TO cl_salv_table,

  lx_xml   TYPE xstring.

***



SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-001.

SELECT-OPTIONS: a_bldat FOR aufm-bldat.

PARAMETERS: rg_op1 RADIOBUTTON GROUP opx DEFAULT 'X'. "ALV

PARAMETERS: rg_op2 RADIOBUTTON GROUP opx.             "Excel

SELECTION-SCREEN END OF BLOCK bl1.



START-OF-SELECTION.



***

  IF rg_op1 EQ 'X'. "ALV



    SELECT

      au~mblnr

      au~matnr

      au~shkzg

      au~menge

      au~dmbtr

      au~bldat

      au~aufnr

      ca~ktext

      ca~auart

      ca~erdat

      ca~aufnr

    INTO CORRESPONDING FIELDS OF TABLE gt_aufmcaufv

    FROM

          aufm AS au

    INNER JOIN

          caufv AS ca

    ON

      au~bldat = ca~erdat

    WHERE

      au~aufnr = ca~aufnr

      AND

      ca~auart = 'PI01'.



  ELSEIF rg_op2 EQ 'X'. "Excel



    SELECT

      au~matnr

      au~menge

      au~dmbtr

    INTO CORRESPONDING FIELDS OF TABLE gt_aufmcaufv

    FROM

          aufm AS au

    INNER JOIN

          caufv AS ca

    ON

      au~bldat = ca~erdat

      WHERE

        au~shkzg = 'S'.



  ENDIF.



***



  IF rg_op1 EQ 'X'. "ALV



    PERFORM display_alv.



  ELSEIF rg_op2 EQ 'X'. "Excel



    TRY.

        cl_salv_table=>factory(

        IMPORTING

          r_salv_table = lo_table

        CHANGING

          t_table      = gt_aufmcaufv ).   "my table

      CATCH cx_salv_msg.

    ENDTRY.



    lx_xml = lo_table->to_xml( xml_type = '10' ). "XLSX



    CALL FUNCTION 'XML_EXPORT_DIALOG'

      EXPORTING

        i_xml                      = lx_xml

        i_default_extension        = 'XLSX'

        i_initial_directory        = ''

        i_default_file_name        = 'export.XLSX'

        i_mask                     = 'Excel (*.XLSX)|*.XLSX'

      EXCEPTIONS

        application_not_executable = 1

        OTHERS                     = 2.



  ENDIF.



END-OF-SELECTION.



*** Form display_alv



FORM display_alv.



  DATA:

       gc_alv_table TYPE REF TO cl_salv_table,

       gc_functions TYPE REF TO cl_salv_functions,

       gc_columns TYPE REF TO cl_salv_columns_table.



  cl_salv_table=>factory(

      IMPORTING r_salv_table = gc_alv_table

      CHANGING t_table = gt_aufmcaufv[]

  ).



  gc_alv_table->display( ).



ENDFORM.                    "display_alv
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}.L0S31 {
font-style: italic;
color: #808080;
}.L0S32 {
color: #3399FF;
}.L0S33 {
color: #4DA619;
}.L0S52 {
color: #0000FF;
}.L0S55 {
color: #800080;
}.L0S70 {
color: #808080;
}
1 ACCEPTED SOLUTION

VXLozano
Active Contributor

Your code is a mess.

First of all, try to modularize (if you want not to go full OOP, at least keep the code as clean as possible).
Something like

start-of-selection.
  case 'X'.
    when opt_alv.
      perform alv.
    when opt_xls.
      perform xls.
  endcase.

This will help you (and everyone else) to understand your code. If you refactor your program this way, you will:

  1. be able to buy yourself a shirt with the script "I'm cool, I do refactor"
  2. detect some pieces of your code that are in the wrong place
9 REPLIES 9

sveabecker
Community Advocate
Community Advocate
0 Kudos

Welcome and Thank you for visiting SAP Community to get answers to your questions. Please add more details to your question, e.g. you can also add a screenshot. With that, you can reach a broader range of experts to get your question answered. I also recommend to do this tutorial https://developers.sap.com/tutorials/community-qa.html

The more details you provide, the more likely it is that members will be able to assist you.

Finally, if you're hoping to connect with readers, please consider adding a picture to your profile. Here's how you do it: https://www.youtube.com/watch?v=F5JdUbyjfMA&list=PLpQebylHrdh5s3gwy-h6RtymfDpoz3vDS. By personalizing your profile with a photo of you, you encourage readers to respond.

Regards, Svea

SAP Community Global Moderator


----- Stay up-to-date with SAP Community and subscribe to What's New today! -----

VXLozano
Active Contributor

Your code is a mess.

First of all, try to modularize (if you want not to go full OOP, at least keep the code as clean as possible).
Something like

start-of-selection.
  case 'X'.
    when opt_alv.
      perform alv.
    when opt_xls.
      perform xls.
  endcase.

This will help you (and everyone else) to understand your code. If you refactor your program this way, you will:

  1. be able to buy yourself a shirt with the script "I'm cool, I do refactor"
  2. detect some pieces of your code that are in the wrong place

VXLozano
Active Contributor
0 Kudos

I deleted my previous answer because I thought it was not a true answer, but I'm not 100% sure, because I think with the CODE addition, the OP has removed the PERFORM alv thingie.

Lol, thank you. Ima new in abap and trying to do my best.

VXLozano
Active Contributor
0 Kudos

No problem. But remember, to be new in something is not an excuse to do it the wrong way. Try to read something about the benefits of OOP or, at least, modularization and those things that make your code clean.

VXLozano
Active Contributor
0 Kudos

Hiyas, Sandra... I'm not sure if the OP wants to hide columns (his post is as messy as his code). I understood the program does both outputs regardless the radiobutton selected.

Sandra_Rossi
Active Contributor
0 Kudos

Please clarify what result you expect for radio button 1 and for radio button 2.

raymond_giuseppi
Active Contributor

By default the program will of course not distinguish the two sets of fields / columns in your ALV as you didn't write anything for that.

You could (inclusive options)

  • Manage the columns in your code (hide the columns not loaded from the database (for that use GET_COLUMNS, LOOP and use SET_VISIBLE)
  • Manage different display variants (for this use GET_LAYOUT and then SET_KEY to provide different key id to display variants, e.g. use same report value but use a different handle value building the key value)
  • Rewrite your code
  • Etc.

0 Kudos

Thank you for help.