Friday, October 16, 2009

Excel to ABAP

It's cold here today were getting the first snow of the season. I am having a diffuculty coming up with anything to write about besides the fact that I hate snow. I know what your think why for heaven sake do you live in Conklin, Ny then. Well I moved up here in 2004 to do a project and I ended up getting involved with a women here who has the two greatest daughters in the world. So now I can't leave at least for a few years. :> But I can't complain to much it's given me a chance to get involved in this SAP project which has been great.

Ok now that you know more about me then you should lets get down to the reason your reading this blog to begin with.

How to get an excel spread sheet into SAP.
Well it's really a simple procedure.

First you must create an internal table to hold the spread sheet.
data iexcel type table of alsmex_tabline with header line.
Now then it's a simple call to function module 'ALSM_EXCEL_TO_INTERNAL_TABLE'.
Here is the exact call.
call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                = p_fname
      i_begin_col             = 1
      i_begin_row             = 1
      i_end_col               = 8
      i_end_row               = 9000
    tables
      intern                  = iexcel
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
The P_Fname is the actual excel Spread Sheet on your PC.
The following code is how the data gets read by ABAP.
loop at iexcel.
  if iexcel-col = '0001'.
    artab-bukrs = iexcel-value.
  endif.
  at end of row.
    append artab.
    clear artab.
  endat.   
  clear iexcel.
endloop.
In the above example I only have one column defined and it has the Company code you would of course define as many columns as you need.
The above code snippet sets up a loop to read thru all records in the internal table. It then checks to see what column the read returned. Then at the end of column check. Add logic to check to see if the row is complete or not. If it is then in this example we will write out a internal table then get the next record.

Well that's it I think I am going to go back to complaining about the snow now.





Reblog this post [with Zemanta]