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: 

Upload Excel File data to ALV GRID Control

Former Member
0 Kudos
2,169

Hello,

I'm a novice in ABAP.

Could someone sample me how to upload data retrieved from an Excel file to an ALV GRID control.

Thanks

Regards

Harry

8 REPLIES 8

timo_wendt
Explorer
0 Kudos
389

try this link:

former_member188685
Active Contributor
0 Kudos
389

Hi,

Upload the Excel file to internal table, and then build the field catalog, and pass the fieldcatalog and internal table to method SET_TABLE_FOR_FIRST_DISPLAY of cl_gui_alv_grid.

regards

vijay

Former Member
0 Kudos
389

Hi Guys,

First of all thank you for your prompt answers.

I get quite lost.

Could you please review my code below and tell me what is wrong.

I succeed uploading the file but I got trouble when trying to display it s content in ALV GRID.

Could you Please take a look at my code and tell me what is wrong

&----


*& Report Z_UPLOAD_FROM_EXCEL_HARRY *

*& *

&----


*& *

*& *

&----


REPORT Z_UPLOAD_FROM_EXCEL_HARRY .

DAta: filename(128),filetype(3),filesize type i,

container type ref to cl_gui_custom_container,

alv type ref to cl_gui_alv_grid.

*Define a new type 'team_member' with 3 components

types: begin of team_member,

FirstN(10) type c,

LastN(10) type c,

Ext(10) type c,

end of team_member.

*Define an internal table type

types: itab type team_member occurs 10.

DATA: team_rec type team_member,

tab type itab.

call function 'UPLOAD'

exporting

CODEPAGE = 'IBM'

FILENAME = 'D:\ABAP\BIDITeam.txt'

FILETYPE = 'DAT'

ITEM = 'Read test from Excel file BIDITeam.txt'

importing

filesize = filesize

act_filename = filename

act_filetype = filetype

tables

data_tab = tab

exceptions

conversion_error = 1

invalid_table_width = 2

invalid_type = 3.

write: 'sy-subrc:' , sy-subrc,

/ 'Name :',(60) filename,

/ 'Type :', filetype,

/ 'Size :', filesize.

skip.

loop at tab into team_rec.

write: / team_rec-FirstN,team_rec-LastN,team_rec-Ext.

endloop.

*Create container instance

create object container

exporting container_name = 'BIDI_TEAM'.

  • lifetime =

*Create alv grid instance

create object alv

exporting i_parent = container.

call method alv->set_table_for_first_display

exporting i_structure_name = 'itab'

changing it_outtab = tab.

call screen 100.

&----


*& Module STATUS_0100 OUTPUT

&----


  • text

----


MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'STATUS'.

SET TITLEBAR 'WELCOME'.

call method alv->refresh_table_display.

*split

ENDMODULE. " STATUS_0100 OUTPUT

*method fill_list.

*

*

*

*endmethod

&----


*& Module USER_COMMAND_0100 INPUT

&----


  • text

----


MODULE USER_COMMAND_0100 INPUT.

if sy-ucomm = 'BACK' or

sy-ucomm = 'EXIT' or

sy-ucomm = 'CANCEL'.

leave program.

endif.

ENDMODULE. " USER_COMMAND_0100 INPUT

Many thanks

Harry

0 Kudos
389

Hi just check this Code...

REPORT  ZTEST1234    message-id zz                           .
DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA: L_VALID TYPE C,
      V_FLAG,
      V_DATA_CHANGE,
      V_ROW TYPE LVC_S_ROW,
      V_COLUMN TYPE LVC_S_COL,
      V_ROW_NUM TYPE LVC_S_ROID.

*---------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION .
  PUBLIC SECTION .
    METHODS:
**Hot spot Handler
    HANDLE_HOTSPOT_CLICK FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
                      IMPORTING E_ROW_ID E_COLUMN_ID ES_ROW_NO,
**Double Click Handler
    HANDLE_DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
                                     IMPORTING E_ROW E_COLUMN ES_ROW_NO.


ENDCLASS.                    "lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
*Handle Hotspot Click
  METHOD HANDLE_HOTSPOT_CLICK .
    CLEAR: V_ROW,V_COLUMN,V_ROW_NUM.
    V_ROW  = E_ROW_ID.
    V_COLUMN = E_COLUMN_ID.
    V_ROW_NUM = ES_ROW_NO.
    message i000 with v_row 'clicked'.
  ENDMETHOD.                    "lcl_event_handler

*Handle Double Click
  METHOD  HANDLE_DOUBLE_CLICK.

  ENDMETHOD.                    "handle_double_click
endclass.                    "LCL_EVENT_HANDLER IMPLEMENTATION

*&---------------------------------------------------------------------*
*&             Global Definitions
*&---------------------------------------------------------------------*
DATA:      G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,"Container1
            G_HANDLER TYPE REF TO LCL_EVENT_HANDLER. "handler



DATA: OK_CODE LIKE SY-UCOMM,
      SAVE_OK LIKE SY-UCOMM,
      G_CONTAINER1 TYPE SCRFNAME VALUE 'TEST',
      GS_LAYOUT TYPE LVC_S_LAYO.


*- Fieldcatalog for First and second Report
DATA: IT_FIELDCAT  TYPE  LVC_T_FCAT,
      X_FIELDCAT TYPE LVC_S_FCAT,
      LS_VARI  TYPE DISVARIANT.

*---------------------------------------------------------------------
*                START-OF_SELECTION
*---------------------------------------------------------------------
START-OF-SELECTION.
  data:begin of  itab occurs 0,
       vbeln like likp-vbeln,
       posnr like lips-posnr,
       end of itab.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    FILENAME                      = 'C:text.txt'

  TABLES
    DATA_TAB                      = itab
 EXCEPTIONS
   FILE_OPEN_ERROR               = 1
   FILE_READ_ERROR               = 2
   NO_BATCH                      = 3
   GUI_REFUSE_FILETRANSFER       = 4
   INVALID_TYPE                  = 5
   NO_AUTHORITY                  = 6
   UNKNOWN_ERROR                 = 7
   BAD_DATA_FORMAT               = 8
   HEADER_NOT_ALLOWED            = 9
   SEPARATOR_NOT_ALLOWED         = 10
   HEADER_TOO_LONG               = 11
   UNKNOWN_DP_ERROR              = 12
   ACCESS_DENIED                 = 13
   DP_OUT_OF_MEMORY              = 14
   DISK_FULL                     = 15
   DP_TIMEOUT                    = 16
   OTHERS                        = 17
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.



END-OF-SELECTION.
  IF NOT ITAB[] IS INITIAL.
    CALL SCREEN 100.
  ELSE.
    MESSAGE I002 WITH 'NO DATA FOR THE SELECTION'(004).
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM CREATE_AND_INIT_ALV .
  DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.


  CREATE OBJECT G_CUSTOM_CONTAINER
         EXPORTING CONTAINER_NAME = G_CONTAINER1.
  CREATE OBJECT G_GRID
         EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
* Set a titlebar for the grid control
  CLEAR GS_LAYOUT.
  GS_LAYOUT-GRID_TITLE = TEXT-003.
  GS_LAYOUT-ZEBRA = SPACE.
  GS_LAYOUT-CWIDTH_OPT = 'X'.
  GS_LAYOUT-NO_ROWMARK = 'X'.

  CALL METHOD G_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER.

  CREATE OBJECT G_HANDLER.
  SET HANDLER G_HANDLER->HANDLE_DOUBLE_CLICK FOR G_GRID.
  SET HANDLER G_HANDLER->HANDLE_HOTSPOT_CLICK FOR G_GRID.


* setting focus for created grid control
  CALL METHOD CL_GUI_CONTROL=>SET_FOCUS
    EXPORTING
      CONTROL = G_GRID.
* Build fieldcat and set editable for date and reason code
* edit enabled. Assign a handle for the dropdown listbox.
  PERFORM BUILD_FIELDCAT.

* Optionally restrict generic functions to 'change only'.
*   (The user shall not be able to add new lines).
  PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.
**Vaiant to save the layout
  LS_VARI-REPORT      = SY-REPID.
  LS_VARI-HANDLE      = SPACE.
  LS_VARI-LOG_GROUP   = SPACE.
  LS_VARI-USERNAME    = SPACE.
  LS_VARI-VARIANT     = SPACE.
  LS_VARI-TEXT        = SPACE.
  LS_VARI-DEPENDVARS  = SPACE.

**Calling the Method for ALV output
  CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY
    EXPORTING
      IT_TOOLBAR_EXCLUDING = LT_EXCLUDE
      IS_VARIANT           = LS_VARI
      IS_LAYOUT            = GS_LAYOUT
      I_SAVE               = 'A'
    CHANGING
      IT_FIELDCATALOG      = IT_FIELDCAT
      IT_OUTTAB            = ITAB[].



* Set editable cells to ready for input initially
  CALL METHOD G_GRID->SET_READY_FOR_INPUT
    EXPORTING
      I_READY_FOR_INPUT = 1.
*  CALL METHOD G_GRID->SET_TOOLBAR_INTERACTIVE.

ENDFORM.                               "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PT_EXCLUDE text
*----------------------------------------------------------------------*
FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.
* Only allow to change data not to create new entries (exclude
* generic functions).
  DATA LS_EXCLUDE TYPE UI_FUNC.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_CUT.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE_NEW_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_UNDO.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
ENDFORM.                               " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*&      Form  build_fieldcat
*&---------------------------------------------------------------------*
*       Fieldcatalog
*----------------------------------------------------------------------*
FORM BUILD_FIELDCAT .
  data: l_pos type i.
  L_POS = L_POS + 1.


  X_FIELDCAT-SCRTEXT_M = 'Delivery'(024).
  X_FIELDCAT-FIELDNAME = 'VBELN'.
  X_FIELDCAT-TABNAME = 'IT_FINAL'.
  X_FIELDCAT-COL_POS    = L_POS.
  X_FIELDCAT-NO_ZERO    = 'X'.
  X_FIELDCAT-OUTPUTLEN = '10'.
  X_FIELDCAT-HOTSPOT = 'X'.

  APPEND X_FIELDCAT TO IT_FIELDCAT.
  CLEAR X_FIELDCAT.
  L_POS = L_POS + 1.

  X_FIELDCAT-SCRTEXT_M = 'Item'(025).
  X_FIELDCAT-FIELDNAME = 'POSNR'.
  X_FIELDCAT-TABNAME = 'IT_FINAL'.
  X_FIELDCAT-COL_POS    = L_POS.
  X_FIELDCAT-OUTPUTLEN = '5'.
  APPEND X_FIELDCAT TO IT_FIELDCAT.
  CLEAR X_FIELDCAT.
  L_POS = L_POS + 1.

ENDFORM.                    " build_fieldcat
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'MAIN100'.
  SET TITLEBAR 'MAIN100'.
  IF G_CUSTOM_CONTAINER IS INITIAL.
**Initializing the grid and calling the fm to Display the O/P
    PERFORM CREATE_AND_INIT_ALV.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  case sy-ucomm.

    when 'BACK'.
      leave to screen 0.

  endcase.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

Best Regards

vijay

0 Kudos
389

Hi

Hello I have solved the problem.

My mistake was that I did not map my internal table to Field catalog.

Thanks all for your kind help.

Best regards

Hatty

hymavathi_oruganti
Active Contributor
0 Kudos
389

call fn module ALSM_EXCEL_TO_INTERNAL_TABLE to upload data from excel to internal table and then build fieldcatalog

and display in alv grid

Former Member
0 Kudos
389

Hi Harry,

try to put this code in the PBO of screen 100..I hope you have declared the Custom Container 'BIDI_TEAM' in the same screen.

Create container instance

create object container

exporting container_name = 'BIDI_TEAM'.

  • lifetime =

*Create alv grid instance

create object alv

exporting i_parent = container.

call method alv->set_table_for_first_display

exporting i_structure_name = 'itab'

changing it_outtab = tab.

Regards,

Srivathsan

Former Member
0 Kudos
389

hi Harry,

Check this link to get the code for uploading..

http://www.sap-img.com/abap/upload-direct-excel.htm

Also check out these threads for more details.

Regards

Vinoth