SAP自开发程序详解编程语言

1.显示/查找SAP所有可执行程序清单,双击事务码执行、

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH004 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh004. 
 10 TABLES: trdir, tstc. 
 11 DATA: BEGIN OF gs_data, 
 12         sel   TYPE boolean,     " 用于选择多行 
 13         name  TYPE trdir-name,  " 程序名 
 14         subc  TYPE trdir-subc,  " 程序类型 
 15         rstat TYPE trdir-rstat, " 状态 
 16         tcode TYPE tstc-tcode,  " 事务码 
 17         ttext TYPE tstct-ttext, " 事务码描述 
 18         cnam  TYPE trdir-cnam,  " 创建者 
 19         cdat  TYPE trdir-cdat,  " 创建日期 
 20         unam  TYPE trdir-unam,  " 最后修改人 
 21         udat  TYPE trdir-udat,  " 修改日期 
 22       END OF gs_data. 
 23 DATA: gt_data LIKE TABLE OF gs_data. " ALV显示內表 
 24 DATA: line TYPE i." ALV行数 
 25 " 选择屏幕 
 26 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. 
 27 SELECT-OPTIONS: s_name  FOR trdir-name,            " 程序名 
 28                 s_tcode FOR  tstc-tcode,           " 事务码 
 29                 s_cnam  FOR  trdir-cnam,           " 创建者 
 30                 s_unam  FOR  trdir-unam,           " 最后修改人 
 31                 s_subc  FOR  trdir-subc DEFAULT 1, " 程序类型 
 32                 s_rstat FOR  trdir-rstat.          " 状态 
 33 SELECTION-SCREEN END OF BLOCK b1. 
 34 " F8事件 
 35  
 36 START-OF-SELECTION. 
 37   IF s_name[]  IS INITIAL AND " 程序名 
 38      s_cnam[]  IS INITIAL AND " 创建者 
 39      s_unam[]  IS INITIAL AND " 最后修改人 
 40      s_subc[]  IS INITIAL AND " 程序类型 
 41      s_tcode[] IS INITIAL AND " 事务码 
 42      s_rstat[] IS INITIAL.    " 状态 
 43  
 44     MESSAGE '请至少输入一个条件!' TYPE 'S' DISPLAY LIKE 'E'. 
 45     EXIT. 
 46   ENDIF. 
 47   PERFORM get_data. 
 48   IF gt_data[] IS INITIAL. 
 49     MESSAGE '无符合条件的记录' TYPE 'S' DISPLAY LIKE 'E'. 
 50     EXIT. 
 51   ENDIF. 
 52   PERFORM alv_data. 
 53 *&---------------------------------------------------------------------* 
 54 *&      Form  get_data 
 55 *&---------------------------------------------------------------------* 
 56 *       text  获取数据 
 57 *----------------------------------------------------------------------* 
 58 FORM get_data. 
 59   DATA: BEGIN OF ls_tstc, 
 60         tcode TYPE tstc-tcode, 
 61         ttext TYPE tstct-ttext, 
 62       END OF ls_tstc. 
 63  
 64   DATA: lt_tstc LIKE TABLE OF ls_tstc. 
 65   " 输入事务码时 
 66   IF s_tcode[] IS NOT INITIAL. 
 67     SELECT a~tcode 
 68            b~name 
 69            b~subc 
 70            b~rstat 
 71            b~cnam 
 72            b~cdat 
 73            b~unam 
 74            b~udat 
 75            c~ttext 
 76       INTO CORRESPONDING FIELDS OF TABLE gt_data 
 77       FROM tstc AS a 
 78      INNER JOIN trdir AS b ON a~pgmna = b~name 
 79      INNER JOIN tstct AS c ON a~tcode = c~tcode 
 80      WHERE a~tcode IN s_tcode 
 81        AND b~name  IN s_name   " 程序名 
 82        AND b~cnam  IN s_cnam   " 创建者 
 83        AND b~unam  IN s_unam   " 最后修改人 
 84        AND b~subc  IN s_subc   " 程序类型 
 85        AND b~rstat IN s_rstat. " 状态 
 86   ELSE. 
 87     " 没有输入事务码时 
 88     SELECT 
 89       a~name 
 90       a~subc 
 91       a~rstat 
 92       a~cnam 
 93       a~cdat 
 94       a~unam 
 95       a~udat 
 96       b~tcode 
 97       INTO CORRESPONDING FIELDS OF TABLE gt_data 
 98       FROM trdir AS a 
 99       LEFT JOIN tstc AS b ON a~name  = b~pgmna 
100       WHERE a~name  IN s_name   " 程序名 
101       AND   a~cnam  IN s_cnam   " 创建者 
102       AND   a~unam  IN s_unam   " 最后修改人 
103       AND   a~subc  IN s_subc   " 程序类型 
104       AND   a~rstat IN s_rstat. " 状态 
105  
106     IF gt_data[] IS NOT INITIAL. 
107       " 查询事务码描述文本 
108       SELECT 
109         tcode 
110         ttext 
111         INTO CORRESPONDING FIELDS OF TABLE lt_tstc 
112         FROM tstct 
113         FOR ALL ENTRIES IN gt_data 
114         WHERE tcode = gt_data-tcode. 
115  
116       SORT lt_tstc BY tcode." 先排序 
117       LOOP AT gt_data INTO gs_data. 
118         READ TABLE lt_tstc INTO ls_tstc WITH KEY tcode = gs_data-tcode BINARY SEARCH. 
119         IF sy-subrc EQ 0. 
120           gs_data-ttext = ls_tstc-ttext. 
121           MODIFY gt_data FROM gs_data TRANSPORTING ttext. 
122         ENDIF. 
123       ENDLOOP. 
124     ENDIF. 
125   ENDIF. 
126   " 统计內表行数 
127   DESCRIBE TABLE gt_data LINES line. 
128   " 排序 
129   SORT gt_data BY cnam cdat. 
130 ENDFORM.                    "get_data 
131 *&---------------------------------------------------------------------* 
132 *&      Form  alv_data 
133 *&---------------------------------------------------------------------* 
134 *       text 显示ALV数据 
135 *----------------------------------------------------------------------* 
136 FORM alv_data. 
137   DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE, 
138         layout   TYPE slis_layout_alv. 
139  
140   PERFORM get_layout CHANGING layout.   " alv布局 
141   PERFORM get_fieldcat TABLES fieldcat. " 控制报表显示哪些栏位 
142  
143   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' 
144     EXPORTING 
145       i_callback_program       = sy-repid             " 程序名 
146       is_layout                = layout               " 布局 
147       i_callback_pf_status_set = 'PFSTATUS_FORM'      " STATUS 
148       i_callback_user_command  = 'USER_COMMAND_FORM'  " 定义按钮的功能 
149       it_fieldcat              = fieldcat[]           " alv栏目(显示字段)内表 
150     TABLES 
151       t_outtab                 = gt_data              " 将内表数据赋给ALV 
152     EXCEPTIONS 
153       program_error            = 1 
154       OTHERS                   = 2. 
155   IF sy-subrc <> 0. 
156     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 
157             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
158   ENDIF. 
159 ENDFORM.                    "alv_data 
160 *&---------------------------------------------------------------------* 
161 *&      Form  get_layout 
162 *&---------------------------------------------------------------------* 
163 *       text  ALV布局设置 
164 *----------------------------------------------------------------------* 
165 *      -->C_LAYOUT   text 
166 *----------------------------------------------------------------------* 
167 FORM get_layout CHANGING c_layout TYPE slis_layout_alv. 
168   CLEAR: c_layout. 
169   c_layout-colwidth_optimize = 'X'." 宽度自动调节 
170   c_layout-box_fieldname = 'SEL'.  " 选择多行 
171   c_layout-zebra = 'X'.            " 颜色交替显示 
172 ENDFORM.                    "get_layout 
173 *&---------------------------------------------------------------------* 
174 *&      Form  USER_COMMAND_FORM 
175 *&---------------------------------------------------------------------* 
176 *       text  自定义按钮功能 
177 *----------------------------------------------------------------------* 
178 *      -->R_UCOMM      text 
179 *      -->RS_SELFIELD  text 
180 *----------------------------------------------------------------------* 
181 FORM user_command_form USING r_ucomm     LIKE sy-ucomm 
182                              rs_selfield TYPE slis_selfield. 
183   READ TABLE gt_data INTO gs_data INDEX rs_selfield-tabindex. 
184   IF sy-subrc EQ 0 AND gs_data-tcode IS NOT INITIAL. 
185     CASE r_ucomm. 
186       WHEN '&IC1'. " 双击事件 
187         CALL TRANSACTION gs_data-tcode. " 调用事务码,运行程序 
188       WHEN OTHERS. 
189         MESSAGE '双击事务码执行' TYPE 'S'. 
190     ENDCASE. 
191   ENDIF. 
192 ENDFORM.                    "USER_COMMAND_FORM 
193 *&---------------------------------------------------------------------* 
194 *&      Form  get_fieldcat 
195 *&---------------------------------------------------------------------* 
196 *       text 显示字段 
197 *----------------------------------------------------------------------* 
198 *      -->CT_TAB     text 
199 *----------------------------------------------------------------------* 
200 FORM get_fieldcat TABLES ct_tab TYPE slis_t_fieldcat_alv. 
201   DATA: fcat TYPE slis_fieldcat_alv. 
202   DEFINE add_fcat. 
203     clear fcat. 
204     fcat-fieldname       = &1. " 字段名 
205     fcat-seltext_l       = &2. " 显示字段文本 
206     fcat-seltext_m       = &2. 
207     fcat-seltext_s       = &2. 
208     fcat-key             = &3. " 主键 
209     fcat-hotspot         = &4. " 链接 
210     fcat-checkbox        = &5. " 复选框 
211     fcat-edit            = &6. " 是否可修改 
212     fcat-ref_tabname     = &7. " 参考表 
213     fcat-ref_fieldname   = &8. " 参考表字段 
214     append fcat to ct_tab. 
215   END-OF-DEFINITION. 
216   add_fcat 'NAME'  '程序名'      ''  ''  ''  ''  ''  ''. 
217   add_fcat 'SUBC'  '程序类型'    ''  ''  ''  ''  'TRDIR'  'SUBC'. 
218   add_fcat 'RSTAT' '状态'        ''  ''  ''  ''  'TRDIR'  'RSTAT'. 
219   add_fcat 'TCODE' '事务码'      ''  ''  ''  ''  ''  ''. 
220   add_fcat 'TTEXT' '事务码描述'  ''  ''  ''  ''  ''  ''. 
221   add_fcat 'CNAM'  '创建者'  ''  ''  ''  ''  ''  ''. 
222   add_fcat 'CDAT'  '创建日期'    ''  ''  ''  ''  ''  ''. 
223   add_fcat 'UNAM'  '最后修改人'  ''  ''  ''  ''  ''  ''. 
224   add_fcat 'UDAT'  '修改日期'    ''  ''  ''  ''  ''  ''. 
225 ENDFORM.                    "get_fieldcat 
226 *&---------------------------------------------------------------------* 
227 *&      Form  pfstatus_form 
228 *&---------------------------------------------------------------------* 
229 *       text  STATUS 
230 *----------------------------------------------------------------------* 
231 *      -->RT_EXTAB   text 
232 *----------------------------------------------------------------------* 
233 FORM pfstatus_form USING rt_extab TYPE slis_t_extab. 
234   SET TITLEBAR 'ZCHENH004' WITH line. " 标题 
235   SET PF-STATUS 'ZCHENH004'.         " 工具条 
236 ENDFORM.                    "pfstatus_form </span>

View Code

2.程序异常捕获,防止出现ST22的dump页面

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH005 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh005. 
10 DATA:gv_test   TYPE i , 
11      err_text TYPE string, 
12      e TYPE REF TO cx_root. 
13 *捕获异常 
14 TRY. 
15     gv_test = 'X'. 
16     EXIT. 
17   CATCH cx_root INTO e. 
18     err_text = e->get_text( ). 
19     WRITE:err_text. 
20  
21 ENDTRY. 
22 TRY. 
23     gv_test = 1 / 0. 
24     EXIT. 
25   CATCH cx_root INTO e. 
26     err_text = e->get_text( ). 
27     WRITE:err_text. 
28   CLEANUP. 
29  
30 *    RAISE EXCEPTION e. " 使界面红掉 
31     " 
32 ENDTRY.

View Code

 1 *&---------------------------------------------------------------------*  2 *& Report  ZCHENH017  3 *&  4 *&---------------------------------------------------------------------*  5 *&  6 *&  7 *&---------------------------------------------------------------------*  8   9 REPORT zchenh017. 10 DATA:gv_test   TYPE i , 11      err_text TYPE string, 12      e TYPE REF TO cx_root. 13 TRY. 14     gv_test = 'X'. 15 *    正常代码部分 16 * 17 * 18 * 19 *    EXIT. 20   CATCH cx_root INTO e. 21     err_text = e->get_text( ). 22     IF sy-batch = 'X'. 23       WRITE:'后台异常测试:',err_text. 24     ELSE. 25       WRITE:'前台异常测试:',err_text. 26     ENDIF. 27   CLEANUP. 28     CLEAR:err_text. 29 ENDTRY. 30 WRITE:'前台异常测试:',err_text.

View Code

3.ALV快速开发,需要在SE41中拷贝标准状态栏,

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH007 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh007. 
 10 TABLES:zchenh001. 
 11 DATA:gs_data TYPE zchenh001, 
 12      gt_data TYPE zchenh001 OCCURS 0 WITH HEADER LINE. 
 13 DATA:BEGIN OF gt_alv OCCURS 0. 
 14 DATA :box(1), 
 15       exception(1). 
 16         INCLUDE TYPE zchenh001. 
 17 DATA END OF gt_alv. 
 18  
 19  
 20 PERFORM frm_insert_data. 
 21 PERFORM frm_show_data. 
 22  
 23 *&---------------------------------------------------------------------* 
 24 *&      Form  FRM_INSERT_DATA 
 25 *&---------------------------------------------------------------------* 
 26 *       text 
 27 *----------------------------------------------------------------------* 
 28 *  -->  p1        text 
 29 *  <--  p2        text 
 30 *----------------------------------------------------------------------* 
 31 FORM frm_insert_data . 
 32  
 33   DO 10 TIMES. 
 34     CLEAR:gs_data,gt_data. 
 35     gs_data-znum = sy-index. 
 36     CONCATENATE '陈辉' 'Test' INTO gs_data-zname. 
 37     IF sy-index MOD 2 EQ 0. 
 38       gs_data-zsex = ''. 
 39     ELSE. 
 40       gs_data-zsex = ''. 
 41     ENDIF. 
 42     gs_data-zhigh = '185'. 
 43     APPEND gs_data TO gt_data. 
 44   ENDDO. 
 45  
 46 *  更新数据库表 等待千分之一秒后提交数据库 
 47   MODIFY zchenh001 FROM TABLE gt_data. 
 48   WAIT UP TO 1 / 1000 SECONDS. 
 49   COMMIT WORK. 
 50  
 51 *  DELETE zchenh001 FROM TABLE gt_data. 
 52   LOOP AT gt_data. 
 53     MOVE-CORRESPONDING gt_data TO gt_alv. 
 54     gt_alv-exception = '3'. 
 55     APPEND gt_alv. 
 56   ENDLOOP. 
 57 ENDFORM.                    " FRM_INSERT_DATA 
 58 *&---------------------------------------------------------------------* 
 59 *&      Form  FRM_SHOW_DATA 
 60 *&---------------------------------------------------------------------* 
 61 *       text 
 62 *----------------------------------------------------------------------* 
 63 *  -->  p1        text 
 64 *  <--  p2        text 
 65 *----------------------------------------------------------------------* 
 66 FORM frm_show_data . 
 67   DATA: fieldcat        TYPE lvc_t_fcat WITH HEADER LINE, 
 68         l_grid_settings TYPE lvc_s_glay, 
 69         layout          TYPE lvc_s_layo, 
 70         lv_title        TYPE lvc_title. 
 71   PERFORM frm_build_fieldcat TABLES fieldcat. 
 72   lv_title = '数据展示'. 
 73   layout-excp_fname = 'EXCEPTION'. 
 74   layout-zebra = 'X'. 
 75   layout-cwidth_opt = 'X'. 
 76   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' 
 77     EXPORTING 
 78       i_callback_program          = sy-repid 
 79       is_layout_lvc               = layout 
 80       it_fieldcat_lvc             = fieldcat[] 
 81       i_callback_user_command     = 'USER_COMMAND' 
 82       i_callback_pf_status_set    = 'SET_PF_STATUS' 
 83 *     i_callback_html_top_of_page = 'TOP_OF_PAGE' "调用TOP_OF_PAGE FORM 
 84       i_grid_settings             = l_grid_settings 
 85       i_grid_title                = lv_title 
 86       i_save                      = 'U' 
 87 *     IT_EVENTS                   = GT_EVENTS[] 
 88     TABLES 
 89       t_outtab                    = gt_alv[] 
 90     EXCEPTIONS 
 91       program_error               = 1 
 92       OTHERS                      = 2. 
 93 ENDFORM.                    " FRM_SHOW_DATA 
 94 *&---------------------------------------------------------------------* 
 95 *&      Form  FRM_BUILD_FIELDCAT 
 96 *&---------------------------------------------------------------------* 
 97 *       text 
 98 *----------------------------------------------------------------------* 
 99 *      -->P_FIELDCAT  text 
100 *----------------------------------------------------------------------* 
101 FORM frm_build_fieldcat  TABLES fieldcat. 
102   DATA: lt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE. 
103   DEFINE add2fc. 
104     lt_fieldcat-fieldname = &1. 
105     lt_fieldcat-scrtext_l = &2. 
106     lt_fieldcat-edit = &3. 
107     lt_fieldcat-checkbox = &4. 
108     lt_fieldcat-ref_table = &5. 
109     lt_fieldcat-ref_field = &6. 
110     lt_fieldcat-no_zero = 'X'. 
111     lt_fieldcat-fix_column = &7. 
112     lt_fieldcat-hotspot = &8. 
113     lt_fieldcat-outputlen = 100. 
114     append lt_fieldcat. 
115     clear lt_fieldcat. 
116   END-OF-DEFINITION. 
117   add2fc: 
118      'BOX' '复选框' 'X' 'X' '' '' 'X' '', 
119      'ZNUM' '序号' '' '' '' '' 'X' '', 
120      'ZNAME' '姓名' '' '' '' '' 'X' '', 
121      'ZSEX' '性别' '' '' '' '' 'X' '', 
122      'ZHIGH' '身高' '' '' '' '' 'X' ''. 
123   fieldcat[] = lt_fieldcat[]. 
124  
125 ENDFORM.                    " FRM_BUILD_FIELDCAT 
126  
127  
128 *&---------------------------------------------------------------------* 
129 *&      Form  frm_set_pf_status_ALL 
130 *&---------------------------------------------------------------------* 
131 *       text 
132 *----------------------------------------------------------------------* 
133 *      -->RT_EXTAB   text 
134 *----------------------------------------------------------------------* 
135 FORM set_pf_status USING rt_extab TYPE slis_t_extab. 
136   SET PF-STATUS 'ZCHENH007'. 
137 ENDFORM.                    "set_pf_status 
138  
139 *&---------------------------------------------------------------------* 
140 *&      Form  USER_COMMAND 
141 *&---------------------------------------------------------------------* 
142 *       text 
143 *----------------------------------------------------------------------* 
144 *      -->R_UCOMM      text 
145 *      -->RS_SELFIELD  text 
146 *----------------------------------------------------------------------* 
147 FORM user_command  USING r_ucomm LIKE sy-ucomm 
148                          rs_selfield TYPE slis_selfield. 
149   CASE r_ucomm. 
150     WHEN '&F03' OR '&F15' OR '&F12'. 
151       SET SCREEN 0. 
152       LEAVE SCREEN. 
153     WHEN '&PRINT'. 
154       PERFORM frm_print_data. 
155     WHEN 'ALL'. 
156       LOOP AT gt_alv . 
157         gt_alv-box = 'X'. 
158         MODIFY gt_alv TRANSPORTING box. 
159       ENDLOOP . 
160     WHEN 'SAL'. 
161       LOOP AT gt_alv. 
162         gt_alv-box = ''. 
163         MODIFY gt_alv TRANSPORTING box. 
164       ENDLOOP . 
165     WHEN OTHERS. 
166   ENDCASE. 
167   rs_selfield-refresh = 'X'. 
168 ENDFORM.                    "frm_user_command_ALL 
169  
170 *&---------------------------------------------------------------------* 
171 *&      Form  FRM_PRINT_DATA 
172 *&---------------------------------------------------------------------* 
173 *       text 
174 *----------------------------------------------------------------------* 
175 *  -->  p1        text 
176 *  <--  p2        text 
177 *----------------------------------------------------------------------* 
178 FORM frm_print_data . 
179   DATA:lt_data TYPE zchenh001 OCCURS 0 WITH HEADER LINE. 
180   LOOP AT gt_alv WHERE box = 'X' AND exception = '3'. 
181     MOVE-CORRESPONDING gt_alv TO lt_data. 
182     APPEND lt_data. 
183   ENDLOOP. 
184   EXPORT lt_data TO MEMORY ID 'GT_HEAD'. 
185 ENDFORM.                    " FRM_PRINT_DATA

View Code

事务码:SE41 -> 状态

从程序:SAPLKKBL

 状态:STANDARD_FULLSCREEN

到程序:XXXXXXXX

 状态:XXXXXXXX

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH011 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh011. 
 10 *声明表 
 11 * 定义内表 变量 常量 结构 
 12 DATA: BEGIN OF gt_alv  OCCURS 0, 
 13          col1  TYPE char50,  " alv显示的字段一 
 14          col2  TYPE char50,  " alv显示的字段二 
 15          col3  TYPE char50,  " alv显示的字段三 
 16          col4  TYPE char50,  " alv显示的字段四 
 17          exception, 
 18          id TYPE icon-id,  " 图标 
 19          msg(255), 
 20        END OF gt_alv. 
 21 *声明包含文件 
 22 *定义选择屏幕 
 23 *选择屏幕的PAI PBO 
 24 PERFORM frm_get_data. 
 25 PERFORM frm_pro_data. 
 26 PERFORM frm_show_data. 
 27 *&---------------------------------------------------------------------* 
 28 *&      Form  FRM_GET_DATA 
 29 *&---------------------------------------------------------------------* 
 30 *       从表中或者其他地方抓取数据 
 31 *----------------------------------------------------------------------* 
 32 *  -->  p1        text 
 33 *  <--  p2        text 
 34 *----------------------------------------------------------------------* 
 35 FORM frm_get_data . 
 36  
 37 ENDFORM.                    " FRM_GET_DATA 
 38 *&---------------------------------------------------------------------* 
 39 *&      Form  FRM_PRO_DATA 
 40 *&---------------------------------------------------------------------* 
 41 *       对数据进行处理 加工 
 42 *----------------------------------------------------------------------* 
 43 *  -->  p1        text 
 44 *  <--  p2        text 
 45 *----------------------------------------------------------------------* 
 46 FORM frm_pro_data . 
 47   DATA:lv_id TYPE icon-id. 
 48   SELECT SINGLE id INTO lv_id FROM icon WHERE name = icon_led_green. 
 49   DO 10 TIMES. 
 50     gt_alv-col1 = sy-index. 
 51     gt_alv-col2 = sy-index + sy-index. 
 52     gt_alv-col3 = sy-index * sy-index. 
 53     gt_alv-col4 = sy-index MOD 2. 
 54     gt_alv-exception = 3. 
 55     gt_alv-msg = 'OK!'. 
 56     gt_alv-id = lv_id. 
 57     APPEND gt_alv. 
 58   ENDDO. 
 59 ENDFORM.                    " FRM_PRO_DATA 
 60 *&---------------------------------------------------------------------* 
 61 *&      Form  FRM_SHOW_DATA 
 62 *&---------------------------------------------------------------------* 
 63 *       text 
 64 *----------------------------------------------------------------------* 
 65 *  -->  p1        text 
 66 *  <--  p2        text 
 67 *----------------------------------------------------------------------* 
 68 FORM frm_show_data . 
 69   DATA: fieldcat        TYPE lvc_t_fcat WITH HEADER LINE, 
 70        l_grid_settings TYPE lvc_s_glay, 
 71        layout          TYPE lvc_s_layo, 
 72        lv_title        TYPE lvc_title. 
 73   PERFORM frm_build_fieldcat TABLES fieldcat. 
 74   lv_title = '数据展示'. 
 75   layout-excp_fname = 'EXCEPTION'. 
 76   layout-zebra = 'X'. 
 77   layout-cwidth_opt = 'X'. 
 78   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' 
 79     EXPORTING 
 80       i_callback_program          = sy-repid 
 81       is_layout_lvc               = layout 
 82       it_fieldcat_lvc             = fieldcat[] 
 83       i_callback_user_command     = 'USER_COMMAND' 
 84       i_callback_pf_status_set    = 'SET_PF_STATUS' 
 85 *     i_callback_html_top_of_page = 'TOP_OF_PAGE' "调用TOP_OF_PAGE FORM 
 86       i_grid_settings             = l_grid_settings 
 87       i_grid_title                = lv_title 
 88       i_save                      = 'U' 
 89 *     IT_EVENTS                   = GT_EVENTS[] 
 90     TABLES 
 91       t_outtab                    = gt_alv[] 
 92     EXCEPTIONS 
 93       program_error               = 1 
 94       OTHERS                      = 2. 
 95 ENDFORM.                    " FRM_SHOW_DATA 
 96 *&---------------------------------------------------------------------* 
 97 *&      Form  FRM_BUILD_FIELDCAT 
 98 *&---------------------------------------------------------------------* 
 99 *       text 
100 *----------------------------------------------------------------------* 
101 *      -->P_FIELDCAT  text 
102 *----------------------------------------------------------------------* 
103 FORM frm_build_fieldcat TABLES fieldcat. 
104   DATA: lt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE. 
105   DEFINE add2fc. 
106     lt_fieldcat-fieldname = &1. 
107     lt_fieldcat-scrtext_l = &2. 
108     lt_fieldcat-edit = &3. 
109     lt_fieldcat-checkbox = &4. 
110     lt_fieldcat-ref_table = &5. 
111     lt_fieldcat-ref_field = &6. 
112     lt_fieldcat-no_zero = 'X'. 
113     lt_fieldcat-fix_column = &7. 
114     lt_fieldcat-hotspot = &8. 
115     lt_fieldcat-icon = &9. 
116     lt_fieldcat-outputlen = 100. 
117     append lt_fieldcat. 
118     clear lt_fieldcat. 
119   END-OF-DEFINITION. 
120   add2fc: 
121      'COL1' '字段一' '' '' '' '' 'X' '' '', 
122      'COL2' '字段二' '' '' '' '' 'X' '' '', 
123      'COL3' '字段三' '' '' '' '' 'X' '' '', 
124      'COL4' '字段四' '' '' '' '' 'X' '' '', 
125      'ID'    '图标' '' '' '' '' 'X' '' 'X', 
126      'MSG' '消息' '' '' '' '' 'X' '' ''. 
127   fieldcat[] = lt_fieldcat[]. 
128 ENDFORM.                    " FRM_BUILD_FIELDCAT 
129  
130 *&---------------------------------------------------------------------* 
131 *&      Form  frm_set_pf_status_ALL 
132 *&---------------------------------------------------------------------* 
133 *       text 
134 *----------------------------------------------------------------------* 
135 *      -->RT_EXTAB   text 
136 *----------------------------------------------------------------------* 
137 FORM set_pf_status USING rt_extab TYPE slis_t_extab. 
138   SET PF-STATUS 'ZCHENH011'. 
139 ENDFORM.                    "set_pf_status 
140  
141 *&---------------------------------------------------------------------* 
142 *&      Form  USER_COMMAND 
143 *&---------------------------------------------------------------------* 
144 *       用户在ALV上的命令 
145 *----------------------------------------------------------------------* 
146 *      -->R_UCOMM      text 
147 *      -->RS_SELFIELD  text 
148 *----------------------------------------------------------------------* 
149 FORM user_command  USING r_ucomm LIKE sy-ucomm 
150                           rs_selfield TYPE slis_selfield. 
151   CASE r_ucomm. 
152     WHEN '&F03' OR '&F15' OR '&F12'. 
153       SET SCREEN 0. 
154       LEAVE SCREEN. 
155     WHEN OTHERS. 
156   ENDCASE. 
157  
158   rs_selfield-refresh = 'X'. 
159 ENDFORM.                    "frm_user_command_ALL

View Code

4.有些SE11/SE16表不能修改表中数据时,

  通过SE37 ->  SE16N_INTERFACE

I_TAB:表名,

I_EDIT :X,

I_SAPEDIT:X 

F8执行,选中需要删除或修改的数据

5.SAP中金额转为汉字

示例:100001.11 =》 壹拾万零壹元壹角壹分整

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH008 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh008. 
10 DATA:lv_in TYPE dmbtr, 
11      lv_out TYPE string. 
12 lv_in = '100001.11'. 
13 *lv_in = 0 - lv_in. 
14 PERFORM frm_conv_amount USING lv_in 
15                         CHANGING lv_out. 
16 WRITE:/ lv_out. 
17 *&---------------------------------------------------------------------* 
18 *&      Form  frm_conv_amount 
19 *&---------------------------------------------------------------------* 
20 *       text 
21 *----------------------------------------------------------------------* 
22 *      -->VALUE      text 
23 *      -->(P_SOURCE) text 
24 *      -->VALUE      text 
25 *      -->(P_RESULT) text 
26 *----------------------------------------------------------------------* 
27 FORM frm_conv_amount USING value(p_source) 
28                      CHANGING value(p_result). 
29  
30   DATA: scr(20) TYPE c, res(60) TYPE c. 
31   DATA: len TYPE i, c1 TYPE i, c2 TYPE i, c3 TYPE i. 
32   DATA: d1(1) TYPE c, d2(1) TYPE c, d3 TYPE i. 
33   DATA: digit(2) TYPE c, weight(2) TYPE c. 
34   DATA: rule1(20) TYPE c VALUE '零壹贰叁肆伍陆柒捌玖'. 
35   DATA: rule2(36) TYPE c VALUE '分角元拾佰仟万拾佰仟亿拾佰仟万拾佰仟'. 
36  
37   scr = p_source * 100. 
38  
39   CONDENSE scr NO-GAPS. 
40  
41   IF scr = '0'. 
42     res = '零元'. 
43   ELSE. 
44     len = strlen( scr ). 
45     c1 = 0. 
46     d1 = '0'. 
47  
48     CLEAR res. 
49  
50     DO len TIMES. 
51       c1 = c1 + 1. 
52       c2 = len - c1. 
53       MOVE scr+c2(1) TO d2. 
54       d3 = d2. 
55       MOVE rule1+d3(1) TO digit. 
56       c3 = ( c1 - 1 ). 
57       MOVE rule2+c3(1) TO weight. 
58       IF d2 = '0'. 
59         IF c1 = 3. 
60           digit = ''. 
61         ELSEIF c1 = 7. 
62           digit = ''. 
63         ELSEIF d1 = '0'. 
64           digit = ''. 
65           weight = ''. 
66         ELSE. 
67           weight = ''. 
68         ENDIF. 
69       ENDIF. 
70  
71       CONCATENATE digit weight res INTO res. 
72       d1 = d2. 
73  
74     ENDDO. 
75   ENDIF. 
76  
77   CONCATENATE res '' INTO p_result. 
78 ENDFORM.                    "frm_conv_amount

View Code

 6.SE24中使用自定义的类

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH009 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh009. 
10 DATA: object TYPE REF TO zchenh002. 
11 DATA:speed TYPE i. 
12  
13 START-OF-SELECTION. 
14  
15   CREATE OBJECT object. 
16   CALL METHOD object->show_speed. 
17   CALL METHOD object->add_speed. 
18   CALL METHOD object->show_speed. 
19   speed = 10. 
20   CALL METHOD object->set_speed 
21     EXPORTING 
22       speed_in = speed. 
23   write:'程序中的Speed-->:',speed. 
24   CLEAR:speed. 
25   CALL METHOD object->add_speed. 
26   CALL METHOD object->get_speed 
27     IMPORTING 
28       speed_out = speed. 
29   write:'程序中的Speed-->:',speed.

View Code

7.模拟SM37后台JOB

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH010 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh010. 
 10 DATA: jobcount LIKE tbtcjob-jobcount. 
 11 DATA: abort TYPE c, 
 12       finish TYPE c, 
 13       prelim TYPE c, 
 14       ready TYPE c, 
 15       run TYPE c, 
 16       sched TYPE c. 
 17  
 18 PARAMETERS: p_name LIKE tbtcjob-jobname OBLIGATORY DEFAULT 'Z_BARRY_TEST_JOB', 
 19              p_date LIKE sy-datum OBLIGATORY, 
 20              p_time LIKE sy-uzeit OBLIGATORY, 
 21              p_varint LIKE raldb-variant. 
 22  
 23 SELECT-OPTIONS s_rept FOR sy-repid NO INTERVALS OBLIGATORY . 
 24  
 25 START-OF-SELECTION. 
 26   CALL FUNCTION 'JOB_OPEN' "定义一个Job 
 27       EXPORTING 
 28         jobname          = p_name 
 29       IMPORTING 
 30         jobcount         = jobcount "Job id 
 31       EXCEPTIONS 
 32         cant_create_job = 1 
 33         invalid_job_data = 2 
 34         jobname_missing = 3 
 35         OTHERS           = 4. 
 36   IF sy-subrc <> 0. 
 37     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 
 38             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
 39   ENDIF. 
 40  
 41   LOOP AT s_rept . 
 42     CALL FUNCTION 'JOB_SUBMIT' "Scheduled 这个Job 
 43       EXPORTING 
 44         authcknam               = sy-uname 
 45         jobcount                = jobcount 
 46         jobname                 = p_name 
 47         report                  = s_rept-low 
 48         variant                 = p_varint 
 49       EXCEPTIONS 
 50         bad_priparams           = 1 
 51         bad_xpgflags            = 2 
 52         invalid_jobdata         = 3 
 53         jobname_missing         = 4 
 54         job_notex               = 5 
 55         job_submit_failed       = 6 
 56         lock_failed             = 7 
 57         program_missing         = 8 
 58         prog_abap_and_extpg_set = 9 
 59         OTHERS                  = 10. 
 60     IF sy-subrc <> 0. 
 61       MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 
 62               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
 63     ENDIF. 
 64   ENDLOOP. 
 65  
 66   CALL FUNCTION 'JOB_CLOSE' "Release 这个Job 
 67        EXPORTING 
 68          jobcount             = jobcount 
 69          jobname              = p_name 
 70          sdlstrtdt            = p_date 
 71          sdlstrttm            = p_time 
 72        EXCEPTIONS 
 73          cant_start_immediate = 1 
 74          invalid_startdate    = 2 
 75          jobname_missing      = 3 
 76          job_close_failed     = 4 
 77          job_nosteps          = 5 
 78          job_notex            = 6 
 79          lock_failed          = 7 
 80          OTHERS               = 8. 
 81   IF sy-subrc <> 0. 
 82     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 
 83             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
 84   ENDIF. 
 85  
 86   CALL FUNCTION 'SHOW_JOBSTATE' 
 87     EXPORTING 
 88       jobcount         = jobcount 
 89       jobname          = p_name 
 90     IMPORTING 
 91       aborted          = abort 
 92       finished         = finish 
 93       preliminary      = prelim 
 94       ready            = ready 
 95       running          = run 
 96       scheduled        = sched 
 97     EXCEPTIONS 
 98       jobcount_missing = 1 
 99       jobname_missing  = 2 
100       job_notex        = 3 
101       OTHERS           = 4. 
102   IF sy-subrc <> 0. 
103     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 
104             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
105   ENDIF. 
106  
107   WRITE : / 'Canceled              :' , abort , 
108           / 'Finished              :' , finish, 
109           / 'Scheduled Temporarily :' , prelim, 
110           / 'Ready for Execution   :' , ready, 
111           / 'Active                :' , run, 
112           / 'Scheduling Released   :' , sched.

View Code

8.内表转JSON字符串 ,JSON转ABAP内表

方法一:

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH012 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh012. 
10 TABLES:zchenh001. 
11 DATA:err_text TYPE string, 
12      e TYPE REF TO cx_root. 
13 DATA: json_ser TYPE REF TO cl_trex_json_serializer, 
14       json_des TYPE REF TO cl_trex_json_deserializer. 
15 DATA: jsonstr TYPE string. 
16 DATA:  BEGIN OF itab OCCURS 0. 
17         INCLUDE TYPE zchenh001. 
18 DATA END OF itab. 
19 DATA:gs_zchenh001 TYPE zchenh001. 
20  
21 START-OF-SELECTION. 
22   TRY . 
23  
24       SELECT * INTO CORRESPONDING FIELDS OF TABLE itab 
25         FROM zchenh001 UP TO 10 ROWS. 
26  
27  
28 ***内表->JSON 
29       CREATE OBJECT json_ser 
30         EXPORTING 
31           data = itab[]. 
32       CALL METHOD json_ser->serialize. 
33       CALL METHOD json_ser->get_data 
34         RECEIVING 
35           rval = jsonstr. 
36  
37       WRITE:'内表转JSON',/, jsonstr,/. 
38  
39 ***JSON->内表 
40       CLEAR:itab,itab[]. 
41       CREATE OBJECT json_des. 
42       CALL METHOD json_des->deserialize 
43         EXPORTING 
44           json = jsonstr 
45         IMPORTING 
46           abap = itab[]. 
47       WRITE:'JSON转内表',/. 
48       LOOP AT itab INTO gs_zchenh001. 
49         WRITE:gs_zchenh001-znum,gs_zchenh001-zname,gs_zchenh001-zsex,gs_zchenh001-zhigh,/. 
50       ENDLOOP. 
51  
52     CATCH cx_root INTO e. 
53       err_text = e->get_text( ). 
54       WRITE:err_text. 
55  
56   ENDTRY.

View Code

方法二:

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH013 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh013. 
10  
11 DATA:err_text TYPE string, 
12      e TYPE REF TO cx_root. 
13  
14 DATA:lv_content TYPE string. 
15 DATA:lv_content1 TYPE string. 
16 DATA:lv_json TYPE string . 
17 DATA json_writer TYPE REF TO cl_sxml_string_writer. 
18  
19 DATA:gs_zchenh001 TYPE zchenh001. 
20 DATA:gt_oldtab    TYPE TABLE OF zchenh001. 
21 DATA:gt_newtab    TYPE TABLE OF zchenh001. 
22  
23 START-OF-SELECTION. 
24   TRY . 
25       lv_content = 'CONTENT'. 
26       SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_oldtab 
27         FROM zchenh001 UP TO 10 ROWS. 
28 ***abap内表->JSON 
29       json_writer  = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). 
30       CALL TRANSFORMATION id 
31                           SOURCE content = lv_content 
32                                  itable  = gt_oldtab 
33                           RESULT XML json_writer. 
34       DATA:json TYPE etxml_line_str. 
35       json = json_writer->get_output( ). 
36  
37       CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING' 
38         EXPORTING 
39           im_xstring  = json 
40           im_encoding = 'UTF-8' 
41         IMPORTING 
42           ex_string   = lv_json. 
43  
44       "  json 转 abap内表 
45       CALL TRANSFORMATION id 
46                           SOURCE XML lv_json 
47                           RESULT content = lv_content1 
48                                  itable = gt_newtab. 
49  
50       WRITE:'转换后JSON:',lv_json,/,'转换后内表',/. 
51       LOOP AT gt_newtab INTO gs_zchenh001 . 
52         WRITE:gs_zchenh001-znum,gs_zchenh001-zname,gs_zchenh001-zsex,gs_zchenh001-zhigh,/. 
53       ENDLOOP. 
54     CATCH cx_root INTO e. 
55       err_text = e->get_text( ). 
56       WRITE:err_text. 
57  
58   ENDTRY.

View Code

9.读取SAP的页编码格式
       1.登录界面 -> 配置属性 -> 代码页  语言,编码

2.I18N 代码页配置,

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH016 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh016. 
10 *DATA:  l_codepage(4) TYPE n . 
11 * 
12 *DATA:  l_encoding(20). 
13 * 
14 *CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME' 
15 *  EXPORTING 
16 *    external_name = 'GB2312' 
17 *  IMPORTING 
18 *    sap_codepage  = l_codepage. 
19 * 
20 *l_encoding = l_codepage. 
21 * 
22 *WRITE:'--------->',l_encoding. 
23 *SKIP. 
24  
25 DATA:name TYPE  c, 
26     entry00 LIKE  tcp00, 
27     cpisinf1 LIKE  rscptype-cpisinf1, 
28     cpusedbyf1 LIKE  tcp00-cpcodepage, 
29     cpisinf4 LIKE  rscptype-cpisinf4, 
30     cpisinf5 LIKE  rscptype-cpisinf5, 
31     cpencoding_text TYPE  c, 
32     cpbyteind_text TYPE  c, 
33     cpextmeth_text TYPE  c, 
34     cpnlsmajor_text TYPE  c, 
35     cpsource_text TYPE  c, 
36     http_name TYPE  c, 
37     java_name TYPE  c, 
38     mime_name TYPE  c, 
39     cpg_name TYPE  c, 
40     fcha_name TYPE  c, 
41     matching_ascii TYPE  c. 
42  
43 DATA:lv_code TYPE tcp00-cpcodepage. 
44  
45 DO 1000 TIMES. 
46   lv_code = sy-index. 
47   CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
48     EXPORTING 
49       input  = lv_code 
50     IMPORTING 
51       output = lv_code. 
52  
53   CALL FUNCTION 'SCP_CODEPAGE_INFO' 
54     EXPORTING 
55       codepage         = lv_code 
56     IMPORTING 
57       name             = name 
58       entry00          = entry00 
59       cpisinf1         = cpisinf1 
60       cpusedbyf1       = cpusedbyf1 
61       cpisinf4         = cpisinf4 
62       cpisinf5         = cpisinf5 
63       cpencoding_text  = cpencoding_text 
64       cpbyteind_text   = cpbyteind_text 
65       cpextmeth_text   = cpextmeth_text 
66       cpnlsmajor_text  = cpnlsmajor_text 
67       cpsource_text    = cpsource_text 
68       http_name        = http_name 
69       java_name        = java_name 
70       mime_name        = mime_name 
71       cpg_name         = cpg_name 
72       fcha_name        = fcha_name 
73       matching_ascii   = matching_ascii 
74     EXCEPTIONS 
75       invalid_codepage = 1 
76       internal_error   = 2 
77       OTHERS           = 3. 
78   IF sy-subrc <> 0. 
79  
80   ENDIF. 
81  
82   WRITE:'NAME',name,/ 
83         'ENTRY00',entry00,/ 
84         'CPUSEDBYF1',cpusedbyf1. 
85  
86 ENDDO.

View Code

 10.SAP中模糊查询  使用like和 % 通配符

 1 DATA: l_setname TYPE setnamenew. 
 2  
 3   e_ret = 'S'. 
 4  
 5   CONCATENATE i_setname '%' INTO l_setname. 
 6  
 7 * 读取集值 
 8   SELECT setname lineid valsign valoption valfrom valto 
 9     INTO TABLE t_setleaf 
10     FROM setleaf 
11     WHERE setname LIKE l_setname. 
12  
13   IF sy-subrc NE 0. 
14     e_ret = 'F'. 
15     e_msg = '未取到集数据'. 
16  
17     EXIT. 
18   ENDIF.

View Code

11.SAP中关于Excel的OLE操作

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH021 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh021. 
 10 TYPE-POOLS ole2 . 
 11 DATA:  count TYPE i, 
 12        count_real TYPE i, 
 13        application TYPE ole2_object, 
 14        workbook TYPE ole2_object, 
 15        excel     TYPE ole2_object, 
 16        sheet TYPE ole2_object, 
 17        cells TYPE ole2_object. 
 18 CONSTANTS: row_max TYPE i VALUE 256. 
 19 DATA index TYPE i. 
 20  
 21  
 22 DATA: 
 23       h_cell        TYPE ole2_object,        " cell 
 24       h_f           TYPE ole2_object,        " font 
 25       h_int         TYPE ole2_object, 
 26       h_width       TYPE ole2_object, 
 27       h_columns     TYPE ole2_object, 
 28       h_rows        TYPE ole2_object, 
 29       h_font        TYPE ole2_object, 
 30       h_entirecol   TYPE ole2_object. 
 31 . 
 32  
 33 CREATE OBJECT excel 'EXCEL.APPLICATION'. 
 34  
 35 IF sy-subrc NE 0. 
 36   WRITE: / 'No EXCEL creation possible'. 
 37   STOP. 
 38 ENDIF. 
 39  
 40 SET PROPERTY OF excel 'DisplayAlerts' = 0. 
 41  
 42 CALL METHOD OF 
 43     excel 
 44     'WORKBOOKS' = workbook. 
 45  
 46 SET PROPERTY OF excel 'VISIBLE' = 1. 
 47  
 48  
 49 SET PROPERTY OF excel 'SheetsInNewWorkbook' = 1. 
 50 CALL METHOD OF 
 51     workbook 
 52     'ADD'. 
 53  
 54 CALL METHOD OF 
 55     excel 
 56     'WORKSHEETS' = sheet 
 57   EXPORTING 
 58     #1           = 1. 
 59  
 60 SET PROPERTY OF sheet 'NAME' = 'Color Palette'. 
 61 CALL METHOD OF 
 62     sheet 
 63     'ACTIVATE'. 
 64  
 65 DATA: col TYPE i VALUE 1, 
 66       row TYPE i VALUE 2, 
 67       col1 TYPE i VALUE 2, 
 68       col_real TYPE i VALUE 1. 
 69  
 70 row = 1. 
 71 col = 2. 
 72 CALL METHOD OF 
 73     excel 
 74     'Cells' = h_cell 
 75   EXPORTING 
 76     #1      = row 
 77     #2      = col. 
 78 SET PROPERTY OF h_cell 'Value' = 'No.'. 
 79  
 80 col = col + 1. 
 81 CALL METHOD OF 
 82     excel 
 83     'Cells' = h_cell 
 84   EXPORTING 
 85     #1      = row 
 86     #2      = col. 
 87 SET PROPERTY OF h_cell 'Value' = 'Background'. 
 88  
 89 col = col + 1. 
 90 CALL METHOD OF 
 91     excel 
 92     'Cells' = h_cell 
 93   EXPORTING 
 94     #1      = row 
 95     #2      = col. 
 96 SET PROPERTY OF h_cell 'Value' = 'Foreground with white background'. 
 97  
 98 col = col + 1. 
 99 CALL METHOD OF 
100     excel 
101     'Cells' = h_cell 
102   EXPORTING 
103     #1      = row 
104     #2      = col. 
105 SET PROPERTY OF h_cell 'Value' = 'Foreground with black background'. 
106  
107 CALL METHOD OF 
108     excel 
109     'Rows' = h_rows 
110   EXPORTING 
111     #1     = '2:2'. 
112 SET PROPERTY OF h_rows 'WrapText' = 1. 
113  
114 col = 8. 
115 CALL METHOD OF 
116     excel 
117     'Cells' = h_cell 
118   EXPORTING 
119     #1      = row 
120     #2      = col. 
121 SET PROPERTY OF h_cell 'Value' = 'No.'. 
122  
123 col = col + 1. 
124 CALL METHOD OF 
125     excel 
126     'Cells' = h_cell 
127   EXPORTING 
128     #1      = row 
129     #2      = col. 
130 SET PROPERTY OF h_cell 'Value' = 'Background'. 
131  
132 col = col + 1. 
133 CALL METHOD OF 
134     excel 
135     'Cells' = h_cell 
136   EXPORTING 
137     #1      = row 
138     #2      = col. 
139 SET PROPERTY OF h_cell 'Value' = 'Foreground with white background'. 
140 SET PROPERTY OF h_cell 'Bold' = 1. 
141  
142 col = col + 1. 
143 CALL METHOD OF 
144     excel 
145     'Cells' = h_cell 
146   EXPORTING 
147     #1      = row 
148     #2      = col. 
149 SET PROPERTY OF h_cell 'Value' = 'Foreground with black background'. 
150  
151 CALL METHOD OF 
152     excel 
153     'Rows' = h_rows 
154   EXPORTING 
155     #1     = '1:1'. 
156 SET PROPERTY OF h_rows 'WrapText' = 1. 
157 GET PROPERTY OF h_rows 'Font' = h_font. 
158 SET PROPERTY OF h_font 'Bold' = 1. 
159  
160  
161 count = 1. 
162 count_real = count. 
163 row = 2. 
164 col = 2. 
165 DO 56 TIMES. 
166   PERFORM write_num_and_color. 
167 ENDDO. 
168  
169  
170 CALL METHOD OF 
171     excel 
172     'Columns' = h_columns 
173   EXPORTING 
174     #1        = 'B:K'. 
175 GET PROPERTY OF h_columns 'EntireColumn' = h_entirecol. 
176 SET PROPERTY OF h_entirecol 'Autofit' = 1. 
177  
178  
179  
180 *&---------------------------------------------------------------------* 
181 *&      Form  write_num_and_color 
182 *&---------------------------------------------------------------------* 
183 *       text 
184 *----------------------------------------------------------------------* 
185 FORM write_num_and_color. 
186  
187   " write the color number 
188   index = row_max * ( row - 1 ) + col. 
189   CALL METHOD OF 
190       sheet 
191       'Cells' = cells 
192     EXPORTING 
193       #1      = index. 
194   SET PROPERTY OF cells 'Value' = count_real. 
195  
196   " write the color as the background 
197   col = col + 1. 
198   CALL METHOD OF 
199       excel 
200       'Cells' = h_cell 
201     EXPORTING 
202       #1      = row 
203       #2      = col. 
204   GET PROPERTY OF h_cell 'Interior'   = h_int. 
205   SET PROPERTY OF h_int  'ColorIndex' = count_real. 
206  
207   " write the color as the foreground with a white background 
208   col = col + 1. 
209   CALL METHOD OF 
210       excel 
211       'Cells' = h_cell 
212     EXPORTING 
213       #1      = row 
214       #2      = col. 
215   SET PROPERTY OF h_cell 'Value' = 'Sample Text'. 
216   GET PROPERTY OF h_cell 'Font'    = h_f. 
217   SET PROPERTY OF h_f 'ColorIndex' = count_real. 
218  
219   " write the color as the foreground with a black background 
220   col = col + 1. 
221   CALL METHOD OF 
222       excel 
223       'Cells' = h_cell 
224     EXPORTING 
225       #1      = row 
226       #2      = col. 
227   GET PROPERTY OF h_cell 'Interior'   = h_int. 
228   SET PROPERTY OF h_int  'ColorIndex' = 1. 
229   SET PROPERTY OF h_cell 'Value' = 'Sample Text'. 
230   GET PROPERTY OF h_cell 'Font'    = h_f. 
231   SET PROPERTY OF h_f 'ColorIndex' = count_real. 
232  
233   row = row + 1. 
234   col = col - 3. 
235   count = count + 1. 
236   IF count = 29. 
237     count = 1. 
238     row = 2. 
239     col = col + 6. 
240   ENDIF. 
241   count_real = count_real + 1. 
242  
243 ENDFORM.                    "write_num_and_color

View Code

12.SAP中动态内表

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH022 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh022. 
10 TYPE-POOLS:   abap. 
11 FIELD-SYMBOLS:<dyn_table> TYPE STANDARD TABLE, 
12               <dyn_wa>, 
13               <dyn_field>. 
14 DATA: dy_table TYPE REF TO data, 
15       dy_line  TYPE REF TO data, 
16       xfc      TYPE        lvc_s_fcat, 
17       ifc      TYPE        lvc_t_fcat. 
18 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. 
19 PARAMETERS: p_table(30)  TYPE c DEFAULT 'T001'. 
20 SELECTION-SCREEN END OF BLOCK b1. 
21  
22 START-OF-SELECTION. 
23   PERFORM get_structure. 
24   PERFORM create_dynamic_itab. 
25   PERFORM get_data. 
26   PERFORM write_out. 
27 *&--------------------------------------------------------------------* 
28 *&      Form  get_structure 
29 *&--------------------------------------------------------------------* 
30 *       text 
31 *---------------------------------------------------------------------* 
32 FORM get_structure. 
33   DATA: idetails      TYPE        abap_compdescr_tab, 
34         xdetails      TYPE        abap_compdescr. 
35   DATA: ref_table_des TYPE REF TO cl_abap_structdescr. 
36   ref_table_des ?= cl_abap_typedescr=>describe_by_name( p_table ). 
37  
38   idetails[] = ref_table_des->components[]. 
39   LOOP AT idetails INTO xdetails. 
40     CLEAR xfc. 
41     xfc-fieldname = xdetails-name. 
42     xfc-datatype  = xdetails-type_kind. 
43     xfc-inttype   = xdetails-type_kind. 
44     xfc-intlen    = xdetails-length. 
45     xfc-decimals  = xdetails-decimals. 
46     APPEND xfc TO ifc. 
47   ENDLOOP. 
48 ENDFORM.                    "get_structure 
49 *&--------------------------------------------------------------------* 
50 *&      Form  create_dynamic_itab 
51 *&--------------------------------------------------------------------* 
52 *       text 
53 *---------------------------------------------------------------------* 
54 FORM create_dynamic_itab. 
55   CALL METHOD cl_alv_table_create=>create_dynamic_table 
56     EXPORTING 
57       it_fieldcatalog = ifc 
58     IMPORTING 
59       ep_table        = dy_table. 
60   ASSIGN dy_table->* TO <dyn_table>. 
61   CREATE DATA dy_line LIKE LINE OF <dyn_table>. 
62   ASSIGN dy_line->* TO <dyn_wa>. 
63 ENDFORM.                    "create_dynamic_itab 
64 *&--------------------------------------------------------------------* 
65 *&      Form  get_data 
66 *&--------------------------------------------------------------------* 
67 *       text 
68 *---------------------------------------------------------------------* 
69 FORM get_data. 
70   SELECT * INTO TABLE <dyn_table> FROM (p_table). 
71 ENDFORM.                    "get_data 
72 *&--------------------------------------------------------------------* 
73 *&      Form  write_out 
74 *&--------------------------------------------------------------------* 
75 *       text 
76 *---------------------------------------------------------------------* 
77 FORM write_out. 
78   LOOP AT <dyn_table> INTO <dyn_wa>. 
79     DO. 
80       ASSIGN COMPONENT sy-index OF STRUCTURE <dyn_wa> TO <dyn_field>. 
81       IF sy-subrc <> 0. 
82         EXIT. 
83       ENDIF. 
84       IF sy-index = 1. 
85         WRITE:/ <dyn_field>. 
86       ELSE. 
87         WRITE: <dyn_field>. 
88       ENDIF. 
89     ENDDO. 
90   ENDLOOP. 
91 ENDFORM.                    "write_out

View Code

13. SAP中XML生成并解析

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH027 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8  
  9 REPORT zchenh027. 
 10 * 参考江正军的博客:https://www.cnblogs.com/jiangzhengjun/p/4265595.html 
 11 TYPE-POOLS: ixml,abap. 
 12 TYPES: BEGIN OF xml_line, 
 13         data(1024) TYPE x,"这里的长度设置不会影响输出结果,设置成1都可以 
 14        END OF xml_line. 
 15 DATA: l_ixml            TYPE REF TO if_ixml, 
 16       l_streamfactory   TYPE REF TO if_ixml_stream_factory, 
 17       l_ostream         TYPE REF TO if_ixml_ostream, 
 18       l_renderer        TYPE REF TO if_ixml_renderer, 
 19       l_document        TYPE REF TO if_ixml_document. 
 20 DATA: l_element_flights TYPE REF TO if_ixml_element, 
 21       l_element_airline TYPE REF TO if_ixml_element, 
 22       l_element_flight  TYPE REF TO if_ixml_element, 
 23       l_element_dummy   TYPE REF TO if_ixml_element, 
 24       l_value           TYPE string. 
 25 DATA: l_xml_table       TYPE TABLE OF xml_line WITH HEADER LINE, 
 26       l_xml_size        TYPE i, 
 27       l_rc              TYPE i. 
 28 FIELD-SYMBOLS:<dyn_table> TYPE STANDARD TABLE, 
 29               <dyn_wa>, 
 30               <dyn_field>. 
 31 DATA: path TYPE string VALUE 'd:/users/DNAT10218/Desktop/TEST/XML//flights.xml'. " xml文件保存的路径 
 32 DATA: dy_table TYPE REF TO data, 
 33       dy_line  TYPE REF TO data, 
 34       xfc      TYPE        lvc_s_fcat, 
 35       ifc      TYPE        lvc_t_fcat. 
 36  
 37 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. 
 38 PARAMETERS: p_table(30)  TYPE c DEFAULT 'T001'. 
 39 SELECTION-SCREEN END OF BLOCK b1. 
 40  
 41 START-OF-SELECTION. 
 42  
 43   PERFORM frm_get_structure.  "获取表结构 
 44   PERFORM create_dynamic_itab. 
 45   PERFORM frm_get_data.       "取数 
 46  
 47   PERFORM frm_create_xml_file."生成XML数据 
 48   PERFORM frm_save_xml_file.  "保存到本地XML文件 
 49   PERFORM frm_open_xml_file.  "打开本地XML文件 
 50   PERFORM frm_xml_to_str.     "将XML转换成字符串并解析 
 51  
 52  
 53 **************************************************************** 
 54 *&---------------------------------------------------------------------* 
 55 *&      Form  GET_STRUCTURE 
 56 *&---------------------------------------------------------------------* 
 57 *       text 
 58 *----------------------------------------------------------------------* 
 59 *  -->  p1        text 
 60 *  <--  p2        text 
 61 *----------------------------------------------------------------------* 
 62 FORM frm_get_structure . 
 63 * 参考江正军SAP 表的反射 https://www.cnblogs.com/jiangzhengjun/p/4264644.html 
 64   DATA: idetails      TYPE        abap_compdescr_tab, 
 65         xdetails      TYPE        abap_compdescr. 
 66   DATA: ref_table_des TYPE REF TO cl_abap_structdescr. 
 67  
 68 *  ref_table_des ?= cl_abap_typedescr=>describe_by_name( p_table )."结构类型 
 69   ref_table_des ?= cl_abap_tabledescr=>describe_by_name( p_table )."结构类型 
 70  
 71   idetails[] = ref_table_des->components[]."组成结构体的各个字段组件 
 72  
 73   LOOP AT idetails INTO xdetails. 
 74     CLEAR xfc. 
 75     xfc-fieldname = xdetails-name. 
 76     xfc-datatype  = xdetails-type_kind. 
 77     xfc-inttype   = xdetails-type_kind. 
 78     xfc-intlen    = xdetails-length. 
 79     xfc-decimals  = xdetails-decimals. 
 80     APPEND xfc TO ifc. 
 81   ENDLOOP. 
 82 ENDFORM.                    " GET_STRUCTURE 
 83 *&---------------------------------------------------------------------* 
 84 *&      Form  FRM_GET_DATA 
 85 *&---------------------------------------------------------------------* 
 86 *       text 
 87 *----------------------------------------------------------------------* 
 88 *  -->  p1        text 
 89 *  <--  p2        text 
 90 *----------------------------------------------------------------------* 
 91 FORM frm_get_data . 
 92 *  SELECT * INTO TABLE <dyn_table> FROM (p_table). 
 93   SELECT * FROM (p_table) INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 3 ROWS. 
 94 *  SELECT * INTO TABLE <dyn_table> FROM (p_table) UP TO 3 ROWS. 
 95 ENDFORM.                    " FRM_GET_DATA 
 96 *&---------------------------------------------------------------------* 
 97 *&      Form  FRM_CREATE_XML_FILE 
 98 *&---------------------------------------------------------------------* 
 99 *       text 
100 *----------------------------------------------------------------------* 
101 *  -->  p1        text 
102 *  <--  p2        text 
103 *----------------------------------------------------------------------* 
104 FORM frm_create_xml_file . 
105   DATA: lv_idx TYPE string, 
106         name TYPE string, 
107         total TYPE i, 
108         totalcol TYPE string, 
109         totalitem TYPE string, 
110         tablename TYPE string. 
111   tablename = p_table. 
112 *      获取表的总列数 
113   DESCRIBE TABLE ifc LINES total. 
114   totalcol = total. 
115 *  获取内表总条目数 
116   DESCRIBE TABLE <dyn_table> LINES total. 
117   totalitem = total. 
118   LOOP AT <dyn_table> INTO <dyn_wa>. 
119  
120     AT FIRST. 
121 *       创建 ixml factory 
122       l_ixml = cl_ixml=>create( ). 
123 *       创建 the dom object model 
124       l_document = l_ixml->create_document( ). 
125 *       填充 root node with value flow 
126       l_element_flights  = l_document->create_simple_element( 
127                   name = 'TABLESTRUCT' 
128                   parent = l_document ). 
129       DATA:date TYPE string, 
130            time TYPE string. 
131       date = sy-datum. 
132       time = sy-uzeit. 
133 *      设置XML元素的属性 
134       l_rc = l_element_flights->set_attribute( name = 'DATE' value = date ). 
135       l_rc = l_element_flights->set_attribute( name = 'TIME' value = time ). 
136       l_rc = l_element_flights->set_attribute( name = '创建者' value = '陈辉' ). 
137       l_rc = l_element_flights->set_attribute( name = 'TABLENAME' value = tablename ). 
138       l_element_airline  = l_document->create_simple_element( 
139                   name = 'TABLE' 
140                   parent = l_element_flights  ). "parent为父节点 
141  
142       l_rc = l_element_airline->set_attribute( name = 'TOTALITEM' value = totalitem ). 
143     l_rc = l_element_airline->set_attribute( name = 'TOTALCOL' value = totalcol ). 
144  
145     ENDAT. 
146  
147     LOOP AT ifc INTO xfc. 
148  
149       name =  xfc-fieldname. 
150  
151       l_element_flight  = l_document->create_simple_element( 
152                   name =  name 
153                   parent = l_element_airline  ). 
154       l_rc = l_element_flight->set_attribute( name = 'COLNAME' value = name ). 
155  
156       CLEAR:l_value. 
157       CONCATENATE   '<DYN_WA>-' xfc-fieldname INTO l_value. 
158       FIELD-SYMBOLS <fs>. 
159       ASSIGN (l_value) TO <fs>. 
160       l_value = <fs>. 
161       l_element_dummy  = l_document->create_simple_element( 
162                   name = 'VALUE' 
163                   value = l_value 
164                   parent = l_element_flight ). 
165       lv_idx = sy-tabix . 
166       l_rc = l_element_dummy->set_attribute( name = 'INDEX' value = lv_idx ). 
167       UNASSIGN:<fs>. 
168     ENDLOOP. 
169   ENDLOOP. 
170 *   Creating a stream factory 
171   l_streamfactory = l_ixml->create_stream_factory( )."[stri:m] 流 
172 *   Connect internal XML table to stream factory 
173   l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table[] ). 
174 *   Rendering the document 
175   l_renderer = l_ixml->create_renderer( ostream  = l_ostream    " 
176                                         document = l_document )." l_document为根节点 
177   l_rc = l_renderer->render( )."注:执行此句后, l_xml_table内表里才会有数据 
178   l_xml_size = l_ostream->get_num_written_raw( )."取得XML数据大小 
179 ENDFORM.                    " FRM_CREATE_XML_FILE 
180 *&---------------------------------------------------------------------* 
181 *&      Form  FRM_SAVE_XML_FILE 
182 *&---------------------------------------------------------------------* 
183 *       text 
184 *----------------------------------------------------------------------* 
185 *  -->  p1        text 
186 *  <--  p2        text 
187 *----------------------------------------------------------------------* 
188 FORM frm_save_xml_file . 
189 **--将xml数据导出到本地 
190   CALL METHOD cl_gui_frontend_services=>gui_download 
191     EXPORTING 
192       bin_filesize = l_xml_size 
193       filename     = path 
194       filetype     = 'BIN' 
195     CHANGING 
196       data_tab     = l_xml_table[]. 
197 ENDFORM.                    " FRM_SAVE_XML_FILE 
198 *&---------------------------------------------------------------------* 
199 *&      Form  FRM_OPEN_XML_FILE 
200 *&---------------------------------------------------------------------* 
201 *       text 
202 *----------------------------------------------------------------------* 
203 *  -->  p1        text 
204 *  <--  p2        text 
205 *----------------------------------------------------------------------* 
206 FORM frm_open_xml_file . 
207 **--将XML数据导入到内表 
208   DATA xmldata TYPE xstring . 
209   DATA: result_xml TYPE STANDARD TABLE OF smum_xmltb . 
210   DATA: return TYPE STANDARD TABLE OF bapiret2 . 
211   DATA: wa_xml TYPE smum_xmltb. 
212   "如果需要上载XML可以用一下方法 
213   CALL FUNCTION 'GUI_UPLOAD' 
214     EXPORTING 
215       filename   = path 
216       filetype   = 'BIN' 
217     IMPORTING 
218       filelength = l_xml_size 
219     TABLES 
220       data_tab   = l_xml_table. 
221 *  "将二进制内表转换(拼接)成一个二进制串 
222   CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' 
223     EXPORTING 
224       input_length = l_xml_size 
225     IMPORTING 
226       buffer       = xmldata 
227     TABLES 
228       binary_tab   = l_xml_table. 
229   CALL FUNCTION 'SMUM_XML_PARSE'"解析XML 
230     EXPORTING 
231       xml_input = xmldata 
232     TABLES 
233       xml_table = result_xml 
234       return    = return. 
235   WRITE: /, '解析结果如下'. 
236   LOOP AT result_xml INTO wa_xml . 
237     WRITE: / wa_xml-hier,wa_xml-type,wa_xml-cname,wa_xml-cvalue. 
238   ENDLOOP. 
239 ENDFORM.                    " FRM_OPEN_XML_FILE 
240 *&---------------------------------------------------------------------* 
241 *&      Form  FRM_XML_TO_STR 
242 *&---------------------------------------------------------------------* 
243 *       text 
244 *----------------------------------------------------------------------* 
245 *  -->  p1        text 
246 *  <--  p2        text 
247 *----------------------------------------------------------------------* 
248 FORM frm_xml_to_str . 
249 **将XML转换成字符串 
250   DATA: w_string TYPE xstring. 
251   DATA ls_xml TYPE string. 
252   DATA: convin TYPE REF TO cl_abap_conv_in_ce. 
253   DATA: str TYPE string. 
254  
255   CALL FUNCTION 'SDIXML_DOM_TO_XML' 
256     EXPORTING 
257       document      = l_document 
258     IMPORTING 
259       xml_as_string = w_string 
260       size          = l_xml_size 
261     TABLES 
262       xml_as_table  = l_xml_table. 
263  
264   "创建解码对象 
265   convin = cl_abap_conv_in_ce=>create( input = w_string ). 
266  
267   CALL METHOD convin->read 
268     IMPORTING 
269       data = ls_xml. 
270   SKIP 10. 
271   WRITE:/,'********************************************************************'. 
272   WRITE: / ls_xml. 
273 *  BREAK-POINT. " 请查看ls_xml的值  并采用标准解析:http://tool.oschina.net/codeformat/xml/ 
274  
275  
276 * 将一个二进制串分割存储到二进制内表中 
277   CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' 
278     EXPORTING 
279       buffer        = w_string 
280     IMPORTING 
281       output_length = l_xml_size 
282     TABLES 
283       binary_tab    = l_xml_table. 
284 *  将二进制内表转换(拼接)成一个字符串 
285   CALL FUNCTION 'SCMS_BINARY_TO_STRING' 
286     EXPORTING 
287       input_length = l_xml_size 
288     IMPORTING 
289       text_buffer  = ls_xml 
290     TABLES 
291       binary_tab   = l_xml_table. 
292   SKIP 10. 
293   WRITE:/,'********************************************************************'. 
294   WRITE: / ls_xml. 
295 ENDFORM.                    " FRM_XML_TO_STR 
296 *&---------------------------------------------------------------------* 
297 *&      Form  FRM_XML_TO_TABLE 
298 *&---------------------------------------------------------------------* 
299 *       text 
300 *----------------------------------------------------------------------* 
301 *  -->  p1        text 
302 *  <--  p2        text 
303 *----------------------------------------------------------------------* 
304 FORM frm_xml_to_table . 
305  
306 ENDFORM.                    " FRM_XML_TO_TABLE 
307 *&---------------------------------------------------------------------* 
308 *&      Form  CREATE_DYNAMIC_ITAB 
309 *&---------------------------------------------------------------------* 
310 *       text 
311 *----------------------------------------------------------------------* 
312 *  -->  p1        text 
313 *  <--  p2        text 
314 *----------------------------------------------------------------------* 
315 FORM create_dynamic_itab . 
316   CALL METHOD cl_alv_table_create=>create_dynamic_table 
317     EXPORTING 
318       it_fieldcatalog = ifc 
319     IMPORTING 
320       ep_table        = dy_table. 
321   ASSIGN dy_table->* TO <dyn_table>. 
322   CREATE DATA dy_line LIKE LINE OF <dyn_table>. 
323   ASSIGN dy_line->* TO <dyn_wa>. 
324 ENDFORM.                    " CREATE_DYNAMIC_ITAB

View Code

14.ABAP中递归与迭代的使用(斐波拉列契数据与阶乘问题)

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH031 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh031. 
10 DATA:lv_a TYPE i, 
11      lv_b TYPE i, 
12      lv_c TYPE i, 
13      count TYPE i. 
14 lv_a = 1. 
15 lv_b = 1. 
16 * lv_c = lv_a + lv_b. 
17 WRITE:/,'第一次调用递归函数(斐波拉列契数列):'. 
18 PERFORM frm_fbllq USING lv_a lv_b CHANGING lv_c. 
19 WRITE:'最终结果:','A=',lv_a,'B=',lv_b,'C=',lv_c. 
20 SKIP 3. 
21 CLEAR:lv_a,lv_b,lv_c,count. 
22 lv_a = 5. 
23 * lv_c = lv_a * (lv_a - 1) * ...* 1. 
24 WRITE:/,'第二次调用递归函数(阶乘):'. 
25 PERFORM frm_jc USING lv_a CHANGING lv_c. 
26 WRITE:'最终结果:','A=',lv_a,'C=',lv_c. 
27 *&---------------------------------------------------------------------* 
28 *&      Form  frm_fbllq 
29 *&---------------------------------------------------------------------* 
30 *       text 
31 *----------------------------------------------------------------------* 
32 *  -->  p1        text 
33 *  <--  p2        text 
34 *----------------------------------------------------------------------* 
35 FORM frm_fbllq USING p_a p_b CHANGING p_c. 
36   count = count + 1. 
37   WRITE:/,count,p_a,p_b,p_c. 
38   IF p_a >= 10. 
39     EXIT. 
40   ELSE. 
41     p_c = p_a + p_b. 
42     p_a = p_b. 
43     p_b = p_c. 
44     PERFORM frm_fbllq USING p_a p_b CHANGING p_c. 
45   ENDIF. 
46 ENDFORM.                    " frm_fbllq 
47 *&---------------------------------------------------------------------* 
48 *&      Form  FRM_JC 
49 *&---------------------------------------------------------------------* 
50 *       text 
51 *----------------------------------------------------------------------* 
52 *  -->  p1        text 
53 *  <--  p2        text 
54 *----------------------------------------------------------------------* 
55 FORM frm_jc USING p_a  CHANGING p_c. 
56   count = count + 1. 
57   WRITE:/,count,p_a,p_c. 
58   IF p_c IS INITIAL. 
59     p_c = 1. 
60   ENDIF. 
61   IF p_a = 1 OR p_a <= 0. 
62     EXIT. 
63   ELSE. 
64     p_c =  p_a * p_c. 
65     p_a = p_a - 1. 
66     PERFORM frm_jc USING p_a CHANGING p_c. 
67   ENDIF. 
68 ENDFORM.                    " FRM_JC

View Code

15.ABAP中测试三位数的水仙花数

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH032 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8 *打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数, 
 9 *其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ", 
10 *因为153=1的三次方+5的三次方+3的三次方 
11 REPORT zchenh032. 
12 DATA:BEGIN OF gt_sxhs OCCURS 0, 
13       sxhs TYPE c LENGTH 3, 
14       col1 TYPE i , 
15       col2 TYPE i , 
16       col3 TYPE i , 
17       END OF gt_sxhs. 
18  
19 PERFORM frm_get_data. 
20 *&---------------------------------------------------------------------* 
21 *&      Form  FRM_GET_DATA 
22 *&---------------------------------------------------------------------* 
23 *       text 
24 *----------------------------------------------------------------------* 
25 *  -->  p1        text 
26 *  <--  p2        text 
27 *----------------------------------------------------------------------* 
28 FORM frm_get_data . 
29   DATA:lv_sxhs TYPE c LENGTH 3. 
30   DO 999 TIMES. 
31     gt_sxhs-sxhs = sy-index. 
32     APPEND gt_sxhs. 
33   ENDDO. 
34   LOOP AT gt_sxhs WHERE sxhs BETWEEN 100 AND 999. 
35     gt_sxhs-col1 = gt_sxhs-sxhs+0(1). " 百位 
36     gt_sxhs-col2 = gt_sxhs-sxhs+1(1). " 十位 
37     gt_sxhs-col3 = gt_sxhs-sxhs+2(1). " 个位 
38     lv_sxhs = gt_sxhs-col1 * gt_sxhs-col1 * gt_sxhs-col1 + 
39              gt_sxhs-col2 * gt_sxhs-col2 * gt_sxhs-col2 + 
40              gt_sxhs-col3 * gt_sxhs-col3 * gt_sxhs-col3. 
41     IF lv_sxhs EQ gt_sxhs-sxhs. 
42       WRITE:/,'水仙花数',gt_sxhs-sxhs,'=',gt_sxhs-col1,'的三次方 乘以',gt_sxhs-col2,'的三次方 乘以',gt_sxhs-col3,'的三次方'. 
43     ENDIF. 
44   ENDLOOP. 
45  
46 ENDFORM.                    " FRM_GET_DATA

View Code

16.ABAP前台进度条

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH028 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh028. 
10  
11 DATA:count TYPE string.  " 内表总数据条数 
12 DATA:msg   TYPE string.    " 显示的消息 
13 DATA:index TYPE string.  " 当前处理的索引 
14  
15 DATA:BEGIN OF gt_tab  OCCURS 0, 
16       col1 TYPE string, 
17      END OF gt_tab . 
18  
19  
20 " 插入100条空数据 
21 DO 10000 TIMES. 
22   APPEND gt_tab. 
23 ENDDO. 
24 DESCRIBE TABLE gt_tab LINES count. 
25  
26  
27 *  进度条显示 
28 LOOP AT gt_tab. 
29   CLEAR:msg,index. 
30   index = sy-tabix. 
31   CONCATENATE '正在处理。。。'  index '/' count INTO msg. " 拼接消息 
32   index = index / count * 100.  " 时钟显示的百分比 
33  
34   CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' 
35     EXPORTING 
36       percentage = index " 处理的百分比 
37       text       = msg. 
38 ENDLOOP.

View Code

17.ABAP 分组测试

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH029 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh029. 
10  
11 TYPES:BEGIN OF gs_tab, 
12     col1(10), 
13     col2(10), 
14     col3(10), 
15     col4(10), 
16       END OF gs_tab. 
17 DATA: gt_tab TYPE gs_tab OCCURS 0 WITH HEADER LINE. 
18 DATA: gt_tcd TYPE gs_tab OCCURS 0 WITH HEADER LINE. 
19 DATA: gs_ab TYPE gs_tab. 
20 gs_ab-col1 = '1000'.gs_ab-col2 = 'aaa1'.gs_ab-col3 = 1. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
21 gs_ab-col1 = '1000'.gs_ab-col2 = 'aaa2'.gs_ab-col3 = 2. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
22 gs_ab-col1 = '2000'.gs_ab-col2 = 'aaa2'.gs_ab-col3 = 3. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
23 gs_ab-col1 = '2000'.gs_ab-col2 = 'aaa2'.gs_ab-col3 = 4. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
24 gs_ab-col1 = '2000'.gs_ab-col2 = 'aaa3'.gs_ab-col3 = 5. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
25 gs_ab-col1 = '3000'.gs_ab-col2 = 'aaa3'.gs_ab-col3 = 6. gs_ab-col4 = 'A'. APPEND gs_ab TO gt_tab. 
26  
27 LOOP AT gt_tab. 
28 *  AT END OF col4. 
29   AT NEW col4. 
30     APPEND gt_tab TO gt_tcd. 
31   ENDAT. 
32 ENDLOOP. 
33  
34 LOOP AT  gt_tcd. 
35   WRITE:gt_tcd-col1,'  ',gt_tcd-col2,'  ',gt_tcd-col3,'  ',gt_tcd-col4,'  ',/. 
36 ENDLOOP.

View Code

18. 修改已经释放的请求

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH026 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh026. 
10 TABLES: e070. 
11 PARAMETERS : p_trkorr LIKE e070-trkorr OBLIGATORY. 
12  
13 UPDATE e070 SET trstatus = 'D' "将状态还原成‘可修改’ 
14 WHERE trkorr = p_trkorr. "输入的需要处理的TR 
15  
16 IF sy-subrc = 0. 
17   COMMIT WORK. 
18   MESSAGE 'OK' TYPE 'S'. 
19 ELSE. 
20   ROLLBACK WORK. 
21   MESSAGE 'ERROR!' TYPE 'E'. 
22 ENDIF.

View Code

19.质数判断

  1 *&---------------------------------------------------------------------* 
  2 *& Report  ZCHENH054 
  3 *& 
  4 *&---------------------------------------------------------------------* 
  5 *& 
  6 *& 
  7 *&---------------------------------------------------------------------* 
  8 *参考文章:https://blog.csdn.net/codeswarrior/article/details/78053754 
  9 REPORT zchenh054. 
 10 DATA:start_time   TYPE timestamp, 
 11      end_time     TYPE timestamp, 
 12      produce_time TYPE timestamp, 
 13      start TYPE brutto. 
 14  
 15 CONSTANTS:lv_max TYPE i VALUE 10000. 
 16  
 17 DATA:BEGIN OF gt_data OCCURS 0, 
 18   index TYPE sy-index, 
 19   num TYPE string, 
 20   status, 
 21   msg(20), 
 22 END OF gt_data. 
 23  
 24 START-OF-SELECTION. 
 25   GET TIME STAMP FIELD start_time. 
 26  
 27 *gt_data-num = 97. 
 28 *PERFORM frm_check_data USING gt_data-num 
 29 *                      CHANGING gt_data-status 
 30 *                               gt_data-msg. 
 31 *WRITE:gt_data-msg,/. 
 32  
 33   DO lv_max TIMES. 
 34     CLEAR:gt_data. 
 35     gt_data-num = sy-index. 
 36     PERFORM frm_check_data USING gt_data-num 
 37                         CHANGING gt_data-status 
 38                                  gt_data-msg. 
 39     IF gt_data-status EQ 'S'. 
 40       APPEND gt_data. 
 41     ENDIF. 
 42   ENDDO. 
 43  
 44   LOOP AT gt_data. 
 45     CONDENSE:gt_data-msg. 
 46     WRITE:sy-tabix,gt_data-msg,/. 
 47   ENDLOOP. 
 48  
 49 END-OF-SELECTION. 
 50   GET TIME STAMP FIELD end_time. 
 51   produce_time = end_time - start_time. 
 52   WRITE:'持续运行时间:',produce_time,''. 
 53  
 54  
 55 *&---------------------------------------------------------------------* 
 56 *&      Form  FRM_CHECK_DATA 
 57 *&---------------------------------------------------------------------* 
 58 *       判断一个数是否是质数 
 59 *----------------------------------------------------------------------* 
 60 *  -->  p1        text 
 61 *  <--  p2        text 
 62 *----------------------------------------------------------------------* 
 63 FORM frm_check_data USING p_num    TYPE string 
 64                  CHANGING p_status TYPE char1 
 65                           p_msg    TYPE char20. 
 66   DATA: b TYPE string VALUE 0, 
 67         c TYPE string VALUE 0, 
 68         d TYPE string VALUE 0, 
 69         e TYPE string VALUE 0. 
 70  
 71   CONDENSE:p_msg. 
 72   p_msg = p_num. 
 73  
 74 *1. 特殊质数判断 
 75   IF p_num EQ 2 OR p_num EQ 3. 
 76     p_status = 'S'. 
 77     CONCATENATE p_msg '是质数' INTO p_msg. 
 78     RETURN. 
 79   ENDIF. 
 80  
 81 *2.不在6的倍数两侧的一定不是质数 
 82   b = p_num MOD 6. 
 83   IF b NE 1 AND b NE 5. 
 84     p_status = 'E'. 
 85     CONCATENATE p_msg '不是质数' INTO p_msg. 
 86     RETURN. 
 87   ENDIF. 
 88  
 89 * 3.在6的倍数两侧的也可能不是质数 
 90   b = 5. 
 91   c = sqrt( p_num ). 
 92   WHILE  b LE c. 
 93     d = p_num MOD b. 
 94     e = p_num MOD ( b + 2 ). 
 95     IF d EQ 0 OR d EQ 0. 
 96       p_status = 'E'. 
 97       CONCATENATE p_msg '不是质数' INTO p_msg. 
 98       RETURN. 
 99     ENDIF. 
100     b = b + 6. 
101   ENDWHILE. 
102  
103 * 4.排除所有,剩余的是质数 
104   p_status = 'S'. 
105   CONCATENATE p_msg '是质数' INTO p_msg. 
106 ENDFORM.                    " FRM_CHECK_DATA

View Code

20.ABAP调用播放器播放文件

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH055 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh055. 
10 *http://blog.sina.com.cn/s/blog_56d71d930101dmeu.html 
11 DATA:ls_parameter TYPE string, 
12      lv_file TYPE string VALUE 'D:/users/DNAT10218/Desktop/三国演义.mp4', 
13      lv_flag. 
14 DATA:times TYPE string VALUE '1', " 播放次数 
15       timer TYPE string VALUE '15000'. " 超时时常 
16  
17 *CONCATENATE 'vbscript createobject("sapi.spvoice").speak("' in_hinttxt '")(windows.close)' INTO ls_parameter. 
18  
19 *先判断文件是否存在 
20 CALL METHOD cl_gui_frontend_services=>file_exist 
21   EXPORTING 
22     file   = lv_file 
23   RECEIVING 
24     result = lv_flag. 
25 IF lv_flag EQ 'X'. 
26  
27 *  CONCATENATE 'VBSCRIPT:EXECUTE("DOCUMENT.WRITE ""<BGSOUND SRC=' lv_file '","","/")+"''' 
28 *  'loop=' ''''times'''>"":window.setTimeout ""window.close()"",' timer '")' INTO ls_parameter. 
29  
30   CALL METHOD cl_gui_frontend_services=>execute 
31     EXPORTING 
32       document               = lv_file 
33 *      application            = 'MSHTA' 
34 *     parameter              = ls_parameter 
35 *     default_directory      = 
36 *     maximized              = 
37       minimized              = 'X' 
38 *     synchronous            = 
39      operation              = 'OPEN' 
40     EXCEPTIONS 
41       cntl_error             = 1 
42       error_no_gui           = 2 
43       bad_parameter          = 3 
44       file_not_found         = 4 
45       path_not_found         = 5 
46       file_extension_unknown = 6 
47       error_execute_failed   = 7 
48       synchronous_failed     = 8 
49       not_supported_by_gui   = 9 
50       OTHERS                 = 10. 
51 ENDIF.

View Code

21.ABAP 迷你计算器

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH056 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh056. 
10 DATA: x(15) TYPE c. 
11 CALL FUNCTION 'FITRV_CALCULATOR' 
12   EXPORTING 
13     input_value          = '0' 
14     currency             = 'RMB' 
15     start_column         = '10' 
16     start_row            = '10' 
17   IMPORTING 
18     output_value         = x 
19   EXCEPTIONS 
20     invalid_input        = 1 
21     calculation_canceled = 2 
22     OTHERS               = 3. 
23  
24 WRITE:X.

View Code

22.ABAP 保存本地程序

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH057 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh057. 
10 TABLES tadir. 
11 DATA: BEGIN OF it_table OCCURS 0, 
12         obj_name LIKE tadir-obj_name, 
13       END OF it_table. 
14 DATA report_table TYPE TABLE OF string WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 500. 
15 DATA: file TYPE string, 
16       path TYPE string. 
17  
18 SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME." TITLE text-001 . 
19 PARAMETER: obj_name LIKE tadir-obj_name DEFAULT 'ZCHENH05'. 
20 SELECT-OPTIONS: object FOR tadir-object DEFAULT 'PROG', 
21                 author FOR tadir-author, 
22                 devclass FOR tadir-devclass. 
23 SELECTION-SCREEN END OF BLOCK blk01 . 
24  
25 START-OF-SELECTION. 
26   PERFORM frm_save_file. 
27 *&---------------------------------------------------------------------* 
28 *&      Form  FRM_SAVE_FILE 
29 *&---------------------------------------------------------------------* 
30 *       text 
31 *----------------------------------------------------------------------* 
32 *  -->  p1        text 
33 *  <--  p2        text 
34 *----------------------------------------------------------------------* 
35 FORM frm_save_file . 
36  
37   CONCATENATE obj_name '%' INTO obj_name. 
38   SELECT obj_name 
39   INTO TABLE it_table FROM tadir 
40   WHERE obj_name LIKE obj_name 
41   AND object IN object 
42   AND author IN author 
43   AND devclass IN devclass. 
44  
45   path = 'D:/ABAP/'. 
46   CONDENSE path NO-GAPS. 
47  
48   LOOP AT it_table. 
49  
50     READ REPORT it_table-obj_name INTO report_table. 
51  
52     CLEAR file. 
53     CONCATENATE path it_table-obj_name '.TXT' INTO file. 
54  
55     CALL FUNCTION 'GUI_DOWNLOAD' 
56       EXPORTING 
57         filename = file 
58         filetype = 'ASC' 
59       TABLES 
60         data_tab = report_table. 
61  
62   ENDLOOP. 
63 ENDFORM.                    " FRM_SAVE_FILE

View Code

23.ABAP 动态拼接Where 语句

 1 *&---------------------------------------------------------------------* 
 2 *& Report  ZCHENH043 
 3 *& 
 4 *&---------------------------------------------------------------------* 
 5 *& 
 6 *& 
 7 *&---------------------------------------------------------------------* 
 8  
 9 REPORT zchenh043. 
10 TABLES: zchenh001. 
11  
12 DATA where_tab(80) OCCURS 10 WITH HEADER LINE. 
13 DATA : count_rec TYPE i. 
14  
15 SELECT-OPTIONS: s_zhigh FOR zchenh001-zhigh NO INTERVALS OBLIGATORY DEFAULT '185'. 
16  
17 MOVE 'zhigh in s_zhigh' TO where_tab. 
18 APPEND where_tab. 
19  
20 CONCATENATE 'and zname like' '''%陈辉%''' 
21              INTO where_tab SEPARATED BY space. 
22 APPEND where_tab. 
23 CLEAR where_tab. 
24  
25 SELECT COUNT( * ) INTO count_rec 
26 FROM zchenh001 
27 WHERE (where_tab). 
28  
29 WRITE:'共查询到',count_rec.

View Code

 

 

      

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18032.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论