一个上传EXCEL导入示例

REPORT  ZTEST_UPEXCEL.

data: gt_table type TABLE OF SFLIGHT,
      gs_table like line of gt_table.
DATA:lt_excel TYPE TABLE OF ZSTABLINE.
DATA ls_excel LIKE LINE OF lt_excel.
FIELD-SYMBOLS:<fs>.
CONSTANTS: gc_begin_row TYPE i VALUE 1,
"Beginning row of excel file
           gc_begin_col TYPE i VALUE 1,
           "Beginning column of excel file
           gc_end_row   TYPE i VALUE 999,
           "Ending row of excel file
           gc_end_col   TYPE i VALUE 999.
"Ending column of excel file

PARAMETERS: p_file  LIKE rlgrap-filename.  "定义存放上传文件路径名的变量

initialization.

at selection-screen on  VALUE-REQUEST FOR p_file.   "这个只用在批处理这边
  PERFORM sub_find USING p_file.




start-of-selection.

  CALL FUNCTION ZALSM_EXCEL_TO_INTERNAL_TABLE
    EXPORTING
      filename                      = p_file
      i_begin_col                   = 1"gc_begin_row‘
      i_begin_row                   = 1"gc_begin_col
      i_end_col                     = 999"gc_end_row
      i_end_row                     = 999"gc_end_col
    TABLES
      intern                        = lt_excel
* EXCEPTIONS
*   INCONSISTENT_PARAMETERS       = 1
*   UPLOAD_OLE                    = 2
*   OTHERS                        = 3
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
  LOOP AT lt_excel INTO ls_excel.

    ASSIGN COMPONENT ls_excel-col OF STRUCTURE gs_table to <fs>.   "把工作区gs_tab中的字段赋给字段符号,而工作区gs_tab中的字段取决于内表中的列ls_excel-col
    CONDENSE ls_excel-value.                  " 去掉空格
    <fs> = ls_excel-value.
    at end of row.                                    "如果是最后一行就把工作区gs_tab存到内表gt_tab中
      "gs_tab-row = ls_excel-row.
      APPEND gs_table TO gt_table.
      CLEAR: gs_table.

    ENDAT.

  ENDLOOP.

  INSERT SFLIGHT FROM TABLE GT_TABLE ACCEPTING DUPLICATE KEYS.
  IF SY-SUBRC = 0.
    COMMIT WORK.
    MESSAGE 维护成功 type S.

  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  SUB_FIND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE  text
*----------------------------------------------------------------------*
FORM SUB_FIND  USING    P_P_FILE.



*  DATA: LD_DEFAULT_EXTENSION TYPE STRING VALUE ‘CSV‘.
  DATA: LT_FILE_TAB          TYPE FILETABLE.
  DATA: LS_FILE_LINE         TYPE FILE_TABLE.
  DATA: LD_RC                TYPE I,
        LW_TITLE  TYPE STRING,
        LW_FILTER TYPE STRING.

*   变量赋值
  LW_TITLE  = 选择文件.
  LW_FILTER = Excel文件|*.xl;*.xls;*.xlsx|.


  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = LW_TITLE
      FILE_FILTER             = LW_FILTER
      INITIAL_DIRECTORY       = C:\‘
    CHANGING
      FILE_TABLE              = LT_FILE_TAB
      RC                      = LD_RC
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      NOT_SUPPORTED_BY_GUI    = 4
      OTHERS                  = 5.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ELSE.
    CHECK LD_RC = 1.
    READ TABLE LT_FILE_TAB INTO LS_FILE_LINE INDEX 1.
    CHECK SY-SUBRC = 0.
    MOVE LS_FILE_LINE-FILENAME TO p_file.
  ENDIF.
  " F4_FOR_FILES


ENDFORM.                    " SUB_FIND

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。