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: 

Uploading data from excel file to a dynamically created internal table

Former Member
0 Kudos

Hi,

I have a requirement where i have to upload data from an excel file into a database table. I would be able to determine the structure of the table only at runtime based on the user input.. so i have created an internal table dynamically.

Could you please tell me if its possible to upload data from an excel file to the dynamically created internal table using any function modules?

I thought of doing this by declaring a generic internal table of one field and then uploading the *.csv file into it and then splitting it based on "," and then assigning it to the field symbol referencing the internal table.. but my file length exceeds 132 characters and i'm only able to get data of lenght 132 char's in my internal table ( generic one).

Could anyone please show me a way around this.

Thanks in advance,

Harsha

6 REPLIES 6

Former Member
0 Kudos

try out FM 'ALSM_EXCEL_TO_INTERNAL_TABLE'

Regards,

Bikash

0 Kudos

Please see the following example program. This should show you want you need to do.




report zrich_0002.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.

data: it_fldcat type lvc_t_fcat,
      wa_it_fldcat type lvc_s_fcat.

type-pools : abap.

data: new_table type ref to data,
      new_line  type ref to data.

data: xcel type table of alsmex_tabline with header line.

selection-screen begin of block b1 with frame title text .
parameters: p_file type  rlgrap-filename default 'c:Test.csv'.
parameters: p_flds type i.
selection-screen end of block b1.

start-of-selection.

* Add X number of fields to the dynamic itab cataelog
  do p_flds times.
    clear wa_it_fldcat.
    wa_it_fldcat-fieldname = sy-index.
    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=>create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
  create data new_line like line of <dyn_table>.
  assign new_line->* to <dyn_wa>.

* Upload the excel
  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
       exporting
            filename                = p_file
            i_begin_col             = '1'
            i_begin_row             = '1'
            i_end_col               = '200'
            i_end_row               = '5000'
       tables
            intern                  = xcel
       exceptions
            inconsistent_parameters = 1
            upload_ole              = 2
            others                  = 3.

* Reformt to dynamic internal table
  loop at xcel.
    assign component xcel-col of structure <dyn_wa> to <dyn_field>.
    if sy-subrc = 0.
     <dyn_field> = xcel-value.
    endif.

    at end of row.
      append <dyn_wa> to <dyn_table>.
      clear <dyn_wa>.
    endat.
  endloop.

* Write out data from table.
  loop at <dyn_table> into <dyn_wa>.
    do.
      assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
      if sy-subrc <> 0.
        exit.
      endif.
      if sy-index = 1.
        write:/ <dyn_field>.
      else.
        write: <dyn_field>.
      endif.
    enddo.
  endloop.

Regards,

Rich Heilman

0 Kudos

Hi,

I donot have the function module 'ALSM_EXCEL_TO_INTERNAL_TABLE' in my system.

I've checked it in se37.

Can't i use GUI_UPLOAD for uploading data from an excel file in *.csv format to an internal table.

Thanks,

Harsha

0 Kudos

Sure, check this out.



report zrich_0002.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.

data: it_fldcat type lvc_t_fcat,
      wa_it_fldcat type lvc_s_fcat.

type-pools : abap.

data: new_table type ref to data,
      new_line  type ref to data.

data: iflat type table of string.
data: xflat type string.


  data: irec type table of string with header line.
  data: tabix type sy-tabix.

data: file type string.

selection-screen begin of block b1 with frame title text .
parameters: p_file type  rlgrap-filename default 'c:Test.csv'.
parameters: p_flds type i.
selection-screen end of block b1.

start-of-selection.

* Add X number of fields to the dynamic itab cataelog
  do p_flds times.
    clear wa_it_fldcat.
    wa_it_fldcat-fieldname = sy-index.
    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=>create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
  create data new_line like line of <dyn_table>.
  assign new_line->* to <dyn_wa>.


  file = p_file.

  call method cl_gui_frontend_services=>gui_upload
    exporting
      filename                = file
    changing
      data_tab                = iflat
    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.


  loop at iflat into xflat.
    clear irec. refresh irec.
    split xflat at ',' into table irec.
    loop at irec.
      tabix = sy-tabix.
      assign component tabix of structure <dyn_wa> to <dyn_field>.
      <dyn_field> = irec.
    endloop.
    append <dyn_wa> to <dyn_table>.
  endloop.


* Write out data from table.
  loop at <dyn_table> into <dyn_wa>.
    do.
      assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
      if sy-subrc <> 0.
        exit.
      endif.
      if sy-index = 1.
        write:/ <dyn_field>.
      else.
        write: <dyn_field>.
      endif.
    enddo.
  endloop.

Regards,

Rich Heilman

Former Member
0 Kudos

Try this fm -

call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'

exporting

filename = p_path

i_begin_col = 1

i_begin_row = 2

i_end_col = 70

i_end_row = 10000

tables

intern = i_data

exceptions

inconsistent_parameters = 1

upload_ole = 2

others = 3.

I_DATA contains data. You need to popualte final itnernal table using this.

I_DATA will contain details for each cell - respective row, column and value of respective cell.

This should help you out.

Former Member
0 Kudos

This message was moderated.