1, OLE介绍
下面介绍用OLE方法上载Excel文件到SAP系统。OLE是什么?网上baidu了一段定义:
ABAP支持基于开放对象数据接口自动连接技术,提供了调用功能的桌面应用程序被整合到ABAP系统作为OLE2的自动服务器(如:excel, word)。 当一个ABAP程序调用OLE的时候,SAPGUI充当OLE的客户端,桌面应用程序为OLE(如Excel,word等)的服务器端。
说实话没太看懂,其实说白了通过OLE方法我们可以模拟绝大多数的Excel操作,就像我们在windows或者苹果操作系统下操作excel,包括拷贝,粘贴,剪贴,宏应用等等都能实现。OLE功能很强大,但是也有致命的弱点就是一个字----慢,天下武功唯快不破呀。OLE上载excel文件时,可以一个一个单元格的上载也可以一块一块的上载,显而易见块上载的方式是相对较快的。下面就介绍一下如何块上载excel文件。
2, 实例代码
例子:
上载一个简单的excel,有a,b,c三列数据,
代码逻辑:
1,程序先将excel中的内容导入到剪贴板中,然后在从剪贴板中通过cl_gui_frontend_services=>clipboard_import copy到内表git_excel_header中,
2,将每一行的内容通过下面代码分隔到内表lit_cell_value中,
1 2 3 |
SPLIT lwa_excel_data-line AT cl_abap_char_utilities=>horizontal_tab INTO TABLE lit_cell_value. |
3,最后列行转换,保存到内表git_order_header中,
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
REPORT z_ole_upload_excel. TYPE-POOLS:ole2. "Data for Excel Application DATA: go_application TYPE ole2_object, "OLE Application go_workbooks TYPE ole2_object, "Workbooks go_workbook TYPE ole2_object, "Workbook go_worksheet TYPE ole2_object, "Excel Sheet go_range TYPE ole2_object. "Range DATA:g_path TYPE string. TYPES:BEGIN OF ty_order_header, col_a(20) TYPE c, col_b(20) TYPE c, col_c(20) TYPE c, END OF ty_order_header. DATA:git_order_header TYPE STANDARD TABLE OF ty_order_header. TYPES: BEGIN OF ty_excel, line(4096) TYPE c, END OF ty_excel. TYPES: ty_it_excel_table TYPE STANDARD TABLE OF ty_excel. "Data for file content of header DATA: git_excel_header TYPE ty_it_excel_table. "File Path for Upload PARAMETERS p_file TYPE rlgrap-filename OBLIGATORY. *&---------------------------------------------------------------------* *& AT SELECTION-SCREEN *&---------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM frm_browser_file CHANGING p_file. *&---------------------------------------------------------------------* *& START-OF-SELECTION. *&---------------------------------------------------------------------* START-OF-SELECTION. * 打开文件 PERFORM frm_open_file. * 读取文件 PERFORM frm_read_file. * 解析文件 PERFORM frm_parse_file. * 关闭文件 PERFORM frm_close_file. *&---------------------------------------------------------------------* *& Include ZGA_NONIPC_SO_UPLOAD_F01 *&---------------------------------------------------------------------* FORM frm_browser_file CHANGING c_path TYPE rlgrap-filename. DATA: l_file_path TYPE string. "File Path DATA: l_window_title TYPE string. "Browser Directory window title l_window_title = text-003. CLEAR: l_file_path. CALL FUNCTION 'WS_FILENAME_GET' EXPORTING def_filename = '' def_path = 'C:\tmp' mask = ',*.XLS.' mode = 'O' title = l_window_title IMPORTING filename = l_file_path EXCEPTIONS inv_winsys = 1 no_batch = 2 selection_cancel = 3 selection_error = 4 OTHERS = 5. IF l_file_path IS NOT INITIAL. c_path = l_file_path. ENDIF. ENDFORM. " FRM_BROWSER_FILE *&---------------------------------------------------------------------* *& Form FRM_OPEN_FILE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_open_file . "This part open the Excel application, and then open the excel file "Open the Application first CREATE OBJECT go_application 'Excel.Application'. SET PROPERTY OF go_application 'DisplayAlerts' = 'False'. SET PROPERTY OF go_application 'Visible' = 0. "1: Visiable, 0: Invisible CALL METHOD OF go_application 'WORKBOOKS' = go_workbooks. g_path = p_file. "Then open upload file with the application CALL METHOD OF go_workbooks 'OPEN' = go_workbook EXPORTING #1 = g_path. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_READ_FILE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_read_file . "Go the first sheet of the excel file, which is for order headers CALL METHOD OF go_application 'WORKSHEETS' = go_worksheet EXPORTING #index = 1. "第一个sheet页是1,以此类推 "Get all cells selected CALL METHOD OF go_worksheet 'Cells' = go_range. "Now call the selected cells to clipboard CALL METHOD OF go_range 'COPY'. "Since the data has been copied, now read from clipboard. REFRESH: git_excel_header. CALL METHOD cl_gui_frontend_services=>clipboard_import IMPORTING data = git_excel_header EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc <> 0. MESSAGE 'read file fail' TYPE 'S' DISPLAY LIKE 'E'. STOP. ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CLOSE_FILE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_close_file . "This method close the excel file, and quit the excel application CALL METHOD OF go_workbooks 'CLOSE'. FREE OBJECT go_workbooks. CALL METHOD OF go_application 'QUIT'. FREE OBJECT go_application. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_PARSE_FILE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_parse_file . DATA: lwa_excel_data TYPE ty_excel. "Excel Data Work Area DATA: lit_cell_value TYPE STANDARD TABLE OF string. "Excel Line Cells DATA: lwa_cell_value TYPE string. "Excel Value DATA: lwa_order_header TYPE ty_order_header. "Order Header DATA: lo_error TYPE REF TO cx_root, l_message TYPE string. LOOP AT git_excel_header INTO lwa_excel_data "从第一行读取 FROM 1. REFRESH: lit_cell_value. SPLIT lwa_excel_data-line AT cl_abap_char_utilities=>horizontal_tab INTO TABLE lit_cell_value. CLEAR: lwa_order_header. LOOP AT lit_cell_value INTO lwa_cell_value "读取第一列到第三列的内容 FROM 1 TO 3. TRY . CASE sy-tabix. WHEN 1. lwa_order_header-col_a = lwa_cell_value. WHEN 2. lwa_order_header-col_b = lwa_cell_value. WHEN 3. lwa_order_header-col_c = lwa_cell_value. WHEN OTHERS. ENDCASE. CATCH cx_sy_conversion_no_number INTO lo_error. l_message = lo_error->get_longtext( ). ENDTRY. CLEAR: lwa_cell_value. ENDLOOP. APPEND lwa_order_header TO git_order_header. CLEAR: lwa_order_header. ENDLOOP. ENDFORM. |
excel 文件:
以上。
发表评论