首页 > 其他分享 >ABAP 通用自建表批到

ABAP 通用自建表批到

时间:2023-05-29 10:55:24浏览次数:44  
标签:sy 通用 自建表批 ABAP iexcel ls alv table TYPE

*&---------------------------------------------------------------------*
*& Report ZUPLOAD_TABLE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zupload_table.

*TYPE-POOLS:slis,abap.

DATA: BEGIN OF iexcel OCCURS 0.
        INCLUDE STRUCTURE alsmex_tabline.
DATA: END OF iexcel.

DATA: i_index TYPE i.
FIELD-SYMBOLS: <fs>.



*-----------
DATA: d_ref      TYPE REF TO data,

      lt_alv_cat TYPE TABLE OF lvc_s_fcat,

      ls_alv_cat LIKE LINE OF lt_alv_cat.


DATA: lt_table LIKE TABLE OF dntab.

DATA: ls_table TYPE dntab.


DATA: dyn_table TYPE REF TO data.

DATA: dyn_wa TYPE REF TO data.


FIELD-SYMBOLS :

  <dyn_table> TYPE table,

  <dyn_wa>    TYPE any,

  <dyn_field> TYPE any,

  <fs_str>    TYPE any.


*-------------------------------------------
PARAMETERS: p_fname LIKE rlgrap-filename MEMORY ID m01. " 获取文件路径
PARAMETERS: p_table  TYPE tabname,
            p_begrow TYPE i DEFAULT 2. "开始行

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
  PERFORM get_field.


START-OF-SELECTION.
  PERFORM get_table.
  PERFORM upload.
  PERFORM alv_show.

*&---------------------------------------------------------------------*
*&      Form  get_field
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_field.


  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
*     DEF_FILENAME     = ' '
*     DEF_PATH         = ' '
      mask             = 'Excel Files,*.xls,All Files,*.csv,*.*.'
*     MODE             = ' '
      title            = '请选择要导入的格式'
    IMPORTING
      filename         = p_fname
*     RC               =
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
  IF sy-subrc <> 0.
    WRITE: '导入数据出错'.
  ENDIF.

ENDFORM.                    "get_field


*&---------------------------------------------------------------------*
*&      Form  upLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM upload.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_fname
      i_begin_col             = 1
      i_begin_row             = p_begrow
      i_end_col               = 100
      i_end_row               = 10000
    TABLES
      intern                  = iexcel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    WRITE:sy-subrc.
  ELSE.
    SELECT SINGLE dcpfm INTO @DATA(l_dcpfm)
      FROM usr01
      WHERE bname = @sy-uname.

    LOOP AT iexcel.

      " WRITE:IEXCEL-COL.
      MOVE iexcel-col TO i_index.
      i_index = i_index + 1.
      ASSIGN COMPONENT i_index OF STRUCTURE <dyn_wa> TO <fs> .
      IF <fs> IS ASSIGNED.
        MOVE iexcel-value TO <fs>.
        READ TABLE lt_table INTO ls_table INDEX i_index.
        IF sy-subrc = 0.
          CASE ls_table-inttype.
            WHEN 'D'.
              CALL FUNCTION 'ZCONVERT_DATE_TO_INPUT'
                EXPORTING
                  im_datext = iexcel-value
                IMPORTING
                  ex_datint = <fs>
*                 EX_DATFMUSED       =
*                 EV_MSGTY  =
*                 EV_MSGTXT =
                .

            WHEN 'T'.
              cl_abap_timefm=>conv_time_ext_to_int(
                EXPORTING
                  time_ext      = iexcel-value                " External Represenation of Time
                  is_24_allowed = abap_false       " Is 24:00 permitted?
                IMPORTING
                  time_int      = <fs>                " Internal Represenation of Time
              ).

            WHEN 'P'.
              CASE l_dcpfm.
                WHEN ''.
                  REPLACE ALL OCCURRENCES OF '.' IN iexcel-value WITH '' .
                  REPLACE ',' IN iexcel-value WITH '.' .
                  CONDENSE iexcel-value NO-GAPS.
                WHEN 'Y'."小数点是逗号
                  REPLACE ',' IN iexcel-value WITH '.' .
                  CONDENSE iexcel-value NO-GAPS.
                WHEN 'X'."小数点是逗号
                  REPLACE ALL OCCURRENCES OF ',' IN iexcel-value WITH '' .
                  CONDENSE iexcel-value NO-GAPS.
                WHEN OTHERS.
              ENDCASE.
              <fs> = iexcel-value.
            WHEN OTHERS.
          ENDCASE.
        ENDIF.
      ENDIF.
      AT END OF row.
        APPEND <dyn_wa> TO <dyn_table>.
        CLEAR: <dyn_wa> .
        " APPEND IM_TAB.  "在每一行结束时
        " CLEAR IM_TAB.
      ENDAT.
    ENDLOOP.

  ENDIF.

  CALL FUNCTION 'ZRFC_IN'
    TABLES
      t_data = <dyn_table>.

ENDFORM.                    "upLOAD



*&---------------------------------------------------------------------*
*&      Form  get_table
*&---------------------------------------------------------------------*
*       text  获取内表结构
*----------------------------------------------------------------------*
FORM get_table.
*取出表结构的字段目录
  CALL FUNCTION 'NAMETAB_GET'
    EXPORTING
      langu          = sy-langu
      tabname        = p_table
    TABLES
      nametab        = lt_table
    EXCEPTIONS
      no_texts_found = 1.


*根据取出的字段目录生成参考字段目录

  LOOP AT lt_table INTO ls_table.

    ls_alv_cat-fieldname = ls_table-fieldname.

    ls_alv_cat-ref_table = p_table.

    ls_alv_cat-ref_field = ls_table-fieldname.

    APPEND ls_alv_cat TO lt_alv_cat.

    CLEAR ls_alv_cat.

  ENDLOOP.

*    内表创建

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = lt_alv_cat
    IMPORTING
      ep_table        = d_ref.

*    指定生成的内表到字段符号
  ASSIGN d_ref->* TO <dyn_table>.
*    创建动态工作区结构
  CREATE DATA dyn_wa LIKE LINE OF <dyn_table>.
*    创建动态工作区
  ASSIGN dyn_wa->* TO <dyn_wa>.

ENDFORM.                    "get_table




*&---------------------------------------------------------------------*
*&      Form  ALV_SHOW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM alv_show.
*显示内表中的数据
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_structure_name         = p_table
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_callback_user_command  = 'USER_COMMAND'
    TABLES
      t_outtab                 = <dyn_table>
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.
ENDFORM.                    "ALV_SHOW
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD' EXCLUDING rt_extab.
ENDFORM.
FORM user_command  USING r_ucomm LIKE sy-ucomm
                                   rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN 'ZSAVE'.
      MODIFY (p_table) FROM TABLE <dyn_table>.
      IF sy-subrc EQ 0.
        COMMIT WORK AND WAIT.
        MESSAGE '保存成功' TYPE 'S'.
      ELSE.
        ROLLBACK WORK.
        MESSAGE '保存失败,请检查数据是否有误' TYPE 'S' DISPLAY LIKE 'E'.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.

 

标签:sy,通用,自建表批,ABAP,iexcel,ls,alv,table,TYPE
From: https://www.cnblogs.com/ZJY-1314/p/17439828.html

相关文章

  • DAO和增删改查通用方法--BasicDao
    1. 先分析一个问题  852apache-dbutils+ Druid简化了JDBC开发,但还有不足: 1. SQL 语句是固定,不能通过参数传入,通用性不好,需要进行改进,更方便执行增删改查2.对于select 操作,如果有返回值,返回类型不能固定,需要使用泛型3.将来的表很多, 业务需求复杂,不可能只靠一个Java类完成4......
  • 基于Expression Lambda表达式树的通用复杂动态查询构建器——《原型篇一》[已开源]
    续接上编,本篇来讲讲俄罗斯套娃的设计与实现。首先简单地完善一下前面提到的例子,代码如下:测试实体类//测试实体类publicclassTable{publicintA;publicintB;}独立条件类//独立条件:publicclassField{publicLogicalLogical{get;set;} ......
  • ABAP-屏幕设计-上门拜访动态切换
    *&---------------------------------------------------------------------**&ReportZHQ_01_04*&*&---------------------------------------------------------------------**&*&*&-------------------------------------------------......
  • Centos7配置普通用户不加sudo直接运行docker命令
    平时普通用户执行docker命令都要在docker命令前加上sudo就挺麻烦的,咱们把普通用户执行docker要加的sudo去掉.设置用户组sudogroupadddocker如果出现groupadd:cannotopen/etc/group,则使用以下两行命令解锁,如果没有则不需要运行,直接跳过即可sudochattr-i/etc/shad......
  • ⭐业务分析通用思路和流程
    0、接手业务时看懂数据库中的数据表 1、分析业务思路的2个方向?①以终为始看页面原型效果②看 SQL语句 2、编写代码看哪4样内容?接口文档请求方式提交路径请求参数响应结果......
  • ABAP:CO11N,CO13保存前增强
    SE18:WORKORDER_CONFIRMMETHODif_ex_workorder_confirm~at_save.DATA:s_vornrTYPERANGEOFresb-vornr.DATA:lv_vornrLIKELINEOFs_vornr.DATA:lv_gmngaTYPEafru-gmnga,lv_mengeTYPEmseg-menge,lv_menge_s......
  • Dapper-OracleSQLHelper 的通用封装
    ///<summary>///OracleHelp封装使用///</summary>///<typeparamname="T"></typeparam>publicclassOracleSQLDapperHelper<T>whereT:class{///<summary>///数据库连接字符串......
  • JS 树形数据 Tree的通用方法
    点击查看代码/***@description查找包含自身节点的父代节点*@paramlist列表数据*@paramid节点id*@parampid节点的父id*/exportfunctionlistToTree(list,id,pid){list.forEach((node)=>{constpNdoe=list.find((row)=>row[id]===nod......
  • 基于Expression Lambda表达式树的通用复杂动态查询构建器——《构思篇一》
    在上一篇中构思了把查询子句描述出来的数据结构,那么能否用代码将其表达出来,如何表达呢?再次回顾考察,看下面的查询子句:Id>1andId<10如上所示,有两个独立的条件分别为Id>1和Id<10,用一个逻辑操作符and连接起来。再看下面这条,后面也是两个独立条件通过操作符or连接,并包在括号......
  • JPA通用策略生成器(@GeneratedValue 四种标准用法为TABLE, SEQUENCE, IDENTITY, AUTO)
    JPA通用策略生成器查看JPA的源码可知:packagejavax.persistence;/***Definesthetypesofprimarykeygenerationstrategies.**@seeGeneratedValue**@sinceJavaPersistence1.0*/publicenumGenerationType{/***Indicatesthatthepers......