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: Company: Abhilasha A Dahare. 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. 2009 SAP AG 1
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 2009 SAP AG 2
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. 2009 SAP AG 3
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. 2009 SAP AG 4
5) Define public part for the DC (assembly and compilation). Here we have to make public part for all jar files. Build this DC. 2009 SAP AG 5
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. 2009 SAP AG 6
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. 2009 SAP AG 7
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. 2009 SAP AG 8
2009 SAP AG 9
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. 2009 SAP AG 10
4) Coding Part of the Dc. Create two value attributes, one Resource of type resource and second popupwindow of type IWDWindow. 2009 SAP AG 11
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 2009 SAP AG 12
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++) 2009 SAP AG 13
iter.hasnext();) col=0; for (Iterator iter = columinfo.keyset().iterator(); attributename = (String) iter.next(); HSSFRow row_data_products = sheet.createrow(x+1); IWDNodeElement ele = datanode.getelementat(x); wb.createcellstyle(); HSSFCellStyle cellstle = cellstle.setwraptext(true); cellstle.setalignment(hssfcellstyle.align_center); cellstle.setfillbackgroundcolor(hssfcolor.blue_grey.index); cellstle.setfillforegroundcolor(hssfcolor.blue_grey.index); wb.createfont(); HSSFFont font1 = font1.setcolor(hssfcolor.blue.index); font1.setboldweight(hssffont.boldweight_normal); cellstle.setfont(font1); row_data_products.createcell((short)col++); HSSFCell cell = cell.setcellvalue(ele.getattributevalue(attributename).tostring()); cell.setcellstyle(cellstle); Put"); wb.write(outputstream); showpopup(wdwebresourcetype.xls, outputstream, "XLS Out outputstream.close(); // wdcontext.currentcontextelement().getpopwindow(); // window.close(); catch ( IOException ex ) ex.printstacktrace(); //@@end 2009 SAP AG 14
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. 2009 SAP AG 15
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. 2009 SAP AG 16
2009 SAP AG 17
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. 2009 SAP AG 18
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]); //@@end wdcontext.nodetablenode().addelement(tab); public void nactionexport_data(com.sap.tc.webdynpro.progmodel.api.iwdcustomevent wdevent ) //@@begin onactionexport_data(serverevent) wdthis.wdgetreuseexcelcompinterface().export_to_excel(getproductcolumninfo s(),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(); 2009 SAP AG 19
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. 2009 SAP AG 20
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. 2009 SAP AG 21
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. 2009 SAP AG 22