cancel
Showing results for 
Search instead for 
Did you mean: 

How to upload a .xls excel file into WDA?

Former Member
0 Kudos

Hi,

    

     I have used FileUpload to get the .xls file data into xstring. And used 'HR_KR_XSTRING_TO_STRING' to convert the xstring to string. But instead of getting the correct file data as string I am getting some random scrambled characters. Its working fine while uploading the data as tab delimited text file.

     Could anyone suggest a proper method to tackle this issue. I have tried all the methods posted in SCN,nothing works.

Thanks and Regards.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Try the below code which will help you.


* references declarations

    DATA:

      lref_excel          TYPE REF TO cl_fdt_xl_spreadsheet,

      lref_excel_core     TYPE REF TO cx_fdt_excel_core,

      lref_data           TYPE REF TO data,

      lo_el_n_upload      TYPE REF TO if_wd_context_element.

* internal tables declaration

    DATA:

      lt_worksheets       TYPE string_table,

      lt_contents         TYPE string_table,

      lw_contents         LIKE LINE OF lt_contents,

      ls_file_upload      TYPE ty_ls_file_upload,

      lw_worksheet        TYPE string,

      lw_content          TYPE string,

      lv_msg              TYPE string,

      lv_flag             TYPE boolean.



* Field symbols declarations

    FIELD-SYMBOLS:

          <fs_table>      TYPE table,

          <fs_data>       TYPE any,

          <fs_comp>       TYPE any,

          <fs_output>     TYPE string.



    lo_el_n_upload = lo_nd_n_upload->get_element( ).

    lo_el_n_upload->get_static_attributes( IMPORTING static_attributes = ls_file_upload ).

IF ls_file_upload-file_name IS NOT  INITIAL AND ls_file_upload-file_name NP '*.XLSX' .

      CALL METHOD lo_message_manager->report_error_message

        EXPORTING

          message_text = 'Select .XLSX file'."#EC NOTEXT

      RETURN.

    ENDIF.

    TRY.

*     Create object of class to read .xlsx file contents

        CREATE OBJECT lref_excel

          EXPORTING

            document_name = ls_file_upload-file_name

            xdocument     = ls_file_upload-file_contents.



      CATCH cx_fdt_excel_core INTO lref_excel_core.

        CLEAR lv_msg.

*     Call method to get error message text

        CALL METHOD lref_excel_core->if_message~get_text

          RECEIVING

            result = lv_msg.

*<< Display error message returned in lv_msg >>

* report message

        CALL METHOD lo_message_manager->report_error_message

          EXPORTING

            message_text = lv_msg.

        RETURN.

    ENDTRY.



*Call method to get list of worksheets in the .xlsx file

    lref_excel->if_fdt_doc_spreadsheet~get_worksheet_names( IMPORTING  worksheet_names = lt_worksheets ).

    IF lt_worksheets IS NOT INITIAL.

      READ TABLE lt_worksheets INDEX 1 INTO lw_worksheet.

    ELSE.

*<< Display error message >>

      RETURN.

    ENDIF.



* Get reference of .xlsx file contents in the active worksheet

    lref_data = lref_excel->if_fdt_doc_spreadsheet~get_itab_from_worksheet( lw_worksheet ).

    ASSIGN lref_data->* TO <fs_table>.

    IF <fs_table> IS NOT ASSIGNED.

      RETURN.

    ENDIF.



* Loop dynamic table to prepare final table contents to pass in exporting parameter

    LOOP AT <fs_table> ASSIGNING <fs_data>.



      lv_flag = abap_true.

      WHILE lv_flag = abap_true.

        ASSIGN COMPONENT sy-index OF STRUCTURE <fs_data> TO <fs_comp>.

        IF <fs_comp> IS NOT ASSIGNED.

          lv_flag = abap_false.

          EXIT.

        ELSE.

          CONCATENATE lw_content <fs_comp> INTO lw_content SEPARATED BY '||'.

        ENDIF.

        UNASSIGN <fs_comp>.

      ENDWHILE.



*   Shift final string having a row left by 2 places to remove leading '||'

      SHIFT lw_content LEFT BY 2 PLACES.

*   Append prepared row data to exporting parameter

      APPEND lw_content TO lt_contents.

*   Clear variable having row data

      CLEAR lw_content.

    ENDLOOP.

Thanks

KH

Former Member
0 Kudos

Hi,

The version of SAP that i am using doesn't have the class 'cl_fdt_xl_spreadsheet'.

Also i want a method to upload .xls file specifically, .xlsx is not required.

Thank You,

former_member205488
Active Participant
0 Kudos

Hello!

Try to provide the codepage parameter. Like this:

CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'

  EXPORTING

    from_codepage = 'UTF-8'

    in_xstring    = iv_xcontent

  IMPORTING

    out_string    = lv_content.

Former Member
0 Kudos

Hi,

This method isn't working either.

Thank You.

Former Member
0 Kudos

Hi,

Refer below link which might help you.

Web Dynpro ABAP - File Upload - Excel

Thanks

KH