cancel
Showing results for 
Search instead for 
Did you mean: 

How Export to CSV format from WDA

former_member189599
Participant
0 Kudos

Hi masters!

i need to export an ALV table to CSV fromart

i use the following code

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

EXPORTING

i_field_seperator = ';'

TABLES

i_tab_sap_data = lt_t_book

CHANGING

i_tab_converted_data = lt_t_book_cvs

EXCEPTIONS

conversion_failed = 1

OTHERS = 2.

IF sy-subrc = 0.

"Download data using GUI_DOWNLOAD

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = 'C:\Documents and Settings\lucas.mena\Desktop\csv_test.txt'

filetype = 'ASC'

CODEPAGE = LC_1160

TABLES

data_tab = lt_t_book_cvs.

ENDIF.

as a result, i have received the following error message: Error in Control Framework

thanks,

Lucas

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

You can't use GUI_DOWNLOAD or CL_GUI_FRONTEND_SERVICES from within Web Dynpro ABAP. In WDA you are running in a browser, not the SAPGUI. You have no connection to the SAPGUI.

You have to use the FileDownload UI element or the CL_WD_RUNTIME_SERVICES=>ATTACH_FILE_TO_RESPONSE method.

former_member189599
Participant
0 Kudos

Hi Thomas!

I used the method cl_wd_runtime_services=>attach_file_to_response in order to download to XML/XLS format.

Now, i need to download in CSV format

do you know how i can do that?

thanks

Lucas

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

>

> Hi Thomas!

> I used the method cl_wd_runtime_services=>attach_file_to_response in order to download to XML/XLS format.

> Now, i need to download in CSV format

> do you know how i can do that?

> thanks

>

> Lucas

cl_wd_runtime_services=>attach_file_to_response takes whatever content you give it. It isn't specific to XML or XLS. Give it CSV content and that is what it will download.

former_member189599
Participant
0 Kudos

Hi Thomas!

thanks for your quick answer!

this is the code that i used:

CONSTANTS:

lco_filename TYPE string VALUE `TEST.XML`,

lco_mime_type TYPE string VALUE `text/xml`.

DATA:

lr_bookingid TYPE REF TO bapisbodat-bookingid,

lr_t_book TYPE REF TO if_wd_context_node,

lr_element TYPE REF TO if_wd_context_node, "if_wd_context_element,

lt_t_book TYPE wd_this->elements_t_book,

lv_xml TYPE string,

lv_xxml TYPE xstring.

  • Get reference of the node

lr_t_book = wd_context->get_child_node( name = wd_this->wdctx_t_book ).

IF lr_t_book IS NOT INITIAL.

  • Get the table with the elements attributes

lr_t_book->get_static_attributes_table( IMPORTING table = lt_t_book ).

ENDIF.

  • Returns the XML representation of the selected element

lr_element = wd_context->get_child_node( 'T_BOOK' ).

lv_xml = lr_element->to_xml( ).

  • Converts the string to xstring

lv_xxml = wd_this->convert_file( lv_xml ).

  • Shows it in a new browser window

cl_wd_runtime_services=>attach_file_to_response(

i_filename = lco_filename

i_content = lv_xxml

i_mime_type = lco_mime_type

i_in_new_window = abap_FALSE

i_inplace = abap_false ).

please note, that before calling the method cl_wd_runtime_services=>attach_file_to_response, i use the method lr_element->to_xml to convert XML format.

how can i indicate that the content is CSV format?

Can you give me an example, please?

on more time, thanks

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

The MIME type for CSV is text/csv. MIME types aren't something SAP specific. You can generally lookup correct mime types just by googling for them.

uday_gubbala2
Active Contributor
0 Kudos

Hello Luca,

1) First read the table's data into an internal table.

2) Convert the internal table data to STRING format.

3) Now convert it into tab separated format as how desired.

4) Convert this STRING format to XSTRING format

5) Make use of the attach_file_to_response method.

METHOD onactionon_submit .
  DATA: lv_node TYPE REF TO if_wd_context_node,
        lt_mara TYPE if_main=>elements_mara,
        wa_mara TYPE if_main=>element_mara,
        lead_selection_index TYPE i,
 
        mara_string  TYPE string,
        mara_xstring TYPE xstring.
 
  lv_node = wd_context->get_child_node( name = 'MARA' ).
  CALL METHOD lv_node->get_static_attributes_table
    IMPORTING
      table = lt_mara.
 
  LOOP AT lt_mara INTO wa_mara.
    CONCATENATE mara_string
                wa_mara-matnr
                wa_mara-ersda
                wa_mara-ernam
                wa_mara-matkl
                wa_mara-meins
                cl_abap_char_utilities=>cr_lf INTO mara_string
                                        SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
  ENDLOOP.
 
** Now you need to add the column headers & the desired extra information through coding to 
** mara_string
 
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text   = mara_string
    IMPORTING
      buffer = mara_xstring.
 
 
  CL_WD_RUNTIME_SERVICES=>attach_file_to_response(  i_filename  = 'TEMP.XLS'
                                                    i_content   = mara_xstring
                                                    i_mime_type = 'EXCEL' ).
ENDMETHOD.

Regards,

Uday

former_member189599
Participant
0 Kudos

Hi Uday!!

your answer was very good and resolved my question

thank you very much!

Regards!!

LUCAS

Former Member
0 Kudos

Hi Lucas,

I have the same problem as you. But after I go through all the messages here, I still have some format problem in my csv file.

All the information is displayed in one cell. Please see the following link.

[My exported CSV file|http://picasaweb.google.com/meerwu/DropBox?authkey=Gv1sRgCJy33JuwwsSVDQ&pli=1&gsessionid=ke8pC-MZCCUT4wQhtac6qA#5392804569281421650]

Do you know, how could I seperate the info into the columns?

Former Member
0 Kudos

After doing some research, I got the solution. CSV-Format has many different 'types'. Depending on the types, the content of CSV-file can be separated by comma, semicolon, tabulator, etc. My CSV needs semicolon to separate the info.

The detail about this format can be found throung google.

Answers (0)