10-11-2007 1:02 AM
Gurus,
I had written a program that uploads data from an excel file into the SAP custom tables..This FM works but we have to manually remove the headers out of the excel table..and insert a column for client within the excel file...so overall before loading the table i have to delete the heading s row and insert a extra column for client and fill it...
Now how shud i change my code that it automatically does this....is it possible...is there any FM that i have to use....Please give some inputs....
Right now the main FM's that i am using are: LVC_FIELDCATALOG_MERGE,METHOD cl_alv_table_create=>create_dynamic_table,TEXT_CONVERT_XLS_TO_SAP....
Please suggest...
Thnaks
Sam
10-11-2007 1:13 AM
Hi Sam,
The better way is just remove the header as you are doing now. And regarding adding the client column. Why do you need extra client column.
You can get the current client using sy-mandt.
Regards,
Atish
10-11-2007 1:13 AM
Hello Sam
The function module <b>TEXT_CONVERT_CSV_TO_SAP</b> will automatically convert a .csv file into an itab data having the corresponding SAP DDIC structure.
If you set I_LINE_HEADER = 'X' the fm knows the file has a <i>header line</i> an will remove it.
Instead of thinking about dynamic itab creating do the most obvious and simplest thing: add an empty (client) column to your Excel file. Anything else is nonsense and a waste of time and money.
Regards
Uwe
10-11-2007 1:21 AM
Hey Uwe,
Thanks for the reply..
So u r saying instead of TEXT_CONVERT_XLS_TO_SAP i shud use TEXT_CONVERT_CSV_TO_SAP...and in this FM if i set I_LINE_HEADER = 'X' then the header line would be deleted..but will this let me upload the excel file...?
Then about adding the extra column for MANDT(Client) what shud i do....The excel files are coming from the conversion team and they want that without any effort of making changes in the excel file the file shud be uploaded in our SAP custom tables...They dont want to add any column and make any changes in the excel file.....What code shud i do so that it solves my purpose....please explain...i am still a beginner in ABAP...
Thanks
Sam
10-11-2007 1:13 AM
Hi Sam,
The better way is just remove the header as you are doing now. And regarding adding the client column. Why do you need extra client column.
You can get the current client using sy-mandt.
Regards,
Atish
10-11-2007 1:22 AM
Hey Atish,
Thanks for the reply....
How shud i code so that it picks up the client by itself and adds to the table....how shud i use sy-mandt...
Thanks
Sam
10-11-2007 1:43 AM
Hi Sam,
Can you just paste your code so that I can tell you.
As the current client is always SY-MANDT so no need to get this detail in the file.
While filling the internal table you can just fill sy-mandt for that column.
Regards,
Atish
10-11-2007 2:21 AM
Sure...Atish...
Please look at the code and tell me where shud i include sy-mandt....i am using couple of FM's in my code.....
FORM create_dynamic_inttab USING rv_table_name TYPE tabname.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = rv_table_name
CHANGING
ct_fieldcat = t_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE e003 WITH 'The Interface is inconsistent'(025).
WHEN OTHERS.
MESSAGE e003 WITH 'Program Error'(026).
ENDCASE.
ENDIF.
* Create dynamic internal table and assign to Field Symbol
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fcat
IMPORTING
ep_table = t_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE e003 WITH 'Subpool Directory Full'(027).
WHEN OTHERS.
MESSAGE e003 WITH 'Error while creating dynamic table'(028).
ENDCASE.
ELSE.
ASSIGN t_dyn_table->* TO <f_dyn_table>.
ASSIGN t_dyn_table->* TO <f_dyn_table1>.
* Create dynamic work area and assign to FS
CREATE DATA s_dyn_line LIKE LINE OF <f_dyn_table>.
ASSIGN s_dyn_line->* TO <f_dyn_wa>.
ENDIF.
ENDFORM. " create_dynamic_inttab
*&---------------------------------------------------------------------*
*& Form get_data_from_excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_FILENM text
*----------------------------------------------------------------------*
FORM get_data_from_excel USING rp_filenm TYPE dxlpath.
DATA: v_filenm TYPE localfile.
v_filenm = rp_filenm.
* Upload the excel document into internal table
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_tab_raw_data = t_raw_data
i_filename = v_filenm
I_LINE_HEADER = 'X'
TABLES
i_tab_converted_data = <f_dyn_table>
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE i003 WITH 'Close the file if it is open'(029).
LEAVE LIST-PROCESSING.
WHEN OTHERS.
MESSAGE i003 WITH 'Error in Reading File'(030).
LEAVE LIST-PROCESSING.
ENDCASE.
ENDIF.
ENDFORM. " get_data_from_excel
*&---------------------------------------------------------------------*
*& Form modify_db_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_V_TABLE_NAME text
*----------------------------------------------------------------------*
FORM modify_db_table USING rv_table_name TYPE tabname.
CASE rv_table_name.
WHEN 'ZX_NAME'.
MODIFY ZX_NAME FROM TABLE <f_dyn_table>.
WHEN'ZX_ADDRESS''.
MODIFY ZX_ADDRESS FROM TABLE <f_dyn_table>.
It is just part of the code....
Thanks
Sam
10-11-2007 2:30 AM
Hi Sam,
As you are using dynamic table so the best and simplest solution for you is
1. Delete the first header line from excel sheet
2 Add extra blank column for the client . Do not fill anything
3. Add sy-mandt to the blank column in the below routine.
&----
*& Form modify_db_table
&----
text
----
-->P_V_TABLE_NAME text
----
FORM modify_db_table USING rv_table_name TYPE tabname.
CASE rv_table_name.
WHEN 'ZX_NAME'.
MODIFY ZX_NAME FROM TABLE <f_dyn_table>.
WHEN'ZX_ADDRESS''.
MODIFY ZX_ADDRESS FROM TABLE <f_dyn_table>.
regards,
Atish
10-11-2007 3:13 AM
Hey Atish,
I have to find a solution for this buddy...Atleast for the extra column that we need to add..
I debugged the code and i realised that the field catalog has a field MANDT...
its only when we call the TEXT_CONVERT_XLS_TO_SAP..FM that it doesn't get populated...
Guys, Kindly help me on this...Any inputs for me...
Cheers:
Sam
Message was edited by:
Sam williams
10-11-2007 10:28 PM
Hey gurus,
Kindly provide me some inputs on solving this issue...I was able to delete the header line..but i am not able to insert a column for MANDT automatically...I manulaly have to create a additional column in the excel file for MANDT...
Please help me solve this issue....
Cheers:
Sam
10-12-2007 12:00 AM
Why not just remove MANDT from the field catalog that you are using to create the dynamic table?
You may need to have two images of the table: one with and one without MANDT.
The first (with MANDT) would be needed for your MODIFY at the end.
The second would be used to load the table from XLS.
You would then copy the data + SY-MANDT into the original.
It will be less efficient, but it should work.
After this:
FORM create_dynamic_inttab USING rv_table_name TYPE tabname.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = rv_table_name
CHANGING
ct_fieldcat = t_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE e003 WITH 'The Interface is inconsistent'(025).
WHEN OTHERS.
MESSAGE e003 WITH 'Program Error'(026).
ENDCASE.
ENDIF.
Put this:
t_fcat2 = t_fcat.
DELETE t_fcat2 WHERE Fieldname = 'MANDT'.
* Create original dynamic internal table and assign to Field Symbol with MANDT
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fcat
IMPORTING
ep_table = t_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE e003 WITH 'Subpool Directory Full'(027).
WHEN OTHERS.
MESSAGE e003 WITH 'Error while creating dynamic table'(028).
ENDCASE.
ELSE.
ASSIGN t_dyn_table->* TO <f_dyn_table>.
ASSIGN t_dyn_table->* TO <f_dyn_table1>.
* Create dynamic work area and assign to FS
CREATE DATA s_dyn_line LIKE LINE OF <f_dyn_table>.
ASSIGN s_dyn_line->* TO <f_dyn_wa>.
ENDIF.
* Create second dynamic internal table and assign to Field Symbol without MANDT
* all of the field symbols etc are duplicated for table 2, including work areas etc
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fcat2
IMPORTING
ep_table = t_dyn_table2
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE e003 WITH 'Subpool Directory Full'(027).
WHEN OTHERS.
MESSAGE e003 WITH 'Error while creating dynamic table'(028).
ENDCASE.
ELSE.
ASSIGN t_dyn_table2->* TO <f_dyn_table2>.
ASSIGN t_dyn_table2->* TO <f_dyn_table2_1>.
* Create dynamic work area and assign to FS
CREATE DATA s_dyn_line2 LIKE LINE OF <f_dyn_table2>.
ASSIGN s_dyn_line2->* TO <f_dyn_wa2>.
ENDIF.
ENDFORM.
Your XLS conversion code uses the table without MANDT.
You will have to subsequently move the data to the original table, and load it with sy-mandt as others have suggested.
See below:
FORM get_data_from_excel USING rp_filenm TYPE dxlpath.
DATA: v_filenm TYPE localfile.
v_filenm = rp_filenm.
* Upload the excel document into internal table
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_tab_raw_data = t_raw_data
i_filename = v_filenm
I_LINE_HEADER = 'X'
TABLES
i_tab_converted_data = <f_dyn_table2>
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE i003 WITH 'Close the file if it is open'(029).
LEAVE LIST-PROCESSING.
WHEN OTHERS.
MESSAGE i003 WITH 'Error in Reading File'(030).
LEAVE LIST-PROCESSING.
ENDCASE.
ENDIF.
* loop at table 2 and move to original table, adding MANDT
LOOP AT <f_dyn_table2> ASSIGNING <f_dyn_wa2>.
MOVE-CORRESPONDING <f_dyn_wa2> to <f_dyn_wa>.
<f_dyn_wa>-MANDT = SY-MANDT.
APPEND <f_dyn_wa> to <f_dyn_table>.
ENDLOOP.
ENDFORM. " get_data_from_excel
If you do this, it will no longer be an expected part of the dynamic table for the XLS conversion and the problem will go away.
It's not pretty, and you may have to clean up my code a bit.
Good luck
Brian
Edit code
Message was edited by:
Brian Sammond
10-12-2007 1:00 AM
Brian, Thanks alot for the reply...Let me try this and see if it works for me....Will definitely come and award u the points...
Thanks a lot for the extra effort that u put in to help me....
Sam
10-12-2007 1:18 AM
Brian It gives me a erro sying: data object <f_dyn_wa> has no structure and no component called MANDT...
What can i do to solve it....?
10-12-2007 5:03 PM
There is a simple fix for this.
The syntax checker cannot determine the structure statically, since the table is dynamic.
So we have to do it indirecty.
define a local structure with a single field MANDT, and move sy-mandt to that.
Next, do a move-corresponding from that structure. See below:
FORM get_data_from_excel USING rp_filenm TYPE dxlpath.
DATA: v_filenm TYPE localfile.
v_filenm = rp_filenm.
* define MANDT structure
TYPES: BEGIN OF ty_mandt_struct,
mandt TYPE mandt,
END OF ty_mandt_struct.
DATA: s_mandt TYPE ty_mandt_struct.
* Upload the excel document into internal table
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_tab_raw_data = t_raw_data
i_filename = v_filenm
I_LINE_HEADER = 'X'
TABLES
i_tab_converted_data = <f_dyn_table2>
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
MESSAGE i003 WITH 'Close the file if it is open'(029).
LEAVE LIST-PROCESSING.
WHEN OTHERS.
MESSAGE i003 WITH 'Error in Reading File'(030).
LEAVE LIST-PROCESSING.
ENDCASE.
ENDIF.
* loop at table 2 and move to original table, adding MANDT
*load MANDT into structure, this can be done one-time outside of loop
s_mandt-mandt = sy-mandt.
MOVE-CORRESPONDING s_mandt TO <f_dyn_wa>.
LOOP AT <f_dyn_table2> ASSIGNING <f_dyn_wa2>.
MOVE-CORRESPONDING <f_dyn_wa2> to <f_dyn_wa>.
APPEND <f_dyn_wa> TO <f_dyn_table>.
ENDLOOP.
ENDFORM. " get_data_from_excel
This fixes the problem.
Good luck
Brian
Add introductory comments, put code tags in, move MANDT logic
Message was edited by:
Brian Sammond
10-12-2007 11:23 PM
Thanks Brian....It works now....
Really appreciate your help....
Will come to you in case of any questions...
Thanks
Sam