J2EE,SOAP Web Services, Adobe flex & Java Dictionary . Export To Excel with Formatted cells SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com ...
Accessing Portal Iviews using PCD API
Applies to: SAP Portal 7.0+.For more information, visit the Web Dynpro Java homepage.
Summary The article describes the concept and procedure of Export To Excel with formatted information using jxl API Author:
Nilesh Rane
Company: Larsen & Toubro Infotech Limited Created on: 30 March 2010
Author Bio Nilesh Rane is a SAP NetWeaver Consultant in Larsen & Toubro Infotech Limited. He has three years of SAP Portal experience. He has worked extensively in WebDynpro - Java, EPortal, J2EE,SOAP Web Services, Adobe flex & Java Dictionary
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1
Export To Excel with Formatted cells
Table of Contents Concepts ............................................................................................................................................................. 3 Purpose ........................................................................................................................................................... 3 JXL API ........................................................................................................................................................... 3 Developing a Web Dynpro Project ..................................................................................................................... 4 Result ............................................................................................................................................................ 15 Summary ....................................................................................................................................................... 17 Related Content ................................................................................................................................................ 18 Disclaimer and Liability Notice .......................................................................................................................... 19
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2
Export To Excel with Formatted cells
Concepts Purpose This white paper describes how to use “Export To Excel” functionality using Web dynpro Java and jxl API with formatted information, such as fonts, decimal places, numbers, dates and excel formulas. JXL API Java Excel API is a mature, open source java API enabling developers to read, write, and modify Excel spreadsheets dynamically. Now java developers can read Excel spreadsheets, modify them with a convenient and simple API, and write the changes to any output stream Features and later only)
i
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3
Export To Excel with Formatted cells
Developing a Web Dynpro Project We will create a WebDynpro Project. In the view, we will create a node with 6 different attributes (e.g. int, string, float etc) to check formatting of the data types in excel sheet. After that, We will send formatted data to excel sheet.
Open NWDS in WebDynpro Java Perspective
Select File -> New -> Development Component Project
Give any meaningful name and click on Next
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 4
Export To Excel with Formatted cells
Click on finish.
Since we are using JXL api following jar files need to add in the classpath for the build time and run time references.
For this, create another Library type of Project and add above three jar files into the libraries folder of the project. Expose it as a public part (API and SDA)
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 5
Export To Excel with Formatted cells
We can use this library component in our WebDynpro project as Used DC.
Now, create WebDynpro Application, component and View.
Enter name of the project and package and click on Next Button
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 6
Export To Excel with Formatted cells
Select New WebDynpro component and create view and window and click on Finish button.
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 7
Export To Excel with Formatted cells
Open ExportToExcelView which you have created and create layout
Create context for the view Note that the suffix indicates the type of the attribute. The attribute ctx_va_ExcelFileData is of type IWDResource.
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 8
Export To Excel with Formatted cells
Create following methods in the ExportToExcelView mExportToExcel() Export to Excel
mPopulateTable() Populate data in the Web Dynpro table
mWritetableSheet(jxl.write.WritableSheet p_writableSheet) Formatting of data which is in the WebDynpro node
In the wdDoInit() method of the view write following statement to populate the Node and Table
Write following code in the method mPopulateTable()
Calendar currCal = Calendar.getInstance();
Calendar newCal = (Calendar) currCal.clone(); Calendar toCal = (Calendar) newCal.clone(); toCal.add(Calendar.MONTH,2);
currCal.add(Calendar.MONTH,2); String str = "11.23";
SAP COMMUNITY NETWORK © 2010 SAP AG
SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 9
Export To Excel with Formatted cells
float l_float = Float.valueOf(str.trim()).floatValue(); double l_double = 4.56789;
for(int a=0;a