Function module to export local table to excel - abap

I am working on a program in Business Warehouse that allows you to map out all of your process chains by following the hierarchy of parent to sub-chains by using the rspcchain table. As of right now I have it printing the output to the screen, but would like to export this output to excel instead. I have been unable to find a function module that serves this purpose, so any help would be greatly appreciated
note - after learning about the SALV classes available I changed the code to display the table differently.
REPORT Z_PC_VARIANT_MAPPING.
*Declaring types and variables
TYPES: BEGIN OF t_chains,
chain_id LIKE rspcchain-chain_id,
variant LIKE rspcchain-variante,
END OF t_chains.
DATA: lt_rspcchain TYPE STANDARD TABLE OF t_chains,
lwa_rspcchain TYPE t_chains,
o_alv TYPE REF TO cl_salv_table,
lx_msg TYPE REF TO cx_salv_msg.
TABLES: rspcchain.
*selection screen setup
SELECT-OPTIONS chain_id FOR rspcchain-chain_id.
SELECT-OPTIONS type FOR rspcchain-type.
*filling local table
SELECT chain_id variante
FROM rspcchain INTO TABLE lt_rspcchain
WHERE chain_id IN chain_id AND
type IN type AND
objvers = 'A'.
*original code to test printing output on screen
*LOOP AT lt_rspcchain INTO lwa_rspcchain.
* skip.
* WRITE lwa_rspcchain-chain_id.
* WRITE lwa_rspcchain-variant.
*ENDLOOP.
IF sy-subrc NE 0. "sy-subrc = return code
WRITE 'Data not found'.
ENDIF.
*loading data from local table into alv object table
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = lt_rspcchain ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
*calling display method to display table
o_alv->display( ).

You can use the SALV framework for this, it comes with a class to export whatever would be displayed to various formats, including .MHTML and .XML formats that are understood by Excel. The class CL_SALV_TABLE has a method TO_XML to support this; additionally, you might need the CL_SALV_BS_XML_UTILS to handle the transformations. See the report SALV_TEST_TABLE_DISPLAY_OR_XML for example coding.

You should look at the ABAP2XLSX project.
http://wiki.sdn.sap.com/wiki/display/ABAP/abap2xlsx
Not sure if all the necessary components exist in BW, but this is really the best solution for creating spreadsheets out of ABAP code that I have found.

You can try the following which will download a CSV file, which with the .xls extension opens flawlessly in Excel:
Convert lt_rspcchain to a csv internal table by calling SAP_CONVERT_TO_CSV_FORMAT
Figure out where the user wants to store the file by calling cl_gui_frontend_services=>file_save_dialog( )
Store the file by calling cl_gui_frontend_services=>gui_download( )
I assume you will be able to find how these work by experience or through Google.

Related

Cannot create field catalog with REUSE_ALV_FIELDCATALOG_MERGE

I'm new to ABAP, and I'm trying to build a field catalog using the REUSE_ALV_FIELDCATALOG_MERGE function module. This function module exits with sy-subrc value 1 ("Inconsistent interface") and a message dialog appears saying that the field catalog couldn't be build.
My code is the same as the examples found online. Maybe I missed something.
My program consists of a TOP include, a FORMS include and the main module:
FORMS include:
FORM DISPLAY_WITH_ALV_LIST.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = sy-repid
I_INTERNAL_TABNAME = 'it_report'
I_INCLNAME = sy-repid
CHANGING
CT_FIELDCAT = it_fldcat.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
IT_FIELDCAT = it_fldcat
TABLES
T_OUTTAB = it_report.
ENDFORM.
FORM ZSELECT.
SELECT VBELN ERDAT ERNAM
FROM VBAK
INTO CORRESPONDING FIELDS OF TABLE it_report
WHERE ERDAT IN S_ERDAT
AND ERNAM IN S_ERNAM.
ENDFORM.
TOP include:
TYPE-POOLS: slis.
TABLES VBAK.
DATA: BEGIN OF it_report OCCURS 0,
VBELN LIKE VBAK-VBELN,
ERDAT LIKE VBAK-ERDAT,
ERNAM LIKE VBAK-ERNAM,
END OF it_report.
DATA it_fldcat TYPE slis_t_fieldcat_alv.
Main module:
REPORT ZMLA_EXO1.
INCLUDE ZMLA_EXO1_TOP.
INCLUDE ZMLA_EXO1_SCREEN.
INCLUDE ZMLA_EXO1_FORM.
INITIALIZATION.
AT SELECTION-SCREEN.
START-OF-SELECTION.
PERFORM ZSELECT.
PERFORM DISPLAY_WITH_ALV_LIST.
END-OF-SELECTION.
I would advise using the "SALV" class. It's pretty straight forward, in your case it would look like this:
DATA: go_salv_table TYPE REF TO cl_salv_table.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = go_salv_table
CHANGING
t_table = it_report.
go_salv_table->display( ).
If you still insist on using the function module (FM) REUSE_ALV_FIELDCATALOG_MERGE, and generate field catalog from internal table, then these conditions must be observed:
Your internal table with the data to be displayed has to be declared with the word "OCCURS" (not a must to use addition "WITH HEADER LINE").
Fields of the internal table have to be declared using "LIKE". It will not work if you use "TYPE" to declare the fields.
No line in your program should exceed 72 characters. Otherwise a short dump will be generated with the exception cx_sy_read_src_line_too_long since the FM has to scan your program code looking for the internal table definition.
In short, it's an old FM with a lot of problems.

How to retrieve a specific field from a list output?

I don't have any developer rights in my SAP-System but I found a way to write some ABAP-Code in a tiny "User-Exit" box (I don't know if that's what you call it) inside a report.
I'm trying to submit a HR-Report and plug it's outcoming PERNR into that same report again.
There's a syntax-error that is telling me that t_list doesn't have a component with the Name PERNR.
What do I have to do in order to get this to work?
DATA: t_list TYPE TABLE OF abaplist WITH HEADER LINE,
seltab TYPE TABLE OF rsparams,
selline LIKE LINE OF seltab.
*I found out that the name of the selection field in the Report-GUI is "PNPPERNR" and tested it
selline-selname = 'PNPPERNR'.
selline-sign = 'I'.
selline-option = 'EQ'.
SUBMIT Y5000112
USING SELECTION-SET 'V1_TEST'
EXPORTING LIST TO MEMORY
AND RETURN.
CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = t_list
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE 'Unable to get list from memory'.
ELSE.
LOOP AT t_list.
*The Problem is here: how do I get the pnppernr out of t_list, it's the first column of the report output
selline-low = t_list-pernr.
append selline to seltab.
ENDLOOP.
SUBMIT Y5000112
WITH SELECTION-TABLE seltab
USING SELECTION-SET 'V2_TEST'
AND RETURN.
ENDIF.
Use the function module LIST_TO_ASCI to decode the contents of t_list into something readable. This answer contains some sample code including the data types required. At this point, the data you're looking for will probably occur at the same column range in the output. Use the standard substring access methods - e. g. line+42(21) to obtain the part of the line you need.
The vwegert's answer is more than useful! In my previous answer I forgot to mention LIST_TO_ASCI FM :)
The only thing I can add is that parsing of result lines has no universal solution and greatly depends on its structure. Usually it is done like:
LOOP AT t_list.
SPLIT t_list AT '|' INTO <required_structure>.
selline-low = <required_structure>-pernr.
APPEND selline TO seltab.
ENDLOOP.
where <`required_structure> is your Y5000112 output structure. But this may be not so simple and may require additional manipulations.

Inserting data into BAPIRET2_TAB structure

My method is using an export parameter of type BAPIRET2_TAB. I need to fill the values of this structure, but I cant access the structure directly. For example, parameter-message = 'text', etc.
How can I do this?
These are the parameters I need to pass:
lv_msg_line. type i
lv_syntax_text. //Error message
And this is syntax checker.
syntax-check for l_tab_code
program lv_progname
message l_error_message
line l_error_line
word l_error_word
id 'ERR' table l_tab_errors.
Like said above in the comments, BAPIRET2_TAB is not a structure, and therefore cannot have its components accessed directly via STRUCTURE-FIELD paradigm.
What you need is to declare an structure like this
DATA error_line TYPE LINE OF BAPIRET2_TAB.
Then, you can use it to fill the data in...
error_line-program = sy-repid.
error_line-id = sy-msgid.
... and so forth. Then, lastly, you append the error_line item to your BAPIRET2_TAB.
APPEND error_line TO bapi2tab.
CLEAR error_line.
Hope it helps.

Alv OO program error Message no. 0K534

Everytime i hit enter or any command button the program prompt an error message.
base on this thread http://scn.sap.com/thread/65856 i should declare my internal table
globally on top include.
Even though i already added all variable globally still the error is the same.
Top Include.
data: gr_data type ref to data.
data: la_data type ref to data.
field-symbols: <gt_data> type standard table.
Classs Declaration
me->get_data( CHANGING c_data = <f_tab> ). " Fetch Dynamic Data
METHOD get_data.
GET REFERENCE OF c_data INTO la_data.
move la_data TO gr_data.
assign gr_data->* to <gt_data>.
me->display( ).
assign gr_data->* to <gt_data>.
IF gc_custom_container is initial.
CREATE OBJECT gc_custom_container
EXPORTING
container_name = gv_mycontainer.
ENDIF.
if table is not bound.
try.
"// Create ALV Instance
cl_salv_table=>factory(
exporting
r_container = gc_custom_container
container_name = 'TC_MIXING'
importing
r_salv_table = table
changing
t_table = <gt_data>
).
catch cx_salv_msg. "#EC NO_HANDLER
endtry.
"// Setup ALV Attributes
functions = table->get_functions( ).
functions->set_all( abap_true ).
columns = table->get_columns( ).
columns->set_optimize( abap_true ).
try.
column = columns->get_column( 'MANDT' ).
column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found.
endtry.
"// Dispalay ALV Model
table->display( ).
else.
table->refresh( ).
endif.
ENDMETHOD.
and another question:
how to create structure dynamically base on field-symbol. is this possible?
ls_testvar like line of <f_tab>.
thanks and regards,
Mapet
Question two is possible, with some usage of runtime type services.
Take a look at runtime type services.
se24
CL_ABAP_CLASSDESCR Run Time Type Services
CL_ABAP_DATADESCR Run Time Type Services
CL_ABAP_ELEMDESCR Run Time Type Services
CL_ABAP_INTFDESCR Run Time Type Services
CL_ABAP_OBJECTDESCR Run Time Type Services
CL_ABAP_REFDESCR Run Time Type Services
CL_ABAP_STRUCTDESCR Run Time Type Services
CL_ABAP_TABLEDESCR Run Time Type Services
CL_ABAP_TYPEDESCR Run Time Type Services
You can get the type of the fieldsymbol and create a structure based on it, perhaps You have to iterate through fieldsymbol, if it is composed type and add each component.
I agree, you should not pass a field symbol to the changing parameter for the table data! Use a global or static table. You should also use a typed table instead of data and create your field catalog in design time. This improves the performance a lot.
Cheers
Your table variable holding the ALV must be declared globally as well.
cl_salv_table=>factory(
exporting
r_container = gc_custom_container
container_name = 'TC_MIXING'
importing
r_salv_table = table
changing
t_table = <gt_data>
).
You should separate data retrieving from your displaying code like this:
START-OF-SELECTION.
gr_class->get_data( ).
CALL SCREEN 2000.
PBO:
gr_class->display.
Your display method:
METHOD display IMPLEMENTATION.
IF me->gr_container IS NOT BOUND.
gr_table->display( ).
ELSE.
gr_table->refresh( ).
ENDIF.

How do I get my data to show up in my ALV?

I'm thinking that I'm probably missing an export parameter (from my Function Call POV).
In the REUSE_ALV_GRID_DISPLAY function call, the parameters I pass around are:
Exporting:
i_callback_program,
i_callback_pf_status_set,
i_callback_user_command,
is_layout,
it_fieldcat,
i_save
Tables:
t_outtab
And the exceptions plus handling.
I've checked that the internal table that I pass has data and it does.
I think the information I put up will suffice but if you really need to see the code, I'll do so.
I'm a noob and any help would be appreciated.
Thanx.
There are several ways to use ALV, so we may indeed need more info on your code to help.
First Method is to use the function module REUSE_ALV_GRID_DISPLAY. This will directly display the table content in the output dynpro. If all you need is a display, then go for it, as this is the simpliest : If the table structure is in the dictionnary, this call can be as simple as the following (this will display all members of the struct as column)
myreport = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = myreport
it_excluding = exclude_tab
TABLES
t_outtab = display_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
If the structure is declared in the program, then you have to create a field catalog.
the following code can serve as basis :
FORM fill_fieldcat CHANGING p_fieldcat TYPE slis_t_fieldcat_alv.
* Data definition
DATA ls_fieldcat TYPE slis_fieldcat_alv.
* Macro definition
DEFINE append_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = &1. * name of the field in struct
ls_fieldcat-tabname = &2. * name of the table
ls_fieldcat-row_pos = &3. * column
ls_fieldcat-ref_fieldname = &4. * field in ref table
ls_fieldcat-ref_tabname = &5. * ref table
ls_fieldcat-outputlen = &6. * size of output
ls_fieldcat-seltext_m = &7. * text (space if using the element typetext)
ls_fieldcat-ddictxt = 'M'.
ls_fieldcat-key = &8. * is this a key field in table
ls_fieldcat-emphasize = &9. * emphisze column display
append ls_fieldcat to p_fieldcat.
END-OF-DEFINITION.
* Init.
REFRESH p_fieldcat.
* Append fielcatalog for ALV
append_fieldcat:
'FORMATIONCODE' 'DISPLAY_TAB' 1 'SHORT' 'HRP1000' 12 'Code Stage' space space,
'FORMATIONTEXT' 'DISPLAY_TAB' 1 'STEXT' 'HRP1000' 20 'Libelle Stage' space space,
'SESSIONID' 'DISPLAY_TAB' 1 'OBJID' 'HRP1000' space 'Session' space space,
'BEGDA' 'DISPLAY_TAB' 1 'BEGDA' 'HRP1000' space 'Debut' space space,
'ENDDA' 'DISPLAY_TAB' 1 'BEGDA' 'HRP1000' space 'Fin' space space,
ENDFORM. "fill_fieldCat
you then call the form to create the field catalog, and use it in the it_fieldcat parameter of the function call.
Second method is to use ABAP-Object. Use check se83 for exemples of this use. the basis is as follows :
In your Dynpro you declare a custom container with a given name ("ALV_CONT"). Then in then PBO of the dynpro you initialize the container and put an ALV objct inside :
* global variables :
DATA : delegationlist_table TYPE REF TO cl_gui_alv_grid,
delegationlist_container TYPE REF TO cl_gui_custom_container.
data : gs_layout TYPE lvc_s_layo.
in PBO
IF delegationlist_container IS INITIAL.
* create a custom container control for our ALV Control
CREATE OBJECT delegationlist_container
EXPORTING
container_name = 'ALV_CONT'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
* create an instance of alv control
CREATE OBJECT delegationlist_table
EXPORTING
i_parent = delegationlist_container.
* Set a titlebar for the grid control
gs_layout-grid_title = 'Délégations'.
gs_layout-sel_mode = 'A'.
gs_layout-cwidth_opt ='X'.
* set table as data source
* the struct name *must* be uppercase
* the table must have this struc
CALL METHOD delegationlist_table->set_table_for_first_display
EXPORTING
i_structure_name = 'ZPRT_DELEGATIONLIST'
is_layout = gs_layout
CHANGING
it_outtab = delegationlist.
ENDIF.
Hopes this help,
Regards
Guillaume PATRY
EDIT: Oh, and another thing - if you're really in POV (process on Value-Request = F4), be aware that there are limitations to what you can do. Try your code in a simple report right after START-OF-SELECTION, and if that works, try the same code in a POV module.
===
If you don't pass a structure name, you have to ensure that you pass a complete (!) field catalog, otherwise the ALV grid might start to work erratically or not at all. Use the function modules LVC_FIELDCATALOG_MERGE and LVC_FIELDCAT_COMPLETE (in this order) to get a LVC field catalog that can be used with the classes or REUSE_ALV_GRID_DISPLAY_LVC.
A couple people here suggested using the REUSE_ALV_GRID_DISPLAY. I'm sure this is a common way to get things done (I used to use it myself), but I've taken a sap delta course recently and they strongly suggested to not use it anymore (you can look it up, REUSE_ALV_GRID_DISPLAY is not officialy supported by SAP anymore).
Instead, use CL_SALV_TABLE, documentation here: http://help.sap.com/erp2005_ehp_04/helpdata/EN/d7/b22041aa7df323e10000000a155106/frameset.htm
It's actually rather convenient to use too.
Thanks for the effort but as it turned out, the mistake I did was that I didn't capitalize field names in building the Field Catalog. Such a newbie mistake. I guess I won't be doing that mistake again any time soon.
-migs
Pass the output internal table to the FM Parameter "t_outtab".
It will print your data output.