参考了https://www.cnblogs.com/purehunter/archive/2011/07/22/2113879.html
遇到的需求是有6个内表需要导出数据到一个excel里,分成6个sheet,按月导出,后台处理,针对楼上老哥的代码做了些小改动,hana的话应该还可以用abap2xlsx来做。
1 REPORT y_xb_test3. 2 3 DATA:gt_sflight TYPE TABLE OF sflight. 4 DATA:gt_t001l TYPE TABLE OF t001l. 5 6 START-OF-SELECTION. 7 8 DATA: l_user(30) TYPE c VALUE ‘admin‘, "用户名 9 l_pwd(30) TYPE c VALUE ‘1234test‘, "密码 10 l_host(64) TYPE c VALUE ‘192.1.XX.XX‘, "FTP服务器地址 11 l_path(64) TYPE c VALUE ‘/‘, "路径 12 l_rc(1), 13 l_dest TYPE rfcdes-rfcdest VALUE ‘SAPFTP‘. "前端:sapftp 后台运行:sapftpa. 14 15 DATA: l_handle TYPE i, 16 l_command(255) TYPE c, 17 l_result TYPE TABLE OF txmisporow, 18 l_filename TYPE rlgrap-filename, 19 l_pwdlength TYPE i, 20 l_length TYPE i, 21 l_ftppwd(255) TYPE c, 22 l_key TYPE i VALUE 26101957, 23 l_encoding TYPE abap_encoding VALUE ‘8400‘. 24 25 DATA: l_binary_tab LIKE TABLE OF solix. 26 27 SELECT * 28 FROM sflight 29 INTO TABLE gt_sflight. 30 31 SELECT * 32 FROM t001l 33 INTO TABLE gt_t001l 34 UP TO 10 ROWS. 35 36 l_pwdlength = strlen( l_pwd ). 37 38 39 IF sy-batch = ‘X‘. 40 l_dest = ‘SAPFTPA‘. 41 ELSE. 42 l_dest = ‘SAPFTP‘. 43 ENDIF. 44 *****************开始FTP传输数据********** 45 CALL FUNCTION ‘HTTP_SCRAMBLE‘ "密码加密 46 EXPORTING 47 source = l_pwd 48 sourcelen = l_pwdlength 49 key = l_key 50 IMPORTING 51 destination = l_ftppwd. 52 53 "创建FTP连接 54 55 CALL FUNCTION ‘FTP_CONNECT‘ 56 57 EXPORTING 58 user = l_user 59 password = l_ftppwd 60 host = l_host 61 rfc_destination = l_dest 62 IMPORTING 63 handle = l_handle 64 EXCEPTIONS 65 not_connected = 1 66 OTHERS = 2. 67 68 IF sy-subrc <> 0. 69 MESSAGE ‘FTP path error‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘. 70 LEAVE LIST-PROCESSING. 71 ENDIF. 72 73 l_rc = COND #( WHEN l_path+(1) = ‘/‘ THEN ‘ ‘ ELSE ‘ /‘ ). 74 CONCATENATE ‘cd‘ l_path INTO l_command SEPARATED BY l_rc. "拼接FTP命令,这里是打开文件夹地址 75 76 CALL FUNCTION ‘FTP_COMMAND‘ "执行FTP命令 77 EXPORTING 78 handle = l_handle 79 command = l_command 80 TABLES 81 data = l_result 82 EXCEPTIONS 83 tcpip_error = 1 84 command_error = 2 85 data_error = 3 86 OTHERS = 4. 87 88 IF sy-subrc <> 0. 89 * PERFORM ftp_disconnect USING l_handle l_dest. 90 MESSAGE ‘FTP path error‘ TYPE ‘E‘. 91 ENDIF. 92 93 CONCATENATE ‘TEST‘ sy-datum ‘.xls‘ INTO l_filename. 94 95 96 DATA(l_ixml) = cl_ixml=>create( ). 97 * Creating the dom object model 创建dom对象模型 98 DATA(l_document) = l_ixml->create_document( ). 99 * Create root node ‘Workbook‘ 创建根节点 100 DATA(l_element_root) = l_document->create_simple_element( 101 name = ‘Workbook‘ 102 * uri = ‘urn:schemas-microsoft-com:office:spreadsheet‘ 103 parent = l_document ). 104 l_element_root->set_attribute( 105 name = ‘xmlns‘ 106 value = ‘urn:schemas-microsoft-com:office:spreadsheet‘ ). 107 DATA(ns_attribute) = l_document->create_namespace_decl( 108 name = ‘ss‘ 109 prefix = ‘xmlns‘ 110 uri = ‘urn:schemas-microsoft-com:office:spreadsheet‘ ). 111 l_element_root->set_attribute_node( ns_attribute ). 112 ns_attribute = l_document->create_namespace_decl( 113 name = ‘x‘ 114 prefix = ‘xmlns‘ 115 uri = ‘urn:schemas-microsoft-com:office:excel‘ ). 116 l_element_root->set_attribute_node( ns_attribute ). 117 * Create node for document properties. 为文档属性创建节点 118 DATA(r_element_properties) = l_document->create_simple_element( 119 name = ‘DocumentProperties‘ 120 parent = l_element_root ). 121 DATA:l_value TYPE string. 122 DATA:lv_sname TYPE string. 123 l_value = sy-uname. 124 l_document->create_simple_element( 125 name = ‘Author‘ 126 value = l_value 127 parent = r_element_properties ). 128 129 FIELD-SYMBOLS: 130 <lft_dyntab> TYPE ANY TABLE. 131 "2.创建excel表名和sheet数据 132 133 ASSIGN gt_sflight TO <lft_dyntab>. 134 lv_sname = ‘航班‘. 135 136 PERFORM frm_create_sheetdata USING <lft_dyntab> 137 l_document 138 l_element_root 139 lv_sname. 140 141 lv_sname = ‘仓储地点‘. 142 ASSIGN gt_t001l TO <lft_dyntab>. 143 PERFORM frm_create_sheetdata USING <lft_dyntab> 144 l_document 145 l_element_root 146 lv_sname. 147 148 "xml生成输出 149 150 DATA(l_streamfactory) = l_ixml->create_stream_factory( ). 151 * Connect internal XML table to stream factory 152 DATA(l_ostream) = l_streamfactory->create_ostream_itable( table = l_binary_tab ). 153 * rendering the document 154 DATA(l_renderer) = l_ixml->create_renderer( ostream = l_ostream 155 document = l_document ). 156 l_rc = l_renderer->render( ). 157 * saving the xml document 158 l_length = l_ostream->get_num_written_raw( ). 159 160 "刚刚已经用FTP命令打开的文件夹,现在可以直接把二进制文件传至服务器了。 161 CALL FUNCTION ‘FTP_R3_TO_SERVER‘ 162 EXPORTING 163 handle = l_handle 164 fname = l_filename 165 blob_length = l_length 166 character_mode = ‘ ‘ 167 TABLES 168 blob = l_binary_tab 169 EXCEPTIONS 170 tcpip_error = 1 171 command_error = 2 172 data_error = 3 173 OTHERS = 4. 174 175 CASE sy-subrc. 176 WHEN 0. 177 MESSAGE ‘Upload FTP Success‘ TYPE ‘S‘. 178 WHEN 1. 179 MESSAGE ‘Upload FTP Failure (Tcpip_error)‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘.EXIT. 180 WHEN 2. 181 MESSAGE ‘Upload FTP Failure (Command_error)‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘.EXIT. 182 WHEN 3. 183 MESSAGE ‘Upload FTP Failure (Data_error)‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘.EXIT. 184 WHEN OTHERS. 185 MESSAGE ‘Upload FTP Failure (Other)‘ TYPE ‘S‘ DISPLAY LIKE ‘E‘.EXIT. 186 ENDCASE. 187 188 "关闭FTP 连接 189 CALL FUNCTION ‘FTP_DISCONNECT‘ 190 EXPORTING 191 handle = l_handle. 192 193 CALL FUNCTION ‘RFC_CONNECTION_CLOSE‘ 194 EXPORTING 195 destination = l_dest 196 EXCEPTIONS 197 destination_not_open = 1 198 OTHERS = 2. 199 *&---------------------------------------------------------------------* 200 *& Form FRM_CREATE_SHEETDATA 201 *&---------------------------------------------------------------------* 202 * text 203 *----------------------------------------------------------------------* 204 * --> p1 text 205 * <-- p2 text 206 *----------------------------------------------------------------------* 207 FORM frm_create_sheetdata USING lt_dyntab TYPE ANY TABLE 208 l_document TYPE REF TO if_ixml_document 209 l_element_root TYPE REF TO if_ixml_element 210 lv_sname TYPE string. 211 DATA:l_value TYPE string. 212 DATA(r_worksheet) = l_document->create_simple_element( 213 name = ‘Worksheet ‘ 214 parent = l_element_root ). 215 r_worksheet->set_attribute_ns( 216 name = ‘Name‘ 217 prefix = ‘ss‘ 218 value = lv_sname ). 219 * table 220 * <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1"> 221 DATA(r_table) = l_document->create_simple_element( 222 name = ‘Table‘ 223 parent = r_worksheet ). 224 225 "3.创建column 226 227 "因为xml格式的excel为先column,然后再每个row下存在cell,所以先循环输出column。 228 229 DATA(r_column) = l_document->create_simple_element( 230 name = ‘Column‘ 231 parent = r_table ). 232 233 "4.row和内容类型 234 DATA:l_row TYPE REF TO if_ixml_element, 235 l_cell TYPE REF TO if_ixml_element, 236 l_data TYPE REF TO if_ixml_element. 237 238 "将内表填写到excel表格内 239 DATA:i_index TYPE sy-index. 240 DATA:l_index TYPE string. 241 DATA:l_text TYPE char10. 242 DATA:l_type TYPE string. 243 DATA:desc_struc TYPE REF TO cl_abap_structdescr. 244 FIELD-SYMBOLS:<lv_field> TYPE any. 245 LOOP AT lt_dyntab ASSIGNING FIELD-SYMBOL(<data_line>). 246 desc_struc ?= cl_abap_structdescr=>describe_by_data( <data_line> )."因为内表的每列数据要填写到excel的每行内。所以使用该方法将内表转换。 247 DATA(lt_fields) = desc_struc->get_ddic_field_list( ). 248 249 AT FIRST. 250 l_row = l_document->create_simple_element( 251 name = ‘Row‘ 252 parent = r_table ). 253 LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<data_fields>). 254 * ASSIGN COMPONENT <data_fields>-fieldname OF STRUCTURE <data_line> TO <lv_field>."动态绑定表格内容。 255 CHECK sy-subrc IS INITIAL. 256 l_cell = l_document->create_simple_element( 257 name = ‘Cell‘ 258 parent = l_row ). 259 l_value = <data_fields>-scrtext_l. 260 l_data = l_document->create_simple_element( 261 name = ‘Data‘ 262 value = l_value 263 parent = l_cell ). 264 l_data->set_attribute_ns( 265 name = ‘Type‘ 266 prefix = ‘ss‘ 267 value = ‘String‘ ). 268 ENDLOOP. 269 ENDAT. 270 l_row = l_document->create_simple_element( 271 name = ‘Row‘ 272 parent = r_table ). 273 274 LOOP AT lt_fields ASSIGNING <data_fields>. 275 ASSIGN COMPONENT <data_fields>-fieldname OF STRUCTURE <data_line> TO <lv_field>."动态绑定表格内容。 276 CHECK sy-subrc IS INITIAL. 277 l_cell = l_document->create_simple_element( 278 name = ‘Cell‘ 279 parent = l_row ). 280 l_value = <lv_field>. 281 l_type = <data_fields>-inttype. "fieldname, scrtext_m etc. 282 CASE l_type. 283 WHEN ‘I‘ OR ‘P‘ OR ‘F‘ OR ‘N‘. 284 l_type = ‘String‘. 285 l_value = <lv_field>. 286 CONDENSE l_value NO-GAPS. 287 WHEN ‘D‘ OR ‘T‘. 288 l_type = ‘String‘. 289 WRITE <lv_field> TO l_text. 290 l_value = l_text. 291 WHEN OTHERS. 292 * l_value = <field>. "Without conversion exit 293 WRITE <lv_field> TO l_text. 294 SHIFT l_text LEFT DELETING LEADING space. 295 l_value = l_text. 296 l_type = ‘String‘. 297 ENDCASE. 298 l_data = l_document->create_simple_element( 299 name = ‘Data‘ 300 value = l_value 301 parent = l_cell ). 302 l_data->set_attribute_ns( 303 name = ‘Type‘ 304 prefix = ‘ss‘ 305 value = l_type ). 306 ENDLOOP. 307 ENDLOOP. 308 ENDFORM.