Thursday 5 July 2012

CSV file in application server(internal table data into CSV)

*Declerations for the CSV file.
  * Declerations for CSV
* "CSV file path in application layer.
constants: c_file1 type rcgfiletr-ftappl value 'D:\Test.csv'. * Module name
  data: l_line type string.
  constants c_blank value ''.
  data: l_modulename(31). " MM - OS - 1.1
  constants: c_modulename(10type c value 'Module:'.
* process name
  data: l_processname(100).
  constants: c_processname(15type c value 'Process Name:'.
* step name  eg: Client
  data: c_stepend(75type c value 'End Of The Process',
        g_header(5000type c.

* Declarations for the CSV conversion*******

constants: c_sep type char1 value ','.

databegin of wa_csvdata,
   line(4096),
 end of wa_csvdata.
data: gt_csvdata like table of wa_csvdata.

data: l_data type string.

field-symbols: <wa_field> type any,
                <ls_csv> type any.

* set to X if the no data has been put into the csv workarea
data: flag_firstcol type xflag.

*Declerations to display the status messages.
data : g_status type string.
constants: g_text(16type c value 'Start of Process' .


*Open the CSV file.
* Before processing the data we have to open the CSV file.

  open dataset c_file1 for output in text mode encoding default" To open the CSV file

*Process with the internal table data.

*here <it_final> is the field symbol(or internal table) which contains my data.
  import <it_final> from memory id 'memo1'.

   free memory id 'memo1'.

* TO append the data into CSV file
*To append the header to the csv document
* Module name
  transfer c_blank to c_file1. " To transfer one empty line
  concatenate c_modulename  p_wa_prgm_details-module_name  into l_line separated by c_comma. " To put some header
  transfer l_line to c_file1.

* Process name
  transfer c_blank to c_file1.
  clear l_line.
  concatenate c_processname  p_wa_prgm_details-process_name into l_line separated by c_comma.
  transfer l_line to c_file1.
  transfer c_blank to c_file1.

perform download_csv .

* To append the End of the Process
transfer c_blank to c_file1.
clear l_line.
move c_stepend to l_line.
*CONCATENATE C_stepend p_wa_prgm_details-process_name into l_line SEPARATED BY c_comma.
transfer l_line to c_file1.
transfer c_blank to c_file1.

**********************************************************************
*Download the CSV file.
*perform download_csv .
********************************************************  * here g_geader is the header of the file(Fields)
* we have to concatinate each field name into one string separatied by ','.
* concatenate g_header wa_tdgt_prgm-description into g_header separated by c_comma. " To get the headers 

*******************************************

form download_csv .
*Close the file.
* Before processing the data we have to open the CSV file.
  close dataset c_file1. " To close the CSV file


To view and download the created CSV file go to transaction cg3y and enter the csv file name and the target file name of our system than it will download.


 condense g_header.
  move g_header to wa_csvdata.
  append wa_csvdata to gt_csvdata.
  clear wa_csvdata.
  loop at <it_final> assigning <ls_csv>.
    flag_firstcol = 'X'.
    do.
      assign component sy-index of structure <ls_csv> to <wa_field>.
      if sy-subrc <> 0.
        exit.
      else.
        l_data = <wa_field>.

        if flag_firstcol = 'X'.
          wa_csvdata = l_data.
          flag_firstcol = ''.
        else.
          concatenate wa_csvdata c_sep l_data into wa_csvdata.
        endif.

      endif.
      enddo.
      append wa_csvdata to gt_csvdata.
      clear wa_csvdata.
    endloop.

    loop at gt_csvdata into wa_csvdata.
      transfer wa_csvdata to c_file1.
      clear wa_csvdata.
    endloop.
   refresh gt_csvdata.

No comments:

Post a Comment