Sunday, December 19, 2010

Exporting internal table contents to Excel file

Prerequisite:

One should have the Basic Knowledge about WebDynpro ABAP.

Purpose:

To Export the Internal Table Values from the Browser to the Excel File.

Procedure:

1. Create one WebDynpro Application ZWA_EXPORT_EXCEL, and Save it in the ‘Local Object’.

2. In the View Tab, go to Context. Right Click the Context and Select Create à Node.

3. Give the Node Name as AIRLINE and Cardinality as 0..n and Supply Function as FILL_AIRLINE. Then Press Enter.

4. Inside the Node AIRLINE, Create one Attribute by Right Clicking that Node and Select Create à Attribute.

5. Give the Attribute Name as ID, with Type S_CARR_ID. Then Press Enter.

6. Create Another Node under the Context by Right Clicking the Context and Select Create à Node.

7. Give the Node Name as SFLIGHT with Dictionary Structure SFLIGHT with Cardinality 0..n and Press ‘ Add Attribute from Structure ‘.

8. Select the Component CARRID, CONNID, FLDATE, CURRENCY and PLANETYPE and Press Enter.

9. In the Layout Tab of the View, Right click the ROOTUIELEMENTCONTAINER and give Insert Element. It will open a Pop-Up for Creating an Element.

10. Enter DROPDOWN_AIRLINE in the Name Field and Select DropDownByIndex in the Type Field and Press Enter.



11. Create Context Binding For the Dropdown by Clicking the Button in the Right Side Of the Texts in the Properties. It will open a Popup with the Context Element . In that Select the Attribute ID.

12. Then create one Action for onSelect Event of the Dropdown by Clicking the Create Button as shown below. Then Give the Action Name as GET_FLIGHT_DETAILS and press Enter.

13. Create another Element by Right clicking the ROOTUIELEMENTCONTAINER and give Insert Element. It will open a Pop-Up for Creating an Element.

14. Enter TABLE_FLIGHT in the Name Field and Select Table in the Type Field and Press Enter.

15. In the Text Field of the Table Caption Properties, Give Flight Details.

16. Right Click the TABLE_FLIGHT and Select Create Binding. It will open a Popup for Creating Context Binding.

17. Click the Button Context. It will open a Popup for Selecting Context Element.

18. Select SFLIGHT and Press Enter.

19. It will show all the Context Attributes in that Node. Then Press Enter.

20. Create another Element by Right clicking the ROOTUIELEMENTCONTAINER and give Insert Element. It will open a Pop-Up for Creating an Element.

21. Enter ‘BUTTON’ in the Name Field and Select ‘Button’ from the Dropdown for the Type Field and Press Enter.

22. In the Text Field of the Button Properties, Give Export To Excel.

23. Then create one Action for onAction Event of the Button by Clicking the Create Button as shown below. Then Give the Action Name as EXPORT_TO_EXCEL and press Enter.



23. Then Go to the Methods Tab.

24. Write the Following Coding in the Corresponding Methods.

FILL_AIRLINE:

method FILL_AIRLINE .
* data declaration
data LT_AIRLINE type WD_THIS->ELEMENTS_AIRLINE.
data LS_AIRLINE like line of LT_AIRLINE.
  select distinct CARRID from SFLIGHT into table LT_AIRLINE.
* bind all the elements
NODE->BIND_TABLE(
NEW_ITEMS = LT_AIRLINE
SET_INITIAL_ELEMENTS = ABAP_TRUE ).
endmethod.
ONACTIONGET_FLIGHT_DETAILS:
method ONACTIONGET_FLIGHT_DETAILS .
  data LO_ND_AIRLINE type ref to IF_WD_CONTEXT_NODE.
data LO_EL_AIRLINE type ref to IF_WD_CONTEXT_ELEMENT.
data LS_AIRLINE type WD_THIS->ELEMENT_AIRLINE.
data LV_ID like LS_AIRLINE-ID.
* navigate from  to  via lead selection
LO_ND_AIRLINE = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_AIRLINE ).
* get element via lead selection
LO_EL_AIRLINE = LO_ND_AIRLINE->GET_ELEMENT( ).
* get single attribute
LO_EL_AIRLINE->GET_ATTRIBUTE(
exporting
NAME = `ID`
importing
VALUE = LV_ID ).
  data LO_ND_SFLIGHT type ref to IF_WD_CONTEXT_NODE.
data LO_EL_SFLIGHT type ref to IF_WD_CONTEXT_ELEMENT.
data LT_SFLIGHT type WD_THIS->ELEMENTS_SFLIGHT.
data LS_SFLIGHT type WD_THIS->ELEMENT_SFLIGHT.
* navigate from  to  via lead selection
LO_ND_SFLIGHT = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_SFLIGHT ).
  SELECT CARRID CONNID FLDATE CURRENCY PLANETYPE FROM SFLIGHT INTO
CORRESPONDING FIELDS OF TABLE LT_SFLIGHT
WHERE CARRID = LV_ID.
  
LO_ND_SFLIGHT->BIND_TABLE( LT_SFLIGHT ).
endmethod.
ONACTIONEXPORT_TO_EXCEL:
method ONACTIONEXPORT_TO_EXCEL .
  data LO_ND_SFLIGHT type ref to IF_WD_CONTEXT_NODE.
data LO_EL_SFLIGHT type ref to IF_WD_CONTEXT_ELEMENT.
data LT_SFLIGHT type WD_THIS->ELEMENTS_SFLIGHT.
data LS_SFLIGHT type WD_THIS->ELEMENT_SFLIGHT.
  data TEXT   type STRING.
data XTEXT type XSTRING.
* navigate from  to  via lead selection
LO_ND_SFLIGHT = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_SFLIGHT ).
* get all declared attributes
LO_ND_SFLIGHT->GET_STATIC_ATTRIBUTES_TABLE(
importing
TABLE = LT_SFLIGHT ).
  loop at LT_SFLIGHT into LS_SFLIGHT.
    concatenate TEXT LS_SFLIGHT-CARRID
LS_SFLIGHT-CONNID
LS_SFLIGHT-FLDATE
LS_SFLIGHT-CURRENCY
LS_SFLIGHT-PLANETYPE
CL_ABAP_CHAR_UTILITIES=>NEWLINE into TEXT separated by
CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
  endloop.
  call function 'SCMS_STRING_TO_XSTRING'
exporting
TEXT = TEXT
importing
BUFFER = XTEXT.
  WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
**path to the word file
I_FILENAME = 'WDP.xls'
* String Variable
I_CONTENT = XTEXT
* File Type
I_MIME_TYPE = 'EXCEL' ).
endmethod.

25. Save and Activate the Application.

26. Create Webdynpro Application, save it and Test the Application.

Output:

1. Select the Airline Id LH from the Dropdown.

2. It will display all the Entries with CARRID ‘LH’ from SFLIGHT Table.

3. Now Press the Button Export To Excel.

4. It will open a Popup for File Download. Press the Button Open.

5. It will open an Excel File with the Following Entries.

LH

400

20070428

EUR

A310-300

LH

400

20070526

EUR

A310-300

LH

400

20070721

EUR

A310-300

LH

401

20070525

EUR

A319

LH

401

20070622

EUR

A319

LH

401

20080523

EUR

A319

LH

2402

20070428

EUR

DC-10-10

LH

2402

20080426

EUR

DC-10-10

LH

2402

20080524

EUR

DC-10-10

LH

2407

20070428

EUR

727-200

LH

2407

20070721

EUR

727-200

LH

2407

20070818

EUR

727-200

Note:

To Export the Internal Table to Word Document, replace the code
  WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
I_FILENAME = 'WDP.xls'
I_CONTENT = XTEXT
I_MIME_TYPE = 'EXCEL' ).
With ,
  WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
I_FILENAME = 'WDP.doc'
I_CONTENT = XTEXT
I_MIME_TYPE = 'WORD' ).
To Export the Internal Table to Text File, replace the Excel code 
with the Following
  WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
I_FILENAME = 'WDP.txt'
I_CONTENT = XTEXT
I_MIME_TYPE = 'NOTEPAD' ).


No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites