需求:将alv上面的数据计算到内表中区,然后通过自定义按钮进行下载到csv格式中
附加:现在基本不用csv导出了,但是有些变态需求强行要求,也只好研究出来了,excel与txt导出很简单,那就不多说了。
步骤:
1.copy系统标准的SAP_CONVERT_TO_CSV_FORMAT函数,此函数已经使用不了,命名为ZSAP_CONVERT_TO_CSV_FORMAT,然后进行修改几处;
a.系统标准的TRUXS_T_TEXT_DATA不存在,对于本机gui来说
ZCSV_T为自己修改的类型,用于接收后面函数table里面的值
FUNCTION ZSAP_CONVERT_TO_CSV_FORMAT.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(I_FIELD_SEPERATOR) TYPE CHAR01 DEFAULT ';'
*" VALUE(I_LINE_HEADER) TYPE CHAR01 OPTIONAL
*" VALUE(I_FILENAME) LIKE RLGRAP-FILENAME OPTIONAL
*" VALUE(I_APPL_KEEP) TYPE CHAR01 DEFAULT SPACE
*" TABLES
*" I_TAB_SAP_DATA TYPE STANDARD TABLE
*" CHANGING
*" VALUE(I_TAB_CONVERTED_DATA) TYPE ZCSV_T OPTIONAL
*" EXCEPTIONS
*" CONVERSION_FAILED
*"----------------------------------------------------------------------
CONSTANTS: C_FIELD_SEPARATOR VALUE ','.
DATA:
L_START_STRING LIKE SY-FDPOS,
L_END_STRING LIKE SY-FDPOS,
L_LEN_STRING(6) TYPE N,
L_EOL_STRING LIKE SY-FDPOS,
L_START_TARGET_STRING LIKE SY-FDPOS,
L_END_TARGET_STRING LIKE SY-FDPOS,
L_LEN_TARGET_STRING(6) TYPE N,
L_EOL_TARGET_STRING LIKE SY-FDPOS,
L_CONV_DATA TYPE LINE OF ZCSV_T,
L_CSV_DATA TYPE LINE OF ZCSV_T.
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
EXPORTING
I_FIELD_SEPERATOR = C_FIELD_SEPARATOR
I_LINE_HEADER = I_LINE_HEADER
I_FILENAME = I_FILENAME
TABLES
I_TAB_SAP_DATA = I_TAB_SAP_DATA
CHANGING
I_TAB_CONVERTED_DATA = I_TAB_CONVERTED_DATA
EXCEPTIONS
CONVERSION_FAILED = 4.
CHECK SY-SUBRC <> 0.
* if sy-subrc <> c_rc0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
RAISING CONVERSION_FAILED.
* endif.
DESCRIBE FIELD L_CSV_DATA LENGTH L_LEN_TARGET_STRING
in character mode.
DESCRIBE FIELD L_CONV_DATA LENGTH L_EOL_STRING
in character mode.
LOOP AT I_TAB_CONVERTED_DATA INTO L_CONV_DATA.
L_START_STRING = 1.
CLEAR: L_CSV_DATA,
L_START_TARGET_STRING,
L_END_TARGET_STRING.
DO.
SEARCH L_CONV_DATA FOR C_FIELD_SEPARATOR STARTING AT
L_START_STRING
ENDING AT
L_EOL_STRING.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
IF SY-SUBRC = 0.
L_END_STRING = L_START_STRING + SY-FDPOS.
L_LEN_STRING = L_END_STRING - L_START_STRING + 1.
L_START_STRING = L_START_STRING - 1.
L_END_TARGET_STRING = L_END_TARGET_STRING + L_LEN_STRING + 4.
IF L_END_TARGET_STRING < L_LEN_TARGET_STRING.
L_CSV_DATA+L_START_TARGET_STRING(3) = '"""'.
L_START_TARGET_STRING = L_START_TARGET_STRING + 3.
L_CSV_DATA+L_START_TARGET_STRING(L_LEN_STRING) =
L_CONV_DATA+L_START_STRING.
L_START_TARGET_STRING = L_START_TARGET_STRING + L_LEN_STRING
- 1.
L_CSV_DATA+L_START_TARGET_STRING(1) = '"'.
L_START_TARGET_STRING = L_START_TARGET_STRING + 1.
L_CSV_DATA+L_START_TARGET_STRING(1) = ';'.
L_END_TARGET_STRING = STRLEN( L_CSV_DATA ).
L_START_TARGET_STRING = L_END_TARGET_STRING.
L_START_STRING = L_END_STRING + 1.
ELSE.
EXIT.
ENDIF.
ENDIF.
ENDDO.
MODIFY I_TAB_CONVERTED_DATA FROM L_CSV_DATA.
ENDLOOP.
ENDFUNCTION.
2.下面为调用类的方法,为弹出框选择路径、将内表数据通过copy的函数用逗号分隔封装到另一内表中和下载csv文件。
DATA: truxs_t_text_data(4096) TYPE c OCCURS 0.
DATA: it_tabout LIKE truxs_t_text_data.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = 'CSV'
default_file_name = '*'
file_filter = 'CSV文件(*.CSV)'
CHANGING
filename = w_filename
path = w_file_path
fullpath = w_full_path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CLEAR it_tabout.
CALL FUNCTION 'ZSAP_CONVERT_TO_CSV_FORMAT'
* EXPORTING
* I_FIELD_SEPERATOR = ';'
* I_LINE_HEADER =
* I_FILENAME =
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = lt_worldship
CHANGING
i_tab_converted_data = it_tabout
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc = 0.
ENDIF.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
* bin_filesize =
filename = w_filename
** filetype = 'DAT'
* codepage = '8404'
* ignore_cerr = ABAP_TRUE
* replacement = '#'
CHANGING
data_tab = it_tabout
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.
ENDIF.
阅读(3347) | 评论(0) | 转发(0) |