原创 Tab Zhu [ SAP TAB ](javascript:void\(0\);)
**点击蓝字关注我们**
富强 民主 文明 和谐
自由 平等 公正 法制
爱国 敬业 诚信 友善
_**01** _
批量导出CL_XLSX_DOCUMENT
**不念过** **去**
**START**
**不畏将来**
今天写一下CL_XLSX_DOCUMENT个人觉得这个比较好用。
导出代码如下:
SELECT * INTO CORRESPONDING FIELDS OF TABLE @lt_zmmt001 FROM zmmt001 .
IF sy \- subrc EQ 0 AND p_file IS NOT INITIAL .
lv_filename = p_file .
CALL METHOD zcl_document_jxzhu => export_document_to_frontendEXPORTING
pi_filename = lv_filename_ * iv_sheetname = 'Sheet1' _ _ * IMPORTING _ _ * error_text = _
CHANGING
pt_tab = lt_zmmt001EXCEPTIONS
file_export_error = 1
OTHERS = 2 .
IF sy \- subrc <> 0 . _ * Implement suitable error handling here _
ENDIF . ---
METHOD export_document_to_frontend .
IF pt_tab IS INITIAL .MESSAGE e001 ( 00 ) RAISING file_export_error WITH |Table has no entry . | .
ENDIF .DATA filecontent TYPE xstring . _ " 内容 16 进制 _
TRY .DATA ( xlsx_handling ) = cl_ehfnd_xlsx => get_instance ( ). _ "xlsx 句柄
DATA ( xlsx_document ) = xlsx_handling -> create_doc ( ). _ "xlsx 文件
DATA ( xlsx_sheets ) = xlsx_document -> get_sheets ( ). _ " 得到 sheets
DATA ( first_xlsx_sheet ) = xlsx_document -> get_sheet_by_id (
xlsx_sheets[ 1 ] \- sheet_id ). _ " 得到 sheet _
first_xlsx_sheet -> change_sheet_name ( iv_sheetname ). _ " 设置 sheet
的名称 _
DATA ( lv_column ) = 1 . _ " 设置列 = 1 _DATA lw_tab_ref TYPE REF TO data .
CREATE DATA lw_tab_ref LIKE LINE OF pt_tab .
DATA tablestructure TYPE REF TO cl_abap_structdescr .
tablestructure ?= cl_abap_typedescr => describe_by_data_ref ( lw_tab_ref
). _ *set column name 得到列名 _
LOOP AT tablestructure -> components REFERENCE INTO DATA ( component ).first_xlsx_sheet -> set_cell_content ( iv_row = 1
iv_column = lv_columniv_value = component -> name ).
lv_column = lv_column + 1 .
ENDLOOP .DATA ( lv_row ) = 2 .FIELD-SYMBOLS <tabledata> TYPE STANDARD TABLE .
ASSIGN pt_tab TO <tabledata> . _ *set data of excel 写入内表的数据 _
LOOP AT <tabledata> ASSIGNING FIELD \- SYMBOL ( <currenttabledata> ).
lv_column = 1 .
LOOP AT tablestructure -> components REFERENCE INTO component .ASSIGN COMPONENT component -> name OF STRUCTURE <currenttabledata> TO FIELD
\- SYMBOL ( <columnvalue> ).
first_xlsx_sheet -> set_cell_content ( iv_row = lv_row
iv_column = lv_columniv_value = <columnvalue> ).
lv_column = lv_column + 1 .
ENDLOOP .
lv_row = lv_row + 1 .
ENDLOOP .filecontent = xlsx_document -> save ( ).
CATCH cx_openxml_format INTO DATA ( openxml_format_exception ).MESSAGE e001 ( 00 ) RAISING file_export_error
WITH 'Error occurs when constructing excel file instance.' . _ "
cx_openxml_format. _
CATCH cx_openxml_not_found INTO DATA ( openxml_not_found_exception ).MESSAGE e001 ( 00 ) RAISING file_export_error
WITH ' Error occurs when constructing excel file instance.' _
"CX_OPENXML_NOT_FOUND |. _ .
CATCH cx_openxml_not_allowed INTO DATA ( openxml_not_allowed_exception ).MESSAGE e001 ( 00 ) RAISING file_export_error
WITH 'Error occurs when constructing excel file instance.' _ "
CX_OPENXML_NOT_ALLOWED |. _ .
ENDTRY . _ * 将 16 进制转化为内表 _cl_scp_change_db => xstr_to_xtab ( EXPORTING im_xstring = filecontent
IMPORTING ex_xtab = DATA ( filecontenttab ) ). _ * 输出 _
cl_gui_frontend_services => gui_download (
EXPORTING
bin_filesize = xstrlen ( filecontent )
filename = pi_filename
filetype = 'BIN'
confirm_overwrite = abap_true
IMPORTING
filelength = DATA ( bytestransferred )
CHANGING
data_tab = filecontenttab
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24
).
IF sy \- subrc <> 0 .MESSAGE ID sy \- msgid TYPE sy \- msgty NUMBER sy \- msgno
WITH sy \- msgv1 sy \- msgv2 sy \- msgv3 sy \- msgv4 .
ELSE .
MESSAGE s001 ( 00 ) WITH bytestransferred ' bytes transferred' .
ENDIF .
ENDMETHOD .**不念过** **去**
**END**
**不畏将来**
**SAP ABAP TAB**
微信号 : TAB_JXZHU
后台回复'ABAP'免费领取学习资料
长按二维码关注我们