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
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
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
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
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.
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.
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.
• 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.
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.
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.
Write the code in the respective methods.
Code forGetcellvarient ():
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); }
Reusable WebDynpro Component for Export to Excel from Table UI Element. (Generic Method).
Code forshowpopup():
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);
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();
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
}
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.
4) Open Data modeler and create Data link between used WebDynpro component and view.
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(getProductColumnInfo s(),wdContext.nodeTableNode()); //@@end } //@@begin others
private static final String tabledata[][]= { {"1001","John","9928453632" }, {"1002","Smith","9963582147" }, {"1003","Kerry","9874596321" }, };
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 }
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,
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.