22
Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method). Applies to: SAP NetWeaver WebDynpro for Java version NWDS 7.0.13. For more information, visit the User Interface Technology homepage , For more information, visit the Web Dynpro Java homepage . Summary This article deals with creating the Reusable component i.e. Generic method for Export to Excel Download. Author: Abhilasha A Dahare. Company: L & T Infotech. Created on: 8 April 2009 Author Bio Abhilasha Dahare is working as a WebDynpro consultant past 1 year in L&T Infotech. SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 1

Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

  • Upload
    donhu

  • View
    243

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Applies to: SAP NetWeaver WebDynpro for Java version NWDS 7.0.13. For more information, visit the User Interface Technology homepage, For more information, visit the Web Dynpro Java homepage.

Summary This article deals with creating the Reusable component i.e. Generic method for Export to Excel Download.

Author: Abhilasha A Dahare.

Company: L & T Infotech.

Created on: 8 April 2009

Author Bio

Abhilasha Dahare is working as a WebDynpro consultant past 1 year in L&T Infotech.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 1

Page 2: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Table of Contents Steps ................................................................................................................................................................... 3 

Creating Reusable Development Components .............................................................................................. 3 Steps for creating External Library DC ......................................................................................................................... 3 Steps for referencing external library DC i.e. creating Reusable WebDynpro DC ........................................................ 6 

Steps for using the reusable WebDynpro component .................................................................................. 16 

Related Content ................................................................................................................................................ 21 

Disclaimer and Liability Notice .......................................................................................................................... 22 

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 2

Page 3: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Steps

Creating Reusable Development Components

We have to create Three Dc’s to make a reusable component, one an External Library DC which will contain the open source jars, other Dc which uses as reusable dc in many DC’s to export the data into excel the and referencing the external library Dc as used dc, and the other WebDynpro Dc which will make use of this to export data.

Steps for creating External Library DC 1) Create an External Library DC (File->New-> Development Component)

2) Local Development My component next

3) Give a project name and caption and select external library project as type and then press finish.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 3

Page 4: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

4) Add the jar files to the library. (Shown below in the screen shot)

poi-scratchpad-3.1-FINAL-20080629.jar poi-3.1-FINAL-20080629.jar poi-contrib-3.1-FINAL-20080629.jar Click on the following links to download respective jar files.

Export to excel Jar Copy all jar files and paste it into as follow.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 4

Page 5: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

5) Define public part for the DC (assembly and compilation). Here we have to make public part for all jar files.

Build this DC.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 5

Page 6: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Steps for referencing external library DC i.e. creating Reusable WebDynpro DC

In this we will create one WebDynpro project which will refer the external library project and can be used as a reusable component to export table data to excel.

1) Create a WebDynpro local development.

Give a project name and caption and select WebDynpro as type ,Next and then click on finish.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 6

Page 7: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

2) Add External Library project as used dc in newly created WebDynpro project.

Finish. 3) Now open component controller and create three methods.

• Getcellvarient () of type java.lang.string and create two parameters for this method, first parameter as p_row of type HSSFRow and second parameter as p_cell_no of integer.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 7

Page 8: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

• Showpopup () of type void. Create three parameters for this method, first parameter as

“Resourcetype” of type wdwebResourcestype .second parameter as “outputstream” of type ByteArrayoutputstream and third parameter as “title” of type java.lang.String.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 8

Page 9: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 9

Page 10: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

• Export_to_excel () of type void and create two parameters for this method, one coulmninfo of

type java.util.map and the other datanode of type IWDNode.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 10

Page 11: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

4) Coding Part of the Dc. Create two value attributes, one Resource of type resource and second popupwindow of type IWDWindow.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 11

Page 12: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Write the code in the respective methods.

Code for Getcellvarient ():

public java.lang.String GetcellVarient( org.apache.poi.hssf.usermodel.HSSFRow p_row, int p_cell_no ) { //@@begin GetcellVarient() String l_value = null ; HSSFCell l_cell = p_row.getCell((short)p_cell_no); if(!(l_cell == null)) { if(l_cell.getCellType() == l_cell.CELL_TYPE_STRING) l_value = l_cell.getStringCellValue(); else if(l_cell.getCellType() == l_cell.CELL_TYPE_NUMERIC) { long l_lng_value = (long)(l_cell.getNumericCellValue()); l_value = String.valueOf(l_lng_value); } } return l_value; //@@end }

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 12

Page 13: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Code for showpopup():

public void showpopup( com.sap.tc.WebDynpro.services.sal.url.api.WDWebResourceType resourcetype, java.io.ByteArrayOutputStream outputstream, java.lang.String title ) { //@@begin showpopup() IWDResource resource = null; resource = WDResourceFactory.createResource(outputstream.toByteArray(),"ExcelSheet",resourcetype); wdContext.currentContextElement().setResources(resource); IWDWindow window; window = wdComponentAPI.getWindowManager().createNonModalExternalWindow(resource.getUrl( WDFileDownloadBehaviour.AUTO.ordinal()), title); window.setWindowSize(200,150); window.setWindowPosition(WDWindowPos.CENTER); window.show(); wdContext.currentContextElement().setPopupwindow(window); //@@end } Code for export_to_excel (): public void export_to_excel( java.util.Map columinfo, com.sap.tc.WebDynpro.progmodel.api.IWDNode datanode ) { //@@begin export_to_excel() ByteArrayOutputStream outputStream = null; HSSFWorkbook wb = null; HSSFSheet sheet = null; try { outputStream = new ByteArrayOutputStream(); wb = new HSSFWorkbook(); sheet = wb.createSheet("Table1"); int col = 0; String attributeName, trimmedHeaderText; HSSFRow row = sheet.createRow(0); for (Iterator iter = columinfo.keySet().iterator(); iter.hasNext();) { attributeName = (String) iter.next(); trimmedHeaderText = (String)columinfo.get(attributeName); columinfo.put(attributeName, trimmedHeaderText); HSSFCell cell_hed = row.createCell((short)col++); cell_hed.setCellType(HSSFCell.CELL_TYPE_STRING); cell_hed.setCellValue(trimmedHeaderText); } for(int x=0; x<datanode.size(); x++) {

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 13

Page 14: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

col=0; for (Iterator iter = columinfo.keySet().iterator(); iter.hasNext();) { attributeName = (String) iter.next(); HSSFRow row_data_products = sheet.createRow(x+1); IWDNodeElement ele = datanode.getElementAt(x); HSSFCellStyle cellstle = wb.createCellStyle(); cellstle.setWrapText(true); cellstle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellstle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index); cellstle.setFillForegroundColor(HSSFColor.BLUE_GREY.index); HSSFFont font1 = wb.createFont(); font1.setColor(HSSFColor.BLUE.index); font1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); cellstle.setFont(font1); HSSFCell cell = row_data_products.createCell((short)col++); cell.setCellValue(ele.getAttributeValue(attributeName).toString()); cell.setCellStyle(cellstle); } } wb.write(outputStream); showpopup(WDWebResourceType.XLS, outputStream, "XLS Out Put"); outputStream.close(); // wdContext.currentContextElement().getPopwindow(); // window.close(); } catch ( IOException ex ) { ex.printStackTrace(); } //@@end

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 14

Page 15: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

}

5) Create Data link Between Component controller and Interface controller and then create method in interface controller “call_compcont_excel” of type void with two parameters columninfo (java.util.Map) and datanode (IWDNode) which will call export to excel method of component controller method.

Write a code in interface controller method as follow. public void call_compcont_excel (java.util.Map columinfo, com.sap.tc.WebDynpro.progmodel.api.IWDNode datanode) { //@@begin call_compcont_excel () wdThis.wdGetExcelAppController ().export_to_excel (columinfo, datanode); //@@end }

6) Create Public part for this Dc, Build and then Deploy.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 15

Page 16: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Steps for using the reusable WebDynpro component

In this application we are going create a WebDynpro project which will export data that is populated in a table in to ExcelSheet.

1) Create development component of WebDynpro.

2) Add Reusable Export to excel Dc (here reuse_export_to_excel) as used dc.

3) Finish and then add used component.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 16

Page 17: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 17

Page 18: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

4) Open Data modeler and create Data link between used WebDynpro component and view.

5) Create value node to populate data in table as follow.

6) Create table in view by using Apply Template and also create a Button and bind it with a method

Export_Data which will call method of reuse_export_to_excel dc interface controller.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 18

Page 19: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

7) Coding Part for the dc.

public void wdDoInit() { //@@begin wdDoInit() for(int i=0;i<=tabledata.length-1;i++) { IPrivateTableExcelAppView.ITableNodeElement tab= wdContext.createTableNodeElement(); //tab=wdContext.nodeTable().getElementAt(i); tab.setCust_id(this.tabledata[i][0]); tab.setCust_name(this.tabledata[i][1]); tab.setCust_phn(this.tabledata[i][2]); wdContext.nodeTableNode().addElement(tab); } //@@end

}

public void nActionExport_data(com.sap.tc.WebDynpro.progmodel.api.IWDCustomEvent wdEvent ) { //@@begin onActionExport_data(ServerEvent) wdThis.wdGetReuseExcelcompInterface().export_to_excel(getProductColumnInfos(),wdContext.nodeTableNode()); //@@end

}

//@@begin others private static final String tabledata[][]= { {"1001","John","9928453632" }, {"1002","Smith","9963582147" }, {"1003","Kerry","9874596321" }, }; private Map getProductColumnInfos() { Map columnInfosMap = new HashMap();

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 19

Page 20: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

columnInfosMap.put( IPrivateTableExcelAppView.ITableNodeElement.CUST__ID, "CUST__ID"); columnInfosMap.put( IPrivateTableExcelAppView.ITableNodeElement.CUST__NAME, "CUST__NAME"); columnInfosMap.put( IPrivateTableExcelAppView.ITableNodeElement.CUST__PHN, "CUST__PHN"); return columnInfosMap; } //@@end }

8) Build and Deploy.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 20

Page 21: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

Related Content 1) Exporting Context Data into Excel Using the WebDynpro Binary Cache 2) Exporting Data from WebDynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s 3) Enhancing Tables in WebDynpro Java – Custom built Table Utilities 4) For more information, visit the User Interface Technology homepage, 5) For more information, visit the Web Dynpro Java homepage.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 21

Page 22: Reusable WebDynpro Component for Export to … WebDynpro Component for Export to Excel from Table UI Element. ... Enhancing Tables in WebDynpro Java ... Web Dynpro Java homepage. SAP

Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com © 2009 SAP AG 22

Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.