Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Create CSV to send it via cl_bsc

wagener-mark
Contributor
0 Kudos

Hallo,

I try to make a csv by:

*...
  LOOP AT t_sflight INTO wa_sflight.
      CONCATENATE "cl_abap_char_utilities=>CR_LF
                  wa_sflight-carrid
                  wa_sflight-connid
                  wa_sflight-fldate
                  wa_sflight-currency
                  wa_sflight-planetype
             INTO xls_file SEPARATED BY ';'.

      APPEND xls_file TO t_content_text.
    ENDLOOP.


    ca_attachment_header-line = 'MyFile.csv'.
    APPEND ca_attachment_header TO ca_t_attachment_header.

    CLEAR ca_documents.
    CLEAR content.


    ca_documents-type = 'csv'.           

*...

The Problem is, all data sets are in the first row...

kind regards

4 REPLIES 4

brad_bohn
Active Contributor
0 Kudos

You need to add the CR_LF at the end. You should also consider encapsulating your field values with double-quotes and using a string -> xstring -> binary format to attach/send the file.

0 Kudos

Thx. when i use cr_lf at the end, there is a blank line after each line...more over there is an error in the first column. only in the first row there is a value

Edit: DATA: xls_file TYPE string,

v_delimiter TYPE char1 VALUE cl_abap_char_utilities=>horizontal_tab.

Edited by: Mark Wagener on Apr 15, 2011 8:16 AM

Former Member
0 Kudos

Hi Mark,

Concatenate the new line character at the end of the column. The new line character is CL_ABAP_CHAR_UTILITIES=>NEWLINE. This will display the next record in a new line.

Also use comma (,) as column separator as file type is .CSV

Regards,

Ramnivas

brad_bohn
Active Contributor
0 Kudos

I've never had that issue, nor do I need the new line character as the other poster suggested. Here is some older code that I still use in an email class. The binary conversion could be updated with some newer syntax and done without the functions.



...

* Now add the internal table to the string
  LOOP AT ATTACH ASSIGNING <FS_LINE>.

    DO.

*     Access each component of the structure
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
      IF SY-SUBRC NE 0.
        EXIT.
      ENDIF.

*     Add the field to the line
      IF SY-INDEX EQ 1.
        CONCATENATE '"' <FS_FIELD> '"' INTO LV_CSV_STRING.
      ELSE.
        CONCATENATE LV_CSV_STRING
                    ','
                    '"'
                    <FS_FIELD>
                    '"'
                    INTO LV_CSV_STRING.
      ENDIF.

    ENDDO.

    CONCATENATE LV_CSV_STRING_FULL
                LV_CSV_STRING
                CL_ABAP_CHAR_UTILITIES=>CR_LF INTO LV_CSV_STRING_FULL.

  ENDLOOP.

...

      CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
        EXPORTING
          TEXT     = LV_CSV_STRING_FULL
          MIMETYPE = 'CSV'
        IMPORTING
          BUFFER   = LV_CSV_XSTRING
        EXCEPTIONS
          FAILED   = 1
          OTHERS   = 2.

      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          BUFFER        = LV_CSV_XSTRING
        IMPORTING
          OUTPUT_LENGTH = LV_LEN
        TABLES
          BINARY_TAB    = LT_CSVBIN.