多sheet页 Excel文件上传
Posted ckstock
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多sheet页 Excel文件上传相关的知识,希望对你有一定的参考价值。
DATA lv_filename TYPE string. TYPES:BEGIN OF ty_excel, werks TYPE marc-werks, bwart TYPE matdoc-bwart, cpudt TYPE matdoc-cpudt, "凭证日期 budat TYPE matdoc-budat, "过账日期 hsdat TYPE matdoc-hsdat, "生产日期 lifnr TYPE matdoc-lifnr, "供应商 20181031 lgort TYPE matdoc-lgort, insmk TYPE matdoc-insmk, "库存类型 matnr TYPE matdoc-matnr, erfmg TYPE matdoc-erfmg, charg TYPE matdoc-charg, erfme TYPE matdoc-erfme, exbwr TYPE matdoc-exbwr, zhxm TYPE zmmb011_t-zhxm, END OF ty_excel. DATA: gt_excel TYPE STANDARD TABLE OF ty_excel. PARAMETERS p_file TYPE rlgrap-filename MODIF ID imp. lv_filename = p_file. CALL FUNCTION ‘ZBC_UPLOAD_EXCEL_OLE‘ EXPORTING pi_filename = lv_filename pi_startline = ‘2‘ pi_startcolumn = ‘1‘ TABLES pt_tab = gt_excel EXCEPTIONS open_file_err = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE e003(zbc) WITH sy-msgv1 sy-msgv2. ELSE. SORT gt_excel. ENDIF.
ZBC_UPLOAD_EXCEL_OLE
都必输入
FUNCTION zbc_upload_excel_ole. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" REFERENCE(PI_FILENAME) TYPE STRING *" REFERENCE(PI_SHEETNAME) TYPE STRING DEFAULT ‘Sheet1‘ *" REFERENCE(PI_STARTLINE) TYPE I DEFAULT ‘1‘ *" REFERENCE(PI_STARTCOLUMN) TYPE I DEFAULT ‘1‘ *" REFERENCE(PI_SKPCL_TBL) TYPE I DEFAULT ‘0‘ *" TABLES *" PT_TAB *" EXCEPTIONS *" OPEN_FILE_ERR *"---------------------------------------------------------------------- DATA: lt_tabc TYPE STANDARD TABLE OF zchar30000, lw_tabc TYPE zchar30000, lv_tabix TYPE sy-tabix, lt_cell TYPE STANDARD TABLE OF string, lw_cell TYPE string, lv_cell_tabix TYPE sy-tabix. DATA: lv_column_num TYPE i, lv_column_skp TYPE i, lw_tab_ref TYPE REF TO data. DATA: lo_cx_root TYPE REF TO cx_root. DATA: lv_message TYPE string. DATA descr_ref TYPE REF TO cl_abap_structdescr. FIELD-SYMBOLS: <lw_tab> TYPE any, <lv_value> TYPE any. CREATE DATA lw_tab_ref LIKE LINE OF pt_tab. ASSIGN lw_tab_ref->* TO <lw_tab>. lv_column_skp = pi_skpcl_tbl. *->把数据放到内表 CALL FUNCTION ‘ZBC_EXCEL_2_INNER_TABLE‘ EXPORTING pi_filename = pi_filename pi_sheetname = pi_sheetname TABLES pt_tab = lt_tabc EXCEPTIONS file_open_error = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE e000(zmm) WITH sy-msgv1 RAISING open_file_err. "&1&2&3&4 ENDIF. *->把数据切割到内表 LOOP AT lt_tabc INTO lw_tabc. lv_tabix = sy-tabix. CHECK lv_tabix >= pi_startline. CLEAR lt_cell. SPLIT lw_tabc AT cl_abap_char_utilities=>horizontal_tab INTO TABLE lt_cell. LOOP AT lt_cell INTO lw_cell. lv_cell_tabix = sy-tabix. lv_column_num = lv_cell_tabix - pi_startcolumn + 1 + lv_column_skp. CHECK lv_column_num > 0. ASSIGN COMPONENT lv_column_num OF STRUCTURE <lw_tab> TO <lv_value>. CHECK sy-subrc = 0. TRY. <lv_value> = lw_cell. CATCH cx_root INTO lo_cx_root. descr_ref ?= cl_abap_typedescr=>describe_by_data( pt_tab ). READ TABLE descr_ref->components INTO DATA(ls_name) INDEX lv_column_num. lv_message = ‘第‘ && lv_tabix && ‘行‘ && ‘第‘ && lv_column_num && ‘列字段名为‘&& ls_name-name && ‘错误的值为:‘ && lw_cell. MESSAGE e000(zmm) WITH lv_tabix lv_column_num lv_message RAISING open_file_err. ENDTRY. ENDLOOP. IF <lw_tab> IS NOT INITIAL. APPEND <lw_tab> TO pt_tab. CLEAR <lw_tab>. ENDIF. ENDLOOP. ENDFUNCTION.
ZBC_EXCEL_2_INNER_TABLE
其余必输入
FUNCTION zbc_excel_2_inner_table. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(PI_FILENAME) TYPE STRING *" VALUE(PI_SHEETNAME) TYPE STRING DEFAULT ‘Sheet1‘ *" TABLES *" PT_TAB *" EXCEPTIONS *" FILE_OPEN_ERROR *"---------------------------------------------------------------------- TYPE-POOLS: ole2. DATA: ole_excel TYPE ole2_object, ole_workbooks TYPE ole2_object, ole_workbook TYPE ole2_object, ole_worksheets TYPE ole2_object, ole_worksheet TYPE ole2_object, ole_cell_begin TYPE ole2_object, ole_cell_end TYPE ole2_object, ole_range TYPE ole2_object. DATA: lv_subrc TYPE sy-subrc, lv_begin_col TYPE i, lv_end_col TYPE i, lv_begin_row TYPE i, lv_end_row TYPE i, lv_add_rows TYPE i VALUE 3000. DATA: lt_excel_tab TYPE STANDARD TABLE OF zchar30000, lw_excel_tab TYPE zchar30000, lw_excel_tab_tmp TYPE zchar30000. *->生成Excel object CREATE OBJECT ole_excel ‘Excel.Application‘. IF sy-subrc <> 0. MESSAGE e001(zbc) RAISING file_open_error. ENDIF. *-> SET PROPERTY OF ole_excel ‘Visible‘ = 0. GET PROPERTY OF ole_excel ‘Workbooks‘ = ole_workbooks. CALL METHOD OF ole_workbooks ‘Open‘ = ole_workbook EXPORTING #1 = pi_filename. *->取得Sheet GET PROPERTY OF ole_workbook ‘Worksheets‘ = ole_worksheets EXPORTING #1 = pi_sheetname. IF sy-subrc = 0. CALL METHOD OF ole_worksheets ‘Activate‘. ELSE. CALL METHOD OF ole_excel ‘QUIT‘. MESSAGE e000(zmm) WITH ‘打开Sheet页出错,请检查Sheet名称‘ RAISING file_open_error. ENDIF. *->把Sheet上数据Copy 到 ClipBoard lv_begin_col = 1. lv_end_col = 256. lv_begin_row = 0. lv_end_row = 0. WHILE lv_subrc IS INITIAL. IF lv_begin_row IS INITIAL. lv_begin_row = 1. lv_end_row = lv_add_rows. ELSE. lv_begin_row = lv_begin_row + lv_add_rows. lv_end_row = lv_end_row + lv_add_rows. ENDIF. CALL METHOD OF ole_worksheets ‘Cells‘ = ole_cell_begin EXPORTING #1 = lv_begin_row #2 = lv_begin_col. CALL METHOD OF ole_worksheets ‘Cells‘ = ole_cell_end EXPORTING #1 = lv_end_row #2 = lv_end_col. CALL METHOD OF ole_worksheets ‘RANGE‘ = ole_range EXPORTING #1 = ole_cell_begin #2 = ole_cell_end. CALL METHOD OF ole_range ‘SELECT‘. IF sy-subrc <> 0. EXIT. ENDIF. CALL METHOD OF ole_range ‘COPY‘. * read clipboard into ABAP CALL METHOD cl_gui_frontend_services=>clipboard_import IMPORTING data = lt_excel_tab EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc <> 0. CALL METHOD OF ole_excel ‘QUIT‘. MESSAGE ‘Error during import of clipboard contents‘ TYPE ‘A‘. ENDIF. lv_subrc = 4. LOOP AT lt_excel_tab INTO lw_excel_tab. lw_excel_tab_tmp = lw_excel_tab. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN lw_excel_tab_tmp WITH space. IF NOT ( lw_excel_tab_tmp = space OR lw_excel_tab_tmp IS INITIAL ). APPEND lw_excel_tab TO pt_tab. CLEAR lv_subrc. ENDIF. ENDLOOP. CLEAR lt_excel_tab. ENDWHILE. DATA: lv_rc TYPE i. CALL METHOD cl_gui_frontend_services=>clipboard_export IMPORTING data = lt_excel_tab CHANGING rc = lv_rc EXCEPTIONS cntl_error = 1 * ERROR_NO_GUI = 2 * NOT_SUPPORTED_BY_GUI = 3 OTHERS = 4. * CALL METHOD OF * ole_worksheets * ‘COLSE‘. * CALL METHOD OF * ole_workbook * ‘COLSE‘. * CALL METHOD OF * ole_workbooks * ‘CLOSE‘. * SET PROPERTY OF ole_excel ‘DisplayAlerts‘ = 0. *前者并不会清空剪贴板,但是会让一些警告消息不再弹出 *如果你对文件做了更改,不保存就关闭的话,会有提示是否关闭的对话框, *这个时候displayalerts = 0就起作用了,可以跳过这个关闭提示 set property of ole_excel ‘CutCopyMode‘ = 0. *或者这个 CALL METHOD OF ole_excel ‘QUIT‘. FREE OBJECT: ole_excel , ole_workbooks , ole_workbook , ole_worksheets , ole_worksheet , ole_cell_begin , ole_cell_end , ole_range . ENDFUNCTION.