1. 程式人生 > 其它 >記錄ABAP開發的日常——讀取EXCEL檔案sheet頁籤的資料

記錄ABAP開發的日常——讀取EXCEL檔案sheet頁籤的資料

前言:在專案中碰到過這樣的需求,使用者的EXCEL檔案模板裡面有多個SHEET頁,每個SHEET頁的資料是不一樣的,每一份資料對應單獨的需求,這樣我們就要根據具體的業務需求讀取相應SHEET頁的資料,在我的上一篇隨筆擴充套件EXCEL上載函式中提到過如何自定義EXCEL上載函式針對指定SHEET頁,這裡就演示下怎麼用該函式讀取SHEET頁中的資料。

1.建立一份EXCEL檔案(包含資料)

示例如下: SHEET1:ABC 和SHEET2:DEF

2.建立示例程式

*&---------------------------------------------------------------------*
*& Report ZDEMO_EXCEL_UPLOAD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_excel_upload.

**********************************************************************
* PROGRAM DETAIL                                                     *
**********************************************************************
* --T-CODE:ZDEMO_EXCEL_UPLOAD
* --AUTHOR:秋の夕
* --CREATE DATE:17.12.2021 15:28:56
* --DESCRIBLE:EXCEL 檔案上傳sheet頁測試
**********************************************************************
* EDIT LOG                                                           *
**********************************************************************
* VERSION          DATE              AUTHOR           REMARK
* <DEVK90XXX>     <YYYY/MM/DD>       <...>            <REASON>

**********************************************************************
* DECLARATIONS                                                       *
**********************************************************************
*TABLES: .

*CONSTANTS: .

TYPES:BEGIN OF ty_data,
        id       TYPE i,
        name(10) TYPE c,
      END OF  ty_data.

DATA: gt_data  TYPE TABLE OF ty_data WITH HEADER LINE.  "SHEET1:ABC"
DATA: gt_data2 TYPE TABLE OF ty_data WITH HEADER LINE.  "SHEET1:DEF"

**********************************************************************
* SELECTION SCREEN                                                   *
**********************************************************************
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY.

*  SELECT-OPTIONS: .

SELECTION-SCREEN: END OF BLOCK b1.


**********************************************************************
* INITIALIZATION                                                     *
**********************************************************************
INITIALIZATION.


**********************************************************************
* AT SELECTION SCREEN                                                *
**********************************************************************
AT SELECTION-SCREEN.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM frm_get_filename USING p_file.

**********************************************************************
* TOP OF PAGE                                                        *
**********************************************************************
TOP-OF-PAGE.


**********************************************************************
* END OF PAGE                                                        *
**********************************************************************
END-OF-PAGE.


**********************************************************************
* AT USER COMMAND                                                    *
**********************************************************************
AT USER-COMMAND.


**********************************************************************
* START OF SELECTION                                                 *
**********************************************************************
START-OF-SELECTION.
  PERFORM frm_upoload_file TABLES gt_data USING 'ABC'.
  PERFORM frm_upoload_file TABLES gt_data2 USING 'DEF'.

**********************************************************************
* END OF SELECTION                                                   *
**********************************************************************
END-OF-selection.
  IF gt_data[] IS NOT INITIAL.
    WRITE:/ |SHEET1:ABC資料|.
    LOOP AT gt_data.
      WRITE: / |{ gt_data-id } { gt_data-name }|.
    ENDLOOP.
  ENDIF.

  IF gt_data2[] IS NOT INITIAL.
    WRITE:/ |SHEET2:DEF資料|.
    LOOP AT gt_data2.
      WRITE: / |{ gt_data2-id } { gt_data2-name }|.
    ENDLOOP.
  ENDIF.


**********************************************************************
* FORMS DECLARATIONS                                                 *
**********************************************************************
FORM frm_get_filename  USING  uv_file.

  "出現開啟檔案的對話方塊
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',*.*,*.*.'
      mode             = 'O' "S為儲存,O為開啟
    IMPORTING
      filename         = uv_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM.                    "GET_FILENAME


FORM frm_upoload_file TABLES lt_input TYPE STANDARD TABLE USING sheet_name.

  DATA lt_upload  LIKE TABLE OF zalsmex_tabline WITH HEADER LINE.
  DATA: ls_i TYPE i.
  FIELD-SYMBOLS <f>.

  CALL FUNCTION 'ZFM_EXCEL_TO_INTERNAL_TABLE'       "這裡的EXCEL上傳函式是已經按照上一篇隨筆做了SHEET處理的"
    EXPORTING
      filename                = p_file
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = 6
      i_end_row               = 50000
      i_sheet_name            = sheet_name
    TABLES
      intern                  = lt_upload
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.


  IF sy-subrc = 0.
*--->處理讀取出的資料
    SORT lt_upload BY row col.
    LOOP AT lt_upload.
      ls_i = lt_upload-col.
      ASSIGN COMPONENT ls_i OF STRUCTURE lt_input TO <f>.
      <f> = lt_upload-value.
      AT END OF row.
        APPEND lt_input TO lt_input.
        CLEAR:lt_input.
      ENDAT.
    ENDLOOP.
  ELSE.
    WRITE: / 'EXCEL UPLOAD FAILED ', p_file, sy-subrc.
  ENDIF.

ENDFORM.                    " UPOLOAD_FILE

3.效果展示

這裡面要注意的事項:如果不指定I_SHEET_NAME引數,預設讀取第一個SHEET頁;如果指定I_SHEET_NAME引數,引數值一定要跟EXCEL檔案裡的SHEET名對上,不然讀取不到資料。

覺得博主的文章對你有幫助的,動動滑鼠一鍵三連,激勵博主寫出更多精彩文章!