多sheet页 Excel文件上传
2021-05-13 00:27
标签:arc vat ted get 上传 pcl char component oar 都必输入 其余必输入 多sheet页 Excel文件上传 标签:arc vat ted get 上传 pcl char component oar 原文地址:https://www.cnblogs.com/ckstock/p/12003761.htmlDATA 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:
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.