需求:將alv上面的數據計算到內表中區,然後通過自定義按鈕進行下載到csv格式中 附加:現在基本不用csv導出了,但是有些變態需求強行要求,也只好研究出來了,excel與txt導出很簡單,那就不多說了。 步驟: 1.copy系統標準的SAP_CONVERT_TO_CSV_FORMAT函數,此函數已經 ...
需求:將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裡面的值,記得用char4096. 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文件。 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.