1. 程式人生 > >Download File in UTF-8 encoding

Download File in UTF-8 encoding

REPORT z_test_utf8 NO STANDARD PAGE HEADING LINE-SIZE 125 .

TYPE-POOLS: abap.

TYPES:

BEGIN OF ty_mara,

matnr TYPE matnr,

END OF ty_mara,

BEGIN OF ty_string,

data TYPE string,

END OF ty_string.

DATA :

it_mara TYPE STANDARD TABLE OF ty_mara,

wa_mara TYPE ty_mara,

it_string TYPE STANDARD TABLE OF ty_string,

wa_string TYPE ty_string.

SELECT matnr

FROM mara

INTO TABLE it_mara UP TO 20 ROWS.

IF sy-subrc = 0.

SORT it_mara BY matnr.

ENDIF.

DATA:

v_string TYPE string,

v_xsrting TYPE xstring,

v_len TYPE i,

rc TYPE i VALUE 0,

v_encoding TYPE abap_encoding.

* Get the LOGON Encoding 

for the particular user

CALL METHOD cl_gui_frontend_services=>get_saplogon_encoding

CHANGING

rc = rc

file_encoding = v_encoding

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

not_supported_by_gui = 3

cannot_initialize_globalstate = 4

OTHERS = 5.

IF sy-subrc <> 

0 OR

v_encoding = 0.

CLEAR v_encoding.

ENDIF.

CHECK v_encoding IS NOT INITIAL.

* Attribute for UTF-8 format

v_xsrting = cl_abap_char_utilities=>byte_order_mark_utf8.

  

*--------- BEGIN OF DELETE S.SAHA 11.03.09 

* Convert XSTRING TO STRING

 

* CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'

* EXPORTING

* from_codepage = v_encoding

* in_xstring = v_xsrting

*IMPORTING

*out_string = v_string.

*IF sy-subrc = 0.

   

*--------- END OF DELETE S.SAHA 11.03.09 

  

* -------------- Using the class CL_ABAP_CONV_IN_CE to convert from XSTRING to STRING - Suhas Saha 11.03.09

DATA: v_conv TYPE REF TO cl_abap_conv_in_ce.

 * Create a Conversion Instance

v_conv = cl_abap_conv_in_ce=>create(

encoding = v_encoding

input = v_xsrting ).

 v_conv->read( IMPORTING data = v_string ).

* -------------- Using the class CL_ABAP_CONV_IN_CE to convert from XSTRING to STRING - Suhas Saha 11.03.09

  

LOOP AT it_mara INTO wa_mara.

IF sy-tabix = 1.

* To download a file in UTF-8 add the contents of BOM

* UTF8 infront of the first record.

CONCATENATE v_string wa_mara INTO wa_string-data.

APPEND wa_string TO it_string.

ELSE.

MOVE wa_mara TO wa_string-data.

APPEND wa_string TO it_string.

ENDIF.

ENDLOOP.

ENDIF.

* Download the file

CALL METHOD cl_gui_frontend_services=>gui_download

EXPORTING

filename = 'C:\Documents and Settings\ssaha\Desktop\utf.txt'

codepage = v_encoding

CHANGING

data_tab = it_string

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.

WRITE: /'File downloaded successfully !!!'.

ENDIF.