2007 Oct 01 4:19 AM
hi,
i have a custom created Z table into which i want to import some data from an excel file. which function can i use to do so because SE16 allows me to insert data only one by one via a data entry screen. this is time consuming. i want to import data from excel file so that its faster.
2007 Oct 01 4:26 AM
Use the FM: ALSM_EXCEL_TO_INTERNAL_TABLE to upload data from excel sheet to internal table.
After getting the data into internal table, update the Z table using the modify statement.
Sample code of how to use the above function is
parameters : p_ifname type rlgrap-filename.
data : it_data type table of alsmex_tabline initial size 0,
is_data type alsmex_tabline.
types : begin of ty_tab,
kunnr type bsid-kunnr, " added on 31-07-2007
zuonr type bsid-zuonr,
end of ty_tab.
data : it_tab type table of ty_tab initial size 0,
is_tab type ty_tab.
* If Input file name is not initial.
if not p_ifname is initial.
* Upload EXCEL data into internal table
call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
exporting
filename = p_ifname
i_begin_col = 1
i_begin_row = 1
i_end_col = 256
i_end_row = 65356
tables
intern = it_data
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endif.
* Append EXCEL Data into a internal table
loop at it_data into is_data.
at new row.
clear is_tab.
endat.
if is_data-col = '001'.
move is_data-value to is_tab-kunnr.
endif.
if is_data-col = '002'.
move is_data-value to is_tab-zuonr.
endif.
at end of row.
append is_tab to it_tab.
endat.
clear : is_data.
endloop.
Regards
Gopi
2007 Oct 01 4:23 AM
Hi Raja..
you have to Call the FM
<b>GUI_UPLOAD</b>
This function module is used to Upload the Data from Presentation Server file (Excel, Notepad) to Internal table.
From the internal table you can insert the records into DB table using.
<b>INSERT <DBTABLE> FROM TABLE <ITAB> ACCEPTING DUPLICATE KEYS.</b>
reward if Helpful.
2007 Oct 01 4:24 AM
Hi,
1. Goto SE38 transaction
2. Create a report program to Upload Data
3. Use FM GUI_UPLOAD to upload the data into internal table
4. Use INSERT Statement to Insert the records from internal table in Z Table.
INSERT ZTABLE from ITAB.
COMMIT WORK.
Best regards,
Prashant
2007 Oct 01 4:26 AM
Use the FM: ALSM_EXCEL_TO_INTERNAL_TABLE to upload data from excel sheet to internal table.
After getting the data into internal table, update the Z table using the modify statement.
Sample code of how to use the above function is
parameters : p_ifname type rlgrap-filename.
data : it_data type table of alsmex_tabline initial size 0,
is_data type alsmex_tabline.
types : begin of ty_tab,
kunnr type bsid-kunnr, " added on 31-07-2007
zuonr type bsid-zuonr,
end of ty_tab.
data : it_tab type table of ty_tab initial size 0,
is_tab type ty_tab.
* If Input file name is not initial.
if not p_ifname is initial.
* Upload EXCEL data into internal table
call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
exporting
filename = p_ifname
i_begin_col = 1
i_begin_row = 1
i_end_col = 256
i_end_row = 65356
tables
intern = it_data
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endif.
* Append EXCEL Data into a internal table
loop at it_data into is_data.
at new row.
clear is_tab.
endat.
if is_data-col = '001'.
move is_data-value to is_tab-kunnr.
endif.
if is_data-col = '002'.
move is_data-value to is_tab-zuonr.
endif.
at end of row.
append is_tab to it_tab.
endat.
clear : is_data.
endloop.
Regards
Gopi
2007 Oct 01 4:29 AM
Hi Raja,
You have to write a Z (custom) ABAP program to do this. It should be an easy program to write - follow the steps below.
- upload the XLS (file) into itab1 (use FM GUI_UPLOAD)
- loop at itab1 and pass the relevant fields to itab2 whose structure is like Z table
- append itab2
- update Z table from itab2 (one-for-all or loop at itab2 to update one row at a time - Read documentation on UPDATE/INSERT statement)
- use Commit Work after all lines are written to Z table. (outside the loop of itab2)
Don't forget to reward points.
Cheers,
Sougata.
2007 Oct 01 5:28 AM
HI,
this program uploads data from excel and modifies ztable,(inserts records into ztable), check this and modify according to ur requirement)
This program uploads material number from excel sheet and does
******modifications to material number if required by the user
******and updates the table zmatnr with new material against the old material number
*************************************************************************
REPORT zmat_no message-id zebg.
TYPE-POOLS truxs.
TABLES:zmatnr.
DATA : itab LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.
DATA row LIKE alsmex_tabline-row.
data : g_matnr like mara-matnr.
data : count type i.
data : itab_count type i.
data : gi_final like zmatnr occurs 0 with header line.
*data : begin of gi_final occurs 0,
mat_old like mara-matnr,
mat_new like mara-matnr,
end of gi_final.
***********************Selection Screen*************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETER : pfname LIKE rlgrap-filename OBLIGATORY.
select-options : records for count.
SELECTION-SCREEN END OF BLOCK b1.
*******************************************************************
*********************At Selection Screen*************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfname.
PERFORM search.
*******************************************************************
START-OF-SELECTION.
perform process.
form process.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = pfname
i_begin_col = 1
i_begin_row = 2
i_end_col = 12
i_end_row = 65000
TABLES
intern = itab
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
describe table itab lines itab_count.
row = 1.
loop at itab.
if itab-row <> row.
append gi_final.
clear gi_final.
endif.
case itab-col.
when '1'.
CLEAR G_MATNR.
gi_final-OLD_MATNR = itab-value.
CONCATENATE 'NEW' gi_final-old_matnr INTO itab-value.
gi_final-new_MATNR = itab-value.
endcase.
row = itab-row.
append gi_final.
clear gi_final.
endloop.
CALL FUNCTION 'PROGRESS_INDICATOR'
EXPORTING
I_TEXT = 'File Has Been Successfully Uploaded from Workstation ' .
if not gi_final[] is initial.
if not records-low is initial .
if not records-high is initial.
records-high = records-high + 1.
DESCRIBE TABLE gi_final LINES count.
IF records-high < count.
DELETE gi_final FROM records-high TO count.
ENDIF.
IF records-low <> 1.
IF records-low <> 0.
DELETE gi_final FROM 1 TO records-low.
ENDIF.
ENDIF.
endif.
endif.
endif.
IF NOT GI_FINAL[] IS INITIAL.
CALL FUNCTION 'PROGRESS_INDICATOR'
EXPORTING
I_TEXT = 'Processing zmatnr table'
I_OUTPUT_IMMEDIATELY = 'X'.
if itab_count <> count.
*
message i000 with 'records are not matching'.
*
exit.
*
else.
modify zmatnr from table gi_final.
message i000 with 'data base table modified successfully'.
endif.
endif.
endform.
&----
*& Form search
&----
text
----
--> p1 text
<-- p2 text
----
FORM search .
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
static = 'X'
CHANGING
file_name = pfname.
ENDFORM. " search
regards
siva
2007 Oct 01 6:09 AM
hi guys,
this is very weird that such a popular system like SAP still does not have a built in ready made function to import data into a table. this is very sad.
2007 Oct 01 6:29 AM
Well, why don't you write one, i.e. instead of writing the logic supplied by us as a program may be you can write it as a FM for someone else to use in your company in the future.
If SAP did everything then programmers like us would have nothing to do!
2007 Oct 01 6:55 AM
hi,
you will first have to use 'alsm_excel_to_internal_table' method, which wil transfer data from excel sheet to internal table, thn u cn move the required data to the corresponding z table.
2024 May 05 11:45 AM - edited 2024 May 05 3:00 PM
REPORT csv.
* Reading data from Microsoft CSV *
PARAMETERS: p_fname TYPE string OBLIGATORY,
tb_name TYPE tabname16 OBLIGATORY,
date_f(10) TYPE c MODIF ID abc DEFAULT 'MM/DD/YYYY', "date format
deli(1) TYPE c MODIF ID abc DEFAULT ';', "delimiter
dec_f TYPE string MODIF ID abc DEFAULT '1,234,567.89', "decimal format
w_h_line AS CHECKBOX MODIF ID abc DEFAULT 'X'. "with_header_line
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-group1 = 'ABC'.
screen-input = 0.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
PERFORM file_open_dialog CHANGING p_fname.
START-OF-SELECTION.
DATA: lf_line TYPE string.
DATA: lt_config TYPE mmpur_mgrp2pgrp_t,
gt_itab TYPE truxs_t_text_data,
ls_file_line LIKE LINE OF gt_itab,
lv_filename TYPE string,
lv_filename_usr TYPE localfile,
l_table_count TYPE i,
data_ref TYPE REF TO data.
DATA : d_entry TYPE REF TO data.
FIELD-SYMBOLS:
<t_entry> TYPE table,
<line> TYPE any.
CREATE DATA d_entry TYPE STANDARD TABLE OF (tb_name).
ASSIGN d_entry->* TO <t_entry>.
FIELD-SYMBOLS:
<line2> TYPE any .
CREATE DATA data_ref TYPE (tb_name).
ASSIGN data_ref->* TO <line2>.
DATA:tb_data TYPE STANDARD TABLE OF str_table,
gt_split TYPE TABLE OF string,
gt_variable TYPE TABLE OF string,
tb_header TYPE TABLE OF string,
it_data TYPE STANDARD TABLE OF data_ref,
count TYPE i,
index TYPE i VALUE 1,
header TYPE string,
value TYPE string.
DATA : variable TYPE string,
in_month TYPE string,
in_day TYPE string,
out_month(2),
out_day(2).
DATA : variable_tmp LIKE variable,
TYPE_of_value LIKE dd01v-datatype,
is_decimal(1) TYPE c,
is_negative(1) TYPE c.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = p_fname
filetype = 'ASC'
has_field_separator = deli
read_by_line = 'X'
TABLES
data_tab = gt_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.
IF sy-subrc NE 0.
RETURN.
ENDIF.
IF lines( gt_itab ) = 0 .
WRITE / ` Excel file is empty!`.
RETURN.
ENDIF.
LOOP AT gt_itab INTO ls_file_line.
lf_line = ls_file_line.
* split record into fields
IF sy-tabix = 1.
SPLIT ls_file_line AT deli INTO TABLE tb_header.
DESCRIBE TABLE tb_header LINES count.
ELSE.
"SPLIT ls_file_line AT deli INTO TABLE gt_split.
CALL FUNCTION 'RSDS_CONVERT_CSV'
EXPORTING
i_data_sep = deli
i_esc_char = '"'
i_record = lf_line
i_field_count = 9999
IMPORTING
e_t_data = gt_split
EXCEPTIONS
escape_no_close = 1
escape_improper = 2
conversion_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE 'Error importing file' TYPE 'E'.
ENDIF.
index = 1.
WHILE index <= count.
READ TABLE gt_split INDEX index TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
header = tb_header[ index ].
CONDENSE header.
value = gt_split[ index ].
CONDENSE value.
IF value IS NOT INITIAL.
TRY.
variable = CONV string( value ).
" check date format MM/DD/YYYY and conv it
FIND REGEX '^\d{1,2}\/\d{1,2}\/\d{4}$' IN variable .
IF sy-subrc = 0.
SPLIT variable AT '/' INTO TABLE gt_variable.
in_day = gt_variable[ 1 ].
in_month = gt_variable[ 2 ].
out_month = |{ in_month ALPHA = IN }|.
out_day = |{ in_day ALPHA = IN }|.
DATA(date_formated) = |{ gt_variable[ 3 ] }| & |{ out_day }| & |{ out_month }|.
value = CONV date( date_formated ).
ENDIF.
" check date format MM/DD/YYYY and conv it
"check value if is number
CLEAR : variable_tmp , TYPE_of_value , is_decimal.
variable_tmp = variable .
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN variable_tmp WITH space.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN variable_tmp WITH space.
IF sy-subrc = 0.
is_decimal = 'X'.
ENDIF.
REPLACE ALL OCCURRENCES OF SUBSTRING '-' IN variable_tmp WITH space.
IF sy-subrc = 0.
is_negative = 'X'.
ENDIF.
CALL FUNCTION 'NUMERIC_CHECK'
EXPORTING
string_in = variable_tmp
IMPORTING
string_out = variable_tmp
htype = TYPE_of_value.
IF TYPE_of_value = 'NUMC'.
IF is_decimal = 'X'.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH space.
IF is_negative <> 'X'.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN value WITH ','.
ENDIF.
ELSE.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH space.
ENDIF.
ENDIF.
"check value if is number
<line2>-(header) = value.
IF <line2>-(header) <> value.
WRITE / ` ERROR : line` && ` => ` && index && ` column` && ` => ` && header.
WRITE / ` Error during registration of characteristic ` && value && ` to data element ` && <line2>-(header) .
RETURN.
ENDIF.
CATCH cx_root.
WRITE / ` ERROR : line` && ` => ` && index && ` column` && ` => ` && header.
WRITE / ` ERROR : Incompatible data element`.
RETURN.
ENDTRY.
ENDIF.
ENDIF.
index = index + 1.
ENDWHILE.
APPEND INITIAL LINE TO <t_entry> ASSIGNING FIELD-SYMBOL(<line1>).
<line1> = CORRESPONDING #( <line2> ).
CLEAR : <line2>.
ENDIF.
ENDLOOP.
LOOP AT <t_entry> ASSIGNING <line>.
INSERT (tb_name) FROM <line>.
IF sy-subrc = 0.
l_table_count = l_table_count + 1 .
ENDIF.
ENDLOOP.
COMMIT WORK AND WAIT.
WRITE / ' Query OK, ' && l_table_count && ' rows affected' .
FORM file_open_dialog CHANGING cp_fname.
DATA: lt_filetable TYPE filetable,
lv_rc TYPE i.
cl_gui_frontend_services=>file_open_dialog(
EXPORTING
default_extension = 'csv'
file_filter = '(*.csv)|*.csv|'
multiselection = abap_false
CHANGING
file_table = lt_filetable
rc = lv_rc
EXCEPTIONS
OTHERS = 1 ).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
TRY.
cp_fname = lt_filetable[ 1 ]-filename.
CATCH cx_root.
ENDTRY.
ENDFORM.
"Reading data from Microsoft CSV *