excel模板下载与数据上传处理demo
(选择屏幕添加按钮,F4选择文件,模板下载,读取excel数据)
*&---------------------------------------------------------------------*
*& Report ztest_PS_MODIFY_WBS_NAME
*& Description: 通过附件导入更新项目名称,分期名称
*&---------------------------------------------------------------------*
*& Author:
*& Date: 20221107
*&---------------------------------------------------------------------*
REPORT ztest_ps_modify_wbs_name.
*--------------------------------------------------------------------*
*definition
TABLES:sscrfields.
CONSTANTS:gc_filepath TYPE string VALUE 'SAP/PUBLIC/EXCEL TEMPLATE/ZPS_TMPPOST1.XLSX'.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
*CLASS
CLASS lcl_output DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS:f4_path RETURNING VALUE(selected_folder) TYPE string,
down_temp,
upload RETURNING VALUE(up_result) TYPE string.
ENDCLASS. "lcl_output DEFINITION
CLASS lcl_output IMPLEMENTATION.
METHOD f4_path.
DATA: new_path TYPE string,
repid TYPE syrepid,
dynnr TYPE sydynnr,
lt_dynpfields TYPE TABLE OF dynpread,
ls_dynpfields LIKE LINE OF lt_dynpfields.
DATA:lt_filetab TYPE filetable,
ls_filetab TYPE file_table,
lv_rc TYPE i.
* Get current value
dynnr = sy-dynnr.
repid = sy-repid.
ls_dynpfields-fieldname = 'P_PATH'.
APPEND ls_dynpfields TO lt_dynpfields.
CALL FUNCTION 'DYNP_VALUES_READ'
EXPORTING
dyname = repid
dynumb = dynnr
TABLES
dynpfields = lt_dynpfields
EXCEPTIONS
invalid_abapworkarea = 1
invalid_dynprofield = 2
invalid_dynproname = 3
invalid_dynpronummer = 4
invalid_request = 5
no_fielddescription = 6
invalid_parameter = 7
undefind_error = 8
double_conversion = 9
stepl_not_found = 10
OTHERS = 11.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
READ TABLE lt_dynpfields INTO ls_dynpfields INDEX 1.
new_path = ls_dynpfields-fieldvalue.
selected_folder = new_path.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Select Upload File' " Title Of File Open Dialog
* default_extension = " Default Extension
* default_filename = " Default File Name
file_filter = 'All Files (*.*)|*.xlsx|*.xls' " File Extension Filter String
* with_encoding = " File Encoding
initial_directory = new_path " Initial Directory
multiselection = abap_false " Multiple selections poss.
CHANGING
file_table = lt_filetab " Table Holding Selected Files
rc = lv_rc " Return Code, Number of Files or -1 If Error Occurred
* user_action = " User Action (See Class Constants ACTION_OK, ACTION_CANCEL)
* file_encoding =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CHECK lv_rc EQ 1.
READ TABLE lt_filetab INTO ls_filetab INDEX 1.
CHECK sy-subrc EQ 0.
new_path = ls_filetab-filename.
cl_gui_cfw=>flush( ).
CHECK new_path IS NOT INITIAL.
selected_folder = new_path.
ENDMETHOD. "f4_path
METHOD down_temp.
DATA: new_path TYPE string,
lv_mime_file TYPE xstring.
DATA: xdata TYPE xstring, " Will be used for sending as email
t_rawdata TYPE solix_tab, " Will be used for downloading or open directly
bytecount TYPE i. " Will be used for downloading or open directly
new_path = 'C:\'.
cl_gui_frontend_services=>directory_browse(
EXPORTING
window_title = 'Select Path to Save Temp EXCEL-file'
initial_folder = new_path
CHANGING
selected_folder = new_path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4
).
cl_gui_cfw=>flush( ).
CHECK new_path IS NOT INITIAL.
cl_mime_repository_api=>get_api( )->get(
EXPORTING i_url = gc_filepath
IMPORTING e_content = lv_mime_file
EXCEPTIONS OTHERS = 8 ).
IF new_path CA '/'.
REPLACE REGEX '([^/])\s*$' IN new_path WITH '$1/' .
ELSE.
REPLACE REGEX '([^\\])\s*$' IN new_path WITH '$1\\'.
ENDIF.
t_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_mime_file ).
bytecount = xstrlen( lv_mime_file ).
CONCATENATE new_path text-001 '.XLSX' INTO new_path.
* Get trailing blank
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = bytecount
filename = new_path
filetype = 'BIN'
CHANGING data_tab = t_rawdata ).
ENDMETHOD.
METHOD upload.
ENDMETHOD.
ENDCLASS.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
* 选择屏幕
PARAMETERS: p_path TYPE string LOWER CASE MODIF ID pat DEFAULT 'C:\'.
*模板下载
SELECTION-SCREEN FUNCTION KEY 1.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
*调用
INITIALIZATION.
PERFORM frm_ini_sel_screen.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
p_path = lcl_output=>f4_path( ).
AT SELECTION-SCREEN.
PERFORM frm_down_temp.
START-OF-SELECTION.
* lcl_output=>upload( ).
PERFORM frm_upload_data.
END-OF-SELECTION.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
*FORM
FORM frm_ini_sel_screen .
DATA: lv_functxt TYPE smp_dyntxt,
lt_exclude TYPE TABLE OF sy-ucomm.
lv_functxt-icon_id = icon_xls.
lv_functxt-icon_text = text-002.
sscrfields-functxt_01 = lv_functxt."'EXCEL模板下载'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWN_TEMP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_down_temp .
CASE sscrfields-ucomm.
WHEN 'FC01'.
lcl_output=>down_temp( ).
WHEN OTHERS.
ENDCASE.
ENDFORM. " FRM_DOWN_TEMP_GET_DATA_FROM_EXCEL
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_upload_data .
DATA: new_path TYPE string,
repid TYPE syrepid,
dynnr TYPE sydynnr,
lt_dynpfields TYPE TABLE OF dynpread,
ls_dynpfields LIKE LINE OF lt_dynpfields.
DATA:lo_reader TYPE REF TO zif_excel_reader,
lo_excel TYPE REF TO zcl_excel,
lo_worksheet TYPE REF TO zcl_excel_worksheet,
lt_cell TYPE zexcel_t_cell_data.
TYPES:BEGIN OF ts_proj,
pspnr TYPE ps_intnr,
pspid TYPE ps_pspid,
post1 TYPE ps_post1,
END OF ts_proj,
tt_proj TYPE STANDARD TABLE OF ts_proj,
BEGIN OF ts_prps,
pspnr TYPE ps_posnr,
posid TYPE ps_posid,
post1 TYPE ps_post1,
END OF ts_prps,
tt_prps TYPE STANDARD TABLE OF ts_prps
.
DATA:ls_proj TYPE ts_proj,
ls_prps TYPE ts_prps,
lt_proj TYPE tt_proj,
lt_prps TYPE tt_prps,
lt_proj_b TYPE TABLE OF ztest_d_upd_post1,
ls_proj_b TYPE ztest_d_upd_post1,
lt_prps_b TYPE TABLE OF ztest_d_upd_post1,
ls_prps_b TYPE ztest_d_upd_post1.
FIELD-SYMBOLS:<fs_proj> TYPE ts_proj,
<fs_prps> TYPE ts_prps.
* <fs_proj_b> TYPE ztest_d_upd_post1.
* 使用ABAP2XLSX读取excel
CREATE OBJECT lo_reader TYPE zcl_excel_reader_2007.
TRY.
lo_excel = lo_reader->load_file(
i_filename = p_path
* i_use_alternate_zip = SPACE
* i_from_applserver = SY-BATCH
* iv_zcl_excel_classname =
).
CATCH zcx_excel. "
ENDTRY.
CHECK lo_excel IS BOUND.
* 获取项目数据
CALL METHOD lo_excel->set_active_sheet_index_by_name
EXPORTING
i_worksheet_name = text-003. " Worksheets name
lo_worksheet = lo_excel->get_active_worksheet( ).
IF lo_worksheet IS BOUND.
lt_cell = lo_worksheet->sheet_content.
DELETE lt_cell WHERE cell_value IS INITIAL.
DELETE lt_cell WHERE cell_row = 1.
LOOP AT lt_cell INTO DATA(ls_cell).
CASE ls_cell-cell_column.
WHEN 1.
ls_proj-pspid = ls_cell-cell_value.
CALL FUNCTION 'CONVERSION_EXIT_KONPD_INPUT'
EXPORTING
input = ls_cell-cell_value
IMPORTING
output = ls_proj-pspnr
* PROJWA =
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
CONTINUE.
ENDIF.
WHEN 2.
ls_proj-post1 = ls_cell-cell_value.
WHEN OTHERS.
ENDCASE.
AT END OF cell_row.
IF ls_proj-pspnr IS NOT INITIAL.
APPEND ls_proj TO lt_proj.
ENDIF.
CLEAR:ls_proj.
ENDAT.
ENDLOOP.
ENDIF.
* 获取分期数据
CALL METHOD lo_excel->set_active_sheet_index_by_name
EXPORTING
i_worksheet_name = text-004. " Worksheets name
lo_worksheet = lo_excel->get_active_worksheet( ).
IF lo_worksheet IS BOUND.
lt_cell = lo_worksheet->sheet_content.
DELETE lt_cell WHERE cell_value IS INITIAL.
DELETE lt_cell WHERE cell_row = 1.
LOOP AT lt_cell INTO ls_cell.
CASE ls_cell-cell_column.
WHEN 1.
ls_prps-posid = ls_cell-cell_value.
CALL FUNCTION 'CONVERSION_EXIT_ABPSP_INPUT'
EXPORTING
input = ls_cell-cell_value
IMPORTING
output = ls_prps-pspnr
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
CONTINUE.
ENDIF.
WHEN 2.
ls_prps-post1 = ls_cell-cell_value.
WHEN OTHERS.
ENDCASE.
AT END OF cell_row.
IF ls_prps-pspnr IS NOT INITIAL.
APPEND ls_prps TO lt_prps.
ENDIF.
CLEAR:ls_prps.
ENDAT.
ENDLOOP.
ENDIF.
IF lt_proj IS NOT INITIAL.
LOOP AT lt_proj ASSIGNING <fs_proj>.
CALL FUNCTION 'CONVERSION_EXIT_ABPSN_INPUT'
EXPORTING
input = <fs_proj>-pspid
IMPORTING
output = <fs_proj>-pspid.
ENDLOOP.
SELECT pspnr post1 AS post1_b INTO CORRESPONDING FIELDS OF TABLE lt_proj_b
FROM proj
FOR ALL ENTRIES IN lt_proj
WHERE pspnr EQ lt_proj-pspnr.
IF sy-subrc EQ 0.
LOOP AT lt_proj_b ASSIGNING FIELD-SYMBOL(<fs_proj_b>).
IF <fs_proj_b>-pspnr IS NOT INITIAL.
<fs_proj_b>-tabname = 'PROJ'.
READ TABLE lt_proj WITH KEY pspnr = <fs_proj_b>-pspnr INTO ls_proj." BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_proj_b>-post1_a = ls_proj-post1.
ENDIF.
<fs_proj_b>-udate = sy-datum.
<fs_proj_b>-uname = sy-uname.
ENDIF.
ENDLOOP.
ENDIF.
LOOP AT lt_proj ASSIGNING <fs_proj>.
UPDATE proj SET post1 = <fs_proj>-post1 WHERE pspnr EQ <fs_proj>-pspnr.
UPDATE prps SET post1 = <fs_proj>-post1 WHERE posid EQ <fs_proj>-pspid.
ENDLOOP.
MODIFY ztest_d_upd_post1 FROM TABLE lt_proj_b.
ENDIF.
IF lt_prps IS NOT INITIAL.
LOOP AT lt_prps ASSIGNING <fs_prps>.
CALL FUNCTION 'CONVERSION_EXIT_ABPSN_INPUT'
EXPORTING
input = <fs_prps>-posid
IMPORTING
output = <fs_prps>-posid.
ENDLOOP.
SELECT pspnr AS pspnr_f post1 AS post1f_b INTO CORRESPONDING FIELDS OF TABLE lt_prps_b
FROM prps
FOR ALL ENTRIES IN lt_prps
WHERE pspnr EQ lt_prps-pspnr.
IF sy-subrc EQ 0.
LOOP AT lt_prps_b ASSIGNING FIELD-SYMBOL(<fs_prps_b>).
IF <fs_prps_b>-pspnr_f IS NOT INITIAL.
<fs_prps_b>-tabname = 'PRPS'.
READ TABLE lt_prps WITH KEY pspnr = <fs_prps_b>-pspnr_f INTO ls_prps.
IF sy-subrc EQ 0.
<fs_prps_b>-post1f_a = ls_prps-post1.
ENDIF.
<fs_prps_b>-uname = sy-uname.
<fs_prps_b>-udate = sy-datum.
ENDIF.
ENDLOOP.
ENDIF.
LOOP AT lt_prps ASSIGNING <fs_prps>.
UPDATE prps SET post1 = <fs_prps>-post1 WHERE pspnr EQ <fs_prps>-pspnr.
ENDLOOP.
MODIFY ztest_d_upd_post1 FROM TABLE lt_prps_b.
ENDIF.
IF lt_proj_b IS NOT INITIAL OR lt_prps_b IS NOT INITIAL.
WRITE:'已更新,详情请查看日志表:ztest_d_upd_post1'.
ELSE.
WRITE:'无数据更新'.
ENDIF.
ENDFORM. " FRM_UPLOAD_DATA
*--------------------------------------------------------------------*
以上