Skip to Content
avatar image
Former Member

Downloading Special Character Data to EXCEL using ATTACH_FILE_TO_RESPONSE

Dear All

I have designed a Webdynpro application  where there is one Button-Download to Excel which downloads the internal table data into Excel file .xls

The Problem is: If Internal Table contains special character like ' " ' example "BRING TEXT in one of column  DESCRIPTION of internal table, the entire data in XLS file gets confined to one cell and the data below this row do not come in Excel file.

Piece of Code :

call function 'SCMS_STRING_TO_XSTRING'

    exporting

      text = l_input

      mimetype = 'APPLICATION/MSEXCEL'

    importing

      buffer = l_xstring.

* Attach file to respond

CALL METHOD cl_wd_runtime_services=>attach_file_to_response(

i_filename = l_c_filename

i_content = l_xstring

i_mime_type = l_c_excel ).



Please guide what needs to be done so that DESCRIPTION column if contains special character or simple english text everytime the data should be downloaded to XLS file in correct format.

Thanks !!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    May 20, 2015 at 12:31 PM

    Hi,

    Can you please post the code that you have coded before passing to the xstring function.

    BR,

    RAM.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Dear Ram

      The code is as below:

      METHOD onactionexport .

        DATA lo_nd_cs_header TYPE REF TO if_wd_context_node.

        DATA lt_cs_header TYPE wd_this->elements_cs_header.

        DATA ls_cs_header LIKE LINE OF lt_cs_header.

        lo_nd_cs_header = wd_context->path_get_node( path = `RFX.CS_HEADER` ).

        lo_nd_cs_header->get_static_attributes_table( IMPORTING table = lt_cs_header ).

        TYPES: BEGIN OF ty_colhide,

                col_index TYPE i,

                hide(1) TYPE c,

               END OF ty_colhide.

        DATA: ls_colhide TYPE ty_colhide,

              lt_colhide TYPE TABLE OF ty_colhide.

        FIELD-SYMBOLS: <fs> TYPE any.

        " Create dynamic internal table and work area

        DATA: struct_type TYPE REF TO cl_abap_structdescr.

        DATA: table_type TYPE REF TO cl_abap_tabledescr.

        DATA: dyn_data_table TYPE REF TO data.

        DATA: dyn_data_wa TYPE REF TO data.

        FIELD-SYMBOLS: <dyn_itab> TYPE table, <dyn_wa> TYPE data.

        struct_type = wd_comp_controller->create_rtti( ).

        table_type = cl_abap_tabledescr=>create( p_line_type = struct_type ).

        CREATE DATA dyn_data_table TYPE HANDLE table_type.

        wd_assist->get_dyn_tab( IMPORTING dyn_tab = dyn_data_table ).

        CHECK dyn_data_table IS NOT INITIAL.

        ASSIGN dyn_data_table->* TO <dyn_itab>.

        CREATE DATA dyn_data_wa TYPE HANDLE struct_type.

        ASSIGN dyn_data_wa->* TO <dyn_wa>.

        DATA: comp_tab TYPE cl_abap_structdescr=>component_table.

        DATA: comp_wa LIKE LINE OF comp_tab.

        comp_tab = wd_comp_controller->create_comp_tab( ).

        "Scan columns for hiding

        DATA: lv_tabix TYPE sytabix.

        DATA: lv_counter TYPE i.

        LOOP AT <dyn_itab> INTO <dyn_wa>.

          ADD 1 TO lv_tabix.

          DO.

            CLEAR: ls_colhide.

            ls_colhide-col_index = sy-index.

            ls_colhide-hide = 'X'.

            IF lv_tabix = 1.

              APPEND ls_colhide TO lt_colhide.

            ENDIF.

            ASSIGN COMPONENT sy-index OF STRUCTURE <dyn_wa> TO <fs>.

            IF sy-subrc IS INITIAL.

              IF <fs> IS NOT INITIAL.

                CLEAR: ls_colhide-hide.

                MODIFY lt_colhide FROM ls_colhide TRANSPORTING hide WHERE col_index = sy-index.

              ENDIF.

            ELSE.

              EXIT.

            ENDIF.

          ENDDO.

        ENDLOOP.

        DATA text   TYPE string.

        DATA xtext  TYPE xstring.

        DATA: l_str TYPE string.

        DATA: l_val TYPE string.

        DATA: l_val2 TYPE string.

        DATA: l_attr_name TYPE string.

        DATA: l_attr TYPE string.

        DATA: l_prev_suppid TYPE string.

        DATA: l_offset TYPE i.

        DATA: l_head_count TYPE i.

        DATA: l_type TYPE rollname.

        DATA: l_descr TYPE reptext.

        DATA: lv_logsys TYPE logsys.

        SELECT SINGLE log_sys INTO lv_logsys

          FROM zsrmt_bckend_dst WHERE sys_name = 'ECC'.

        "Headers

        CLEAR: l_head_count.

        DO 7 TIMES. " Create 7 header lines

          ADD 1 TO l_head_count.

          CLEAR: lv_tabix, lv_counter, l_str, l_prev_suppid.

          LOOP AT comp_tab INTO comp_wa.

            ADD 1 TO lv_tabix.

            READ TABLE lt_colhide INTO ls_colhide WITH KEY col_index = lv_tabix

                                                           hide = 'X'.

            IF lv_tabix LT 6 OR sy-subrc IS INITIAL. "Skip technical or empty columns

              CONTINUE.

            ENDIF.

            "Read Vendor

            l_attr_name = comp_wa-name.

            "Try to shift all characters in attribute name to left

            l_attr = l_attr_name.

            DO.

              IF l_attr CN '0123456789'.

                SHIFT l_attr LEFT BY 1 PLACES.

              ELSE.

                EXIT.

              ENDIF.

            ENDDO.

            l_offset = strlen( l_attr ).

            IF l_offset GE 10. "This means it's a vendor's grouped column

              l_offset = l_offset - 10.

              DO l_offset TIMES.

                SHIFT l_attr LEFT BY 1 PLACES.

              ENDDO.

              CLEAR: ls_cs_header.

              READ TABLE lt_cs_header INTO ls_cs_header WITH KEY suppid = l_attr.

              ADD 1 TO lv_counter. "Count visible grouped column number

            ENDIF.

            CASE l_head_count.

              WHEN 1. "Technical Status

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Technical Status:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'TECH_STATUS' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 2."Vendor Name

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Vendor Name:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'SUPP_NAME' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 3."Country

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Country:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'SUPP_COUNTRY' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 4."Currency

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Currency:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'QUOT_CURR' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 5."Inco Terms

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Inco Terms:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'INCOTERMS' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                    ENDIF.

                    ASSIGN COMPONENT 'INCOTERMS2' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val2 = <fs>.

                      CONCATENATE l_val l_val2 INTO l_val SEPARATED BY space.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 6."Payment Terms

                IF l_attr NE l_prev_suppid.

                  IF lv_counter = 1.

                    l_val = 'Payment Terms:'.

                  ELSEIF lv_counter = 2.

                    ASSIGN COMPONENT 'PMNTTERMS' OF STRUCTURE ls_cs_header TO <fs>.

                    IF sy-subrc IS INITIAL.

                      l_val = <fs>.

                      CLEAR: l_val2.

                      SELECT SINGLE text1 INTO l_val2

                        FROM bbp_payterm_text

                        WHERE logsys = lv_logsys AND

                              spras = sy-langu AND

                              zterm = l_val.

                      CONCATENATE l_val l_val2 INTO l_val SEPARATED BY '-'.

                    ENDIF.

                    l_prev_suppid = l_attr.

                  ENDIF.

                ELSE.

                  CLEAR: l_val, lv_counter.

                ENDIF.

                IF l_val IS INITIAL.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN 7.

                " Header titles

                REPLACE l_attr IN comp_wa-name WITH ''.

                "Read data element description of field

                CLEAR: l_type.

                SELECT SINGLE rollname INTO l_type

                  FROM dd03l WHERE tabname = 'ZSRMT_CS_I' AND

                                   fieldname = comp_wa-name.

                IF sy-subrc IS NOT INITIAL.

                  SELECT SINGLE rollname INTO l_type

                    FROM dd03l WHERE tabname = 'ZSRMT_CS_LPP' AND

                                     fieldname = comp_wa-name.

                ENDIF.

                CLEAR: l_descr.

                IF sy-subrc IS INITIAL.

                  SELECT SINGLE reptext INTO l_descr

                    FROM dd04v WHERE rollname = l_type AND

                                     ddlanguage = sy-langu.

                ENDIF.

                IF comp_wa-name = 'COND_RATE'.

                  l_descr = 'Cond. rate'.

                ELSEIF comp_wa-name = 'COND_VALUE'.

                  l_descr = 'Cond. Abs/Calc. Value'.

                ENDIF.

                IF l_descr IS NOT INITIAL.

                  l_val = l_descr.

                ELSE.

                  l_val = cl_abap_char_utilities=>horizontal_tab.

                ENDIF.

                IF l_str IS INITIAL.

                  l_str = l_val.

                ELSE.

                  IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                     l_str CO cl_abap_char_utilities=>horizontal_tab.

                    CONCATENATE l_str l_val INTO l_str.

                  ELSE.

                    CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

                  ENDIF.

                ENDIF.

              WHEN OTHERS.

            ENDCASE.

          ENDLOOP.

          CONCATENATE text l_str

                      cl_abap_char_utilities=>newline INTO text.

        ENDDO.

        "Append data

        LOOP AT <dyn_itab> INTO <dyn_wa>.

          CLEAR: lv_tabix, l_str.

          LOOP AT comp_tab INTO comp_wa.

            ADD 1 TO lv_tabix.

            READ TABLE lt_colhide INTO ls_colhide WITH KEY col_index = lv_tabix

                                                           hide = 'X'.

            IF lv_tabix LT 6 OR sy-subrc IS INITIAL. "Skip technical or empty columns

              CONTINUE.

            ENDIF.

            CLEAR: l_val.

            ASSIGN COMPONENT comp_wa-name OF STRUCTURE <dyn_wa> TO <fs>.

            IF sy-subrc IS INITIAL.

              l_val = <fs>.

            ENDIF.

            IF l_val IS INITIAL.

              l_val = cl_abap_char_utilities=>horizontal_tab.

            ENDIF.

            IF l_str IS INITIAL.

              l_str = l_val.

            ELSE.

              IF l_val = cl_abap_char_utilities=>horizontal_tab OR

                 l_str CO cl_abap_char_utilities=>horizontal_tab.

                CONCATENATE l_str l_val INTO l_str.

              ELSE.

                CONCATENATE l_str l_val INTO l_str SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

              ENDIF.

            ENDIF.

          ENDLOOP.

          CONCATENATE text l_str

                      cl_abap_char_utilities=>newline INTO text.

        ENDLOOP.

        "Convert to XSTRING

        CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

          EXPORTING

            text   = text

          IMPORTING

            buffer = xtext.

        wdr_task=>client_window->client->attach_file_to_response(

         i_filename = 'CS.xls'

         i_content =  xtext

         i_mime_type = 'EXCEL' ).

      ENDMETHOD.