ABAP中Excel本地导入功能

一、效果

二、代码读取Excel数据

*定义文本标签
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text01.
PARAMETERS: p_file TYPE rlgrap-filename .
SELECTION-SCREEN END OF BLOCK blk1.

*实现打开文件夹,加载Excel
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',All Files,*.*,*.xls,*.xlsx. '
      mode             = 'O'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
*读取Excel数据
START-OF-SELECTION.
  PERFORM frm_get_data.
END-OF-SELECTION.

FORM FRM_GET_DATA.
  DATA: it_excel TYPE STANDARD TABLE OF alsmex_tabline WITH HEADER LINE.
  DATA l_index LIKE sy-tabix.
  FIELD-SYMBOLS: <fs>.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = 3
      i_end_row               = 50000
    TABLES
      intern                  = it_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  IF it_excel[] IS INITIAL.
    MESSAGE '文件中没有数据' TYPE 'E'.
  ENDIF.
    SORT it_excel BY row.
  LOOP AT it_excel.
    MOVE it_excel-col TO l_index.
    ASSIGN COMPONENT l_index OF STRUCTURE  gt_upload TO <fs>.
    MOVE it_excel-value TO <fs>.
    AT END OF row.
      APPEND gt_upload.
      CLEAR gt_upload.
      CLEAR it_excel.
    ENDAT.
  ENDLOOP.
ENDFORM .

 

posted @ 2020-06-29 23:02  cqyyck  阅读(477)  评论(0编辑  收藏  举报