Wednesday, October 18, 2017

Upload Excel to SAP with 'ALSM_EXCEL_TO_INTERNAL_TABLE'

Description:
This code gives the brief idea of how to use  Function module 'ALSM_EXCEL_TO_INTERNAL_TABLE' for uploading the Excel file into your Internal table with the required Format .Hope this will help you .
/* your snippet */
The Function module 'ALSM_EXCEL_TO_INTERNAL_TABLE' returns us a Table having the format :

File Data
First Name Middle Name Last Name
Prem Raj Kaushik
Naresh ... ...

Output Table
Row 
Column 
Value 
0001 
Prem (First Name ) 
0002 
Raj (Middle name ) 
0003 
Kaushik (Last name ) 
0001 
Naresh (First Name)

Code Sample :

DATA : IT_FILE  LIKE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

DATA FILE_PATH LIKE  RLGRAP-FILENAME.

TYPES : BEGIN OF  SBU_UPLOAD ,
  BU TYPE c,
  BU_DESC TYPE c,
  DELETE TYPE C,
END OF SBU_UPLOAD .
DATA TBU_UPLOAD  TYPE STANDARD TABLE OF SBU_UPLOAD  WITH HEADER LINE.

CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'FILE_LOC' "Here file_LOC is the text Element name on Screen.
    IMPORTING
      file_name  = FILE_PATH. 
"In FILE_PATH you get the excel file path , ehich you want to upload in Internal table ex: E:\Prem\Premral_details.xls' and we pass this file name to the following function Module which Return back the Internal table of its own Format ."
 CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                   = FILE_PATH
      I_BEGIN_COL             = '1'
      I_BEGIN_ROW            = '2'
      I_END_COL                 = '3'                //The column is read from #1 to #3
      I_END_ROW               = '256'            //The row is read from #2 to #256
    TABLES
      INTERN                  = IT_FILE
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 2
      OTHERS                  = 3.
  IF SY-SUBRC NE SPACE.
     *leave PROGRAM.
  ENDIF.
" Now I use the following Logic to convert that IT_FILE table  into our own table format."
  LOOP AT IT_FILE.
    CASE IT_FILE-COL.
      WHEN '0001'.
        MOVE IT_FILE-VALUE TO TBU_UPLOAD-BU.
      WHEN '0002'.
        MOVE IT_FILE-VALUE TO TBU_UPLOAD-BU_DESC.
      WHEN '0003'.
        MOVE IT_FILE-VALUE TO TBU_UPLOAD-DELETE.
    ENDCASE.
    AT END OF ROW.
 "At end of row TBU_UPLOAD is updated with the desired value i.e
When Row Column in table one converted  from 1 to 2 , the first column in Table 2 is inserted. Which is the required format of us . "
      APPEND TBU_UPLOAD.
      CLEAR TBU_UPLOAD.
    ENDAT.
*       APPEND TBU_UPLOAD.
  ENDLOOP

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...