Excel文件下载模板

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

* CALL FUNCTION 'ZDOWNLOAD_TEMPLE_FILE'
* EXPORTING
* p_templename = 'ZTEMPLE.XLS'.

DATA:BEGIN OF tzsql OCCURS 0,
sql(100) TYPE c,
END OF tzsql.


DATA:hsql LIKE TABLE OF tzsql WITH HEADER LINE.

*----------------------------------------------------------------------*
* CLASS filetempdown_exception DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS filetempdown_exception DEFINITION INHERITING FROM cx_static_check.

PUBLIC SECTION.
METHODS: constructor IMPORTING value(p_text) TYPE char40 OPTIONAL .
METHODS write_msg.
METHODS get_text REDEFINITION.

PRIVATE SECTION .
DATA: my_text(50) TYPE c.
ENDCLASS. "CX_SAMPLE_EXCEPTION DEFINITION

**** Class-based Exceptions Implementation
*----------------------------------------------------------------------*
* CLASS class_exception IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS filetempdown_exception IMPLEMENTATION.
METHOD constructor .
CALL METHOD super->constructor.
IF p_text IS INITIAL.
my_text = space.
ELSE.
my_text = p_text.
ENDIF.
ENDMETHOD . "constructor
METHOD write_msg.

WRITE / 'Method of Class class_exception'.

ENDMETHOD. "write_msg
METHOD get_text.
result = me->my_text.
ENDMETHOD. ":
ENDCLASS. "class_exception IMPLEMENTATION


*----------------------------------------------------------------------*
* CLASS filetempdowncan_exception DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS filetempdowncan_exception DEFINITION INHERITING FROM cx_static_check.

PUBLIC SECTION.
METHODS write_msg.

ENDCLASS. "CX_SAMPLE_EXCEPTION DEFINITION

**** Class-based Exceptions Implementation
*----------------------------------------------------------------------*
* CLASS class_exception IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS filetempdowncan_exception IMPLEMENTATION.
METHOD write_msg.
WRITE / 'Method of Class class_exception'.
ENDMETHOD. ":
ENDCLASS. "class_exception IMPLEMENTATION


*&---------------------------------------------------------------------*
*& Form frm_exporttablebytab
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_FILENAME) text
* -->VALUE(P_TABLENAMESTRU) text
* -->P_FSTAB text
*-----------------------------------------------------------------

-----*
FORM frm_exporttablebytab USING value(p_filename) value(p_tablenamestru) CHANGING p_fstab TYPE ANY TABLE .

FIELD-SYMBOLS : TYPE STANDARD TABLE.
FIELD-SYMBOLS:.
DATA:dy_line TYPE REF TO data.

DATA: filename TYPE string VALUE 'g:\t_resb_218210000050.txt'.
DATA: BEGIN OF it_field OCCURS 0 ,
field(100),
END OF it_field.
*TYPE-POOLS : abap.
DATA:idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
*FIELD-SYMBOLS: ,.
DATA:col(20) TYPE c.
filename = p_filename.

ASSIGN p_fstab TO .
IF IS INITIAL.
MESSAGE 'NONE DATA ERROR!' TYPE 'E'.
ELSE.
CREATE DATA dy_line LIKE LINE OF .
ASSIGN dy_line->* TO .
ref_table_des ?= cl_abap_typedescr=>describe_by_data( ).
idetails[] = ref_table_des->components[].

LOOP AT idetails INTO xdetails.
CONCATENATE '' xdetails-name INTO col.
APPEND col TO it_field.
ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
confirm_overwrite = 'X'
write_field_separator = 'X'
filename = filename "文件名 必须为 STRING 类型
TABLES
data_tab = "内表
fieldnames = it_field
EXCEPTIONS
file_write_error = 1
file_not_found = 2.

IF sy-subrc EQ 1.
MESSAGE 'FILE_WRITE_ERROR' TYPE 'I'.
ENDIF.

IF sy-subrc EQ 2.
MESSAGE 'FILE_NOT_FOUND ' TYPE 'I'.
ENDIF.

ENDIF.

ENDFORM. "FRM_EXPORTTABLE


*&---------------------------------------------------------------------*
*& Form frm_exporttablebyftab
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_FILENAME) text
* -->P_FSTAB text
*----------------------------------------------------------------------*
FORM frm_exporttablebyftab USING value(p_filename) CHANGING p_fstab TYPE ANY TABLE .

FIELD-SYMBOLS : TYPE STANDARD TABLE.
FIELD-SYMBOLS:.
DATA:dy_line TYPE REF TO data.

DATA: l_tabcount TYPE i.
DATA: filename TYPE string VALUE 'g:\t_resb_218210000050.txt'.
DATA: BEGIN OF it_field OCCURS 0 ,
field(100),
END OF it_field.
*TYPE-POOLS : abap.
DATA:idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
*FIELD-SYMBOLS: ,.
DATA:col(20) TYPE c.
filename = p_filename.

ASSIGN p_fstab TO .
IF IS INITIAL.
MESSAGE 'NONE DATA ERROR!' TYPE 'E'.
ELSE.
DESCRIBE TABLE LINES l_tabcount.

CHECK l_tabcount > 0 . "没有数据,不能导出数据.
CREATE DATA dy_line LIKE LINE OF <

fs_tab>.
ASSIGN dy_line->* TO .
ref_table_des ?= cl_abap_typedescr=>describe_by_data( ).

idetails[] = ref_table_des->components[].

LOOP AT idetails INTO xdetails.

CONCATENATE '' xdetails-name INTO col.
APPEND col TO it_field.
ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
confirm_overwrite = 'X'
write_field_separator = 'X'
filename = filename "文件名 必须为 STRING 类型
TABLES
data_tab = "内表
fieldnames = it_field
EXCEPTIONS
file_write_error = 1
file_not_found = 2.
IF sy-subrc EQ 1.
MESSAGE 'FILE_WRITE_ERROR' TYPE 'I'.
ENDIF.
IF sy-subrc EQ 2.
MESSAGE 'FILE_NOT_FOUND ' TYPE 'I'.
ENDIF.
ENDIF.

* endif.
* """"""""20131105 导出*****end**********
ENDFORM. "FRM_EXPORTTABLE


*&---------------------------------------------------------------------*
*& Form del_stringdi
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_C) text
* -->VALUE(P_PATT) text
* -->P_MOFF text
*----------------------------------------------------------------------*
FORM del_stringdi USING value(p_c) value(p_patt) CHANGING p_moff.
DATA:
off TYPE i,
* moff TYPE i,
mlen TYPE i.
off = 0.
FIND p_patt IN SECTION OFFSET off OF
p_c
MATCH OFFSET p_moff
MATCH LENGTH mlen.
IF sy-subrc NE 0.
p_moff = -1.
ENDIF.
ENDFORM. "del_stringdi

*&---------------------------------------------------------------------*
*& Form down_mode_comm
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_KEY) text
* -->RAISING text
* -->FILETEMPDOWNCAN_EXCEPTION text
* -->FILETEMPDOWN_EXCEPTION text
*----------------------------------------------------------------------*
FORM down_mode_comm USING value(p_key) TYPE char40 P_FILENAME TYPE char40
RAISING filetempdowncan_exception filetempdown_exception.
DATA: lc_path TYPE string,
lc_fullpath TYPE string,
lc_filename TYPE string,
lc_default TYPE string,
l_filename(70) TYPE c.

DATA:l_can TYPE REF TO filetempdowncan_exception.
lc_default = p_key .

IF p_filename IS INITIAL .
CONCATENATE sy-datum '-' p_key INTO lc_default .
ELSE.
CONCATENATE p_filename '-' sy-datum INTO lc_default .
ENDIF .

CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* window_title =
default_extension = 'XLS'
default_file_n

ame = lc_default
* with_encoding =
* file_filter = 'Excel文件(*.XLS)|*.XLS|全部文件 (XLSX.XLSX)|*.XLSX|'
file_filter = 'Excel文件(*.XLS)|*.XLS|'
* initial_directory =
* prompt_on_overwrite = 'X'
CHANGING
filename = lc_filename
path = lc_path
fullpath = lc_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
* invalid_default_file_name = 4
* others = 5
.
IF lc_fullpath = ''.
RAISE EXCEPTION TYPE filetempdowncan_exception .
* RETURN.
ENDIF.
l_filename = lc_filename.
PERFORM download_excel_template_comm USING p_key lc_filename lc_path."'ZSDF32T.XLS'

ENDFORM. "down_mode_comm



*&---------------------------------------------------------------------*
*& Form download_excel_template_comm
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_OBJID text
* -->FP_DEST text
* -->FILE_PATH text
* -->RAISING text
* -->FILETEMPDOWN_EXCEPTION text
*----------------------------------------------------------------------*
FORM download_excel_template_comm USING p_objid LIKE wwwdatatab-objid fp_dest TYPE string
file_path TYPE string RAISING filetempdown_exception .


DATA: lo_objdata LIKE wwwdatatab,
ls_errtxt(40) TYPE c,
lc_path TYPE string,
li_rc LIKE sy-subrc,
ls_destination LIKE rlgrap-filename.

lc_path = file_path .

SELECT SINGLE relid objid
FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.

IF sy-subrc NE 0 OR lo_objdata-objid EQ space."如果不存在,则给出错误提示
CONCATENATE '模板文件' p_objid '不存在' INTO ls_errtxt.
RAISE EXCEPTION TYPE filetempdown_exception EXPORTING p_text = ls_errtxt .
ENDIF.

ls_destination = fp_dest. "保存路径

CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lo_objdata
destination = ls_destination
IMPORTING
rc = li_rc.

IF li_rc NE 0.
CONCATENATE '模板文件:' p_objid '下载失败' INTO ls_errtxt.
RAISE EXCEPTION TYPE filetempdown_exception EXPORTING p_text = ls_errtxt .
ELSE.

CONCATENATE '模板文件' p_objid '下载成功' INTO ls_errtxt.

CALL METHOD cl_gui_frontend_services=>execute
EXPORTING
document = fp_dest
default_directory = lc_path
EXCEPTIONS
OTHERS = 22.

MESSAGE l

s_errtxt TYPE 'S' .
ENDIF.

ENDFORM. "download_excel_template

FUNCTION zdownload_temple_file.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(P_TEMPLENAME) TYPE CHAR40
*" REFERENCE(P_FILENAME) TYPE CHAR40 OPTIONAL
*"----------------------------------------------------------------------


*&---------------------------------------------------------------------*
*& Form DOWN_DILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
DATA:l_key(40) TYPE c.
DATA:l_mess(50) TYPE c.
DATA:g_downtemplate TYPE c.

l_key = p_templename .

DATA oref TYPE REF TO filetempdowncan_exception.
DATA oref_err TYPE REF TO filetempdown_exception.

CLEAR g_downtemplate.

TRY.
PERFORM down_mode_comm USING l_key P_FILENAME."FILETEMPDOWNCAN_EXCEPTION FILETEMPDOWN_EXCEPTION
g_downtemplate = 'E'.
CATCH filetempdowncan_exception INTO oref.
g_downtemplate = 'X'.
MESSAGE '取消模板下载!' TYPE 'I'.
CATCH filetempdown_exception INTO oref_err.
g_downtemplate = 'X'.
CALL METHOD oref_err->get_text
RECEIVING
result = l_mess.
MESSAGE l_mess TYPE 'I'.
ENDTRY.

ENDFUNCTION.

相关文档
最新文档