<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: CSV file changes dynamically in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689442#M303145</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looks like Michael and I were thinking the same.  You need to find the biggest record length first, then build your dynamica internal table, then fill it with data.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

report zrich_0001.

types: begin of ttab,
       rec(1000) type c,
       end of ttab.

types: begin of tdat,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end of tdat.

data: itab type table of ttab with header line.
data: idat type table of tdat with header line.

type-pools : abap, slis.

field-symbols: &amp;lt;dyn_table&amp;gt; type standard table,
               &amp;lt;dyn_wa&amp;gt;,
               &amp;lt;dyn_field&amp;gt;.

data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat,
      it_fldcat type lvc_t_fcat..

data: file_str type string.

data: istr type table of string with header line.

data: istr_lines type i,
      biggest_string type i,
      index(4) type c.

parameters: p_file type localfile.

at selection-screen on value-request for p_file.
  call function 'KD_GET_FILENAME_ON_F4'
       exporting
            static    = 'X'
       changing
            file_name = p_file.

start-of-selection.

  file_str = p_file.

  call function 'GUI_UPLOAD'
       exporting
            filename                = file_str
       tables
            data_tab                = itab
       exceptions
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            others                  = 17.

* What is the biggest string?
  loop at itab.
    split itab-rec at ';' into table istr.
    describe table istr lines istr_lines.
    if biggest_string &amp;lt; istr_lines.
      biggest_string = istr_lines.
    endif.
  endloop.

* build fieldcat per how many fields are needed from the biggest string.
  do biggest_string times.

    clear wa_it_fldcat.
    index = sy-index.
    concatenate 'FLD' index into wa_it_fldcat-fieldname.
    condense wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'C'.
    wa_it_fldcat-inttype =  'C'.
    wa_it_fldcat-intlen = '10'.
    append wa_it_fldcat to it_fldcat .

  enddo.

* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=&amp;gt;create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table-&amp;gt;* to &amp;lt;dyn_table&amp;gt;.

* Create dynamic work area and assign to FS
  create data new_line like line of &amp;lt;dyn_table&amp;gt;.
  assign new_line-&amp;gt;* to &amp;lt;dyn_wa&amp;gt;.


* Now fill dynamic internal table.
  loop at itab.
    clear istr.  refresh istr.
    split itab at ';' into table istr.

    clear &amp;lt;dyn_wa&amp;gt;.

    loop at istr.

      assign component sy-tabix of structure &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_field&amp;gt;.
      if sy-subrc &amp;lt;&amp;gt; 0.
        exit.
      endif.

      if sy-subrc  = 0 and not istr is initial.
        &amp;lt;dyn_field&amp;gt; = istr.
      endif.

      at last.
        append &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_table&amp;gt;.
      endat.

    endloop.

  endloop.

* Write out data from table.
loop at &amp;lt;dyn_table&amp;gt; into &amp;lt;dyn_wa&amp;gt;.
  do.
    assign component  sy-index  of structure &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_field&amp;gt;.
    if sy-subrc &amp;lt;&amp;gt; 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ &amp;lt;dyn_field&amp;gt;.
    else.
      write: &amp;lt;dyn_field&amp;gt;.
    endif.
  enddo.
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My file looks like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

A;b;c;d;e
f;g;
h;i;j;;k;l;m;n;o;p;
q;r;s;t;u
v;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code worked pretty good for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 14 Nov 2006 17:35:05 GMT</pubDate>
    <dc:creator>RichHeilman</dc:creator>
    <dc:date>2006-11-14T17:35:05Z</dc:date>
    <item>
      <title>CSV file changes dynamically</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689440#M303143</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt; I have CSV file &lt;/P&gt;&lt;P&gt;   a;b;c;d;e;f;g;&lt;/P&gt;&lt;P&gt;   as;sd;fg;gh;jk;kl;mn;;;;;;;;;&lt;/P&gt;&lt;P&gt;   qw;we;rt;yu;ui;io;;;;;;;;;;;;;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;which has to move to itab as following format,so each row has to be dynamic &lt;/P&gt;&lt;P&gt;    a    b    c   d    e   f   g  &lt;/P&gt;&lt;P&gt;    as  sd  df   fg  gh  jk  kl  mn   &lt;/P&gt;&lt;P&gt;    qw we  rt   yu  ui  io &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;above file each row can vary means later file can be like&lt;/P&gt;&lt;P&gt;    a;b;c;d;e;f;g;h;i&lt;/P&gt;&lt;P&gt;   as;sd;fg;gh;jk;kl;mn;;;;;;;;;&lt;/P&gt;&lt;P&gt;   qw;we;rt;yu;ui;io;pq;;;;;;;;;;;;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now ,which has to move to itab as following format,so each row has to be dynamic &lt;/P&gt;&lt;P&gt;    a    b    c   d    e   f   g   h       i&lt;/P&gt;&lt;P&gt;    as  sd  df   fg  gh  jk  kl  mn   &lt;/P&gt;&lt;P&gt;    qw we  rt   yu  ui  io  pq&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know how to handle this situtaion.I am very sure that&lt;/P&gt;&lt;P&gt;we have to Split the row by ; but dynamically and need to move to Dynamic internal&lt;/P&gt;&lt;P&gt;table.&lt;/P&gt;&lt;P&gt; Any suggestion and idea on this would be appreciable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;-Pramod&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Nov 2006 16:37:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689440#M303143</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-11-14T16:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: CSV file changes dynamically</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689441#M303144</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Assuming your delimited data is in table gt_data, this should populate &amp;lt;re_table&amp;gt; the way you would like.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA: BEGIN OF gt_data OCCURS 0,
        line(200),
      END OF gt_data.


DATA: BEGIN OF gt_fields OCCURS 0,
        field(50),
      END OF gt_fields.

DATA: BEGIN OF gt_matrix OCCURS 0,
        cnt(2) TYPE n,
        value(50),
      END OF gt_matrix.

DATA: g_field(10),
      g_index(2)  TYPE n,
      g_lines     TYPE i,
      g_max_lines TYPE i.

* ALV TYPES &amp;amp; DATA
TYPE-POOLS: slis.
DATA: t_alv_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      s_alv_layout   TYPE slis_layout_alv,
      p_alv          TYPE slis_vari VALUE '/DEFAULT',
      t_alv_events   TYPE slis_t_event.

DATA: s_fcat         TYPE lvc_s_fcat,
      t_fcat         TYPE lvc_t_fcat,
      re_table       TYPE REF TO data,
      re_head        TYPE REF TO data.

FIELD-SYMBOLS: &amp;lt;re_table&amp;gt; TYPE table,
               &amp;lt;re_head&amp;gt;,
               &amp;lt;re_field&amp;gt;.
* Determine number of columns required and store data in row/column
* pairings
LOOP AT gt_data.
  SPLIT gt_data AT ';' INTO TABLE gt_fields.

  DESCRIBE TABLE gt_fields LINES g_lines.

  IF g_lines &amp;gt; g_max_lines.
    g_max_lines = g_lines.
  ENDIF.

  MOVE sy-tabix TO gt_matrix-cnt.
  LOOP AT gt_fields.
    MOVE gt_fields-field TO gt_matrix-value.
    APPEND gt_matrix.
  ENDLOOP.
ENDLOOP.

* Define field catalog
DO g_max_lines TIMES.
  MOVE sy-index TO g_index.
  CONCATENATE 'COL' g_index INTO s_fcat-fieldname.

  MOVE: sy-index TO s_fcat-col_pos,
        'C'      TO s_fcat-inttype,
        '10'     TO s_fcat-intlen.
  APPEND s_fcat TO t_fcat.
ENDDO.

* Build dynamic internal table
CALL METHOD cl_alv_table_create=&amp;gt;create_dynamic_table
  EXPORTING
    it_fieldcatalog = t_fcat
  IMPORTING
    ep_table        = re_table.

ASSIGN re_table-&amp;gt;* TO &amp;lt;re_table&amp;gt;.
CREATE DATA re_head LIKE LINE OF &amp;lt;re_table&amp;gt;.
ASSIGN re_head-&amp;gt;* TO &amp;lt;re_head&amp;gt;.

* Populate dynamic table
CLEAR g_index.
LOOP AT gt_matrix.
  ADD 1 TO g_index.
  CONCATENATE 'COL' g_index INTO g_field.
  ASSIGN COMPONENT g_field OF STRUCTURE &amp;lt;re_head&amp;gt; TO &amp;lt;re_field&amp;gt;.
  MOVE gt_matrix-value TO &amp;lt;re_field&amp;gt;.

  AT END OF cnt.
    APPEND &amp;lt;re_head&amp;gt; TO &amp;lt;re_table&amp;gt;.
    CLEAR g_index.
  ENDAT.
ENDLOOP.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Nov 2006 17:28:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689441#M303144</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-11-14T17:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: CSV file changes dynamically</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689442#M303145</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looks like Michael and I were thinking the same.  You need to find the biggest record length first, then build your dynamica internal table, then fill it with data.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

report zrich_0001.

types: begin of ttab,
       rec(1000) type c,
       end of ttab.

types: begin of tdat,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end of tdat.

data: itab type table of ttab with header line.
data: idat type table of tdat with header line.

type-pools : abap, slis.

field-symbols: &amp;lt;dyn_table&amp;gt; type standard table,
               &amp;lt;dyn_wa&amp;gt;,
               &amp;lt;dyn_field&amp;gt;.

data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat,
      it_fldcat type lvc_t_fcat..

data: file_str type string.

data: istr type table of string with header line.

data: istr_lines type i,
      biggest_string type i,
      index(4) type c.

parameters: p_file type localfile.

at selection-screen on value-request for p_file.
  call function 'KD_GET_FILENAME_ON_F4'
       exporting
            static    = 'X'
       changing
            file_name = p_file.

start-of-selection.

  file_str = p_file.

  call function 'GUI_UPLOAD'
       exporting
            filename                = file_str
       tables
            data_tab                = itab
       exceptions
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            others                  = 17.

* What is the biggest string?
  loop at itab.
    split itab-rec at ';' into table istr.
    describe table istr lines istr_lines.
    if biggest_string &amp;lt; istr_lines.
      biggest_string = istr_lines.
    endif.
  endloop.

* build fieldcat per how many fields are needed from the biggest string.
  do biggest_string times.

    clear wa_it_fldcat.
    index = sy-index.
    concatenate 'FLD' index into wa_it_fldcat-fieldname.
    condense wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'C'.
    wa_it_fldcat-inttype =  'C'.
    wa_it_fldcat-intlen = '10'.
    append wa_it_fldcat to it_fldcat .

  enddo.

* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=&amp;gt;create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table-&amp;gt;* to &amp;lt;dyn_table&amp;gt;.

* Create dynamic work area and assign to FS
  create data new_line like line of &amp;lt;dyn_table&amp;gt;.
  assign new_line-&amp;gt;* to &amp;lt;dyn_wa&amp;gt;.


* Now fill dynamic internal table.
  loop at itab.
    clear istr.  refresh istr.
    split itab at ';' into table istr.

    clear &amp;lt;dyn_wa&amp;gt;.

    loop at istr.

      assign component sy-tabix of structure &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_field&amp;gt;.
      if sy-subrc &amp;lt;&amp;gt; 0.
        exit.
      endif.

      if sy-subrc  = 0 and not istr is initial.
        &amp;lt;dyn_field&amp;gt; = istr.
      endif.

      at last.
        append &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_table&amp;gt;.
      endat.

    endloop.

  endloop.

* Write out data from table.
loop at &amp;lt;dyn_table&amp;gt; into &amp;lt;dyn_wa&amp;gt;.
  do.
    assign component  sy-index  of structure &amp;lt;dyn_wa&amp;gt; to &amp;lt;dyn_field&amp;gt;.
    if sy-subrc &amp;lt;&amp;gt; 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ &amp;lt;dyn_field&amp;gt;.
    else.
      write: &amp;lt;dyn_field&amp;gt;.
    endif.
  enddo.
endloop.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My file looks like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

A;b;c;d;e
f;g;
h;i;j;;k;l;m;n;o;p;
q;r;s;t;u
v;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code worked pretty good for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Nov 2006 17:35:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/csv-file-changes-dynamically/m-p/1689442#M303145</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2006-11-14T17:35:05Z</dc:date>
    </item>
  </channel>
</rss>

