SAP 批量写入自建表数据 源码

使用的时候修改 对应的参考的表结构即可,针对不同的自建表以及excel数据,修改ALSM_EXCEL_TO_INTERNAL_TABLE的起始行列和结束行列

*&---------------------------------------------------------------------*
*& Report Y_PNJ_DEMO07
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT y_pnj_demo07.

"请勿删除

DATA: gt_table   TYPE TABLE OF alsmex_tabline,
      gs_table   TYPE alsmex_tabline,
      gt_ztwm007 TYPE STANDARD TABLE OF ztwm007,
      gs_ztwm007 TYPE ztwm007.

FIELD-SYMBOLS:<fs>.

SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME.
  PARAMETERS: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK blk2.

INITIALIZATION.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  " 本地文件搜索帮助
  PERFORM frm_file_open.   " 用于上传文件

AT SELECTION-SCREEN.
  PERFORM frm_getdata. " 获取上载的excel数据

START-OF-SELECTION.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*& Form frm_file_open 上传文件
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_file_open .

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',EXCEL FILE,*.XLS;*XLSX;'
      mode             = '0' "S是保存,0是打开
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_getdata
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_getdata .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename    = p_file
      i_begin_col = 1
      i_begin_row = 1
      i_end_col   = 11
      i_end_row   = 6
    TABLES
      intern      = gt_table.


  LOOP AT gt_table INTO gs_table.

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

  MODIFY ztwm007 FROM TABLE gt_ztwm007.

ENDFORM.
posted @ 2024-02-03 09:34  otowa  阅读(55)  评论(0编辑  收藏  举报