0% found this document useful (0 votes)
87 views

SAP

The document discusses exporting data from a Web Dynpro application to different formats like Excel, PowerPoint and PDF using open source APIs. It provides the steps to create development components to reference the external library jars and export data to these formats from a Web Dynpro screen. Code snippets show how to generate the different file formats programmatically using APIs like POI, JExcel and iText.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

SAP

The document discusses exporting data from a Web Dynpro application to different formats like Excel, PowerPoint and PDF using open source APIs. It provides the steps to create development components to reference the external library jars and export data to these formats from a Web Dynpro screen. Code snippets show how to generate the different file formats programmatically using APIs like POI, JExcel and iText.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Exporting Data from Web Dynpro in

Different Formats Using Open


Source (POI, JExcel, iText) API’s

Applies to:
Composition Environment (CE) Webdynpro for Java. For more information, visit the User Interface
Technology homepage. .

Summary
In this article you will find popular open source API’s into Webdynpro for exporting data. This article will help
to achieve this in different formats (Excel, PowerPoint and PDF)

Author: Ayyapparaj KV
Company: Bristlecone India Pvt Ltd
Created on: 24 September 2008

Author Bio

Is a Netweaver Java certified consultant working for Bristlecone

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 1
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

Table of Contents
Choices available for Exporting Data .................................................................................................................3
Open Source API’s used.................................................................................................................................3
Downloading the jar required ..........................................................................................................................3
Creating Development Components ..................................................................................................................3
Steps for creating External Library DC.........................................................................................................................3
Steps for referencing external library DC .....................................................................................................................3
Webdynpro screen ..........................................................................................................................................4
Excel................................................................................................................................................................4
PowerPoint......................................................................................................................................................4
PDF .................................................................................................................................................................5
Creating PDF ...............................................................................................................................................................5
Development Component used ........................................................................................................................10
Related Content................................................................................................................................................10
Disclaimer and Liability Notice..........................................................................................................................11

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 2
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

Choices available for Exporting Data


Following are the options we are going to explore.
• PDF: Exporting data as adobe pdf format.
• XLS: Exporting data as Microsoft Excel format.
• PPT: Exporting data as Microsoft PowerPoint format.

Open Source API’s used


• JExcel: JExcelApi can read an Excel spreadsheet from a file stored on the local file system or from
some input stream. The first step when reading a spreadsheet from a file or input stream is to create
a Workbook
• iText: iText is a library that allows you to generate PDF files on the fly.
• Apache POI: The POI project consists of APIs for manipulating various file formats based upon
Microsoft's OLE 2 Compound Document format using pure Java.

Downloading the jar required


Click on the following links to download respective jar files.
• JExcel
• iText
• Apache POI.
• The project file source code

Creating Development Components


We need to have two Dc’s to achieve this one an External Library DC which will contain all the open source
jars, and the other Dc which will make use of this to export data.

Steps for creating External Library DC


1) Create a External Library DC (File->New->Other->Development Infrastructure->Development
Component->External Library)
2) Add the above jar files to the library.
3) Define public part for the DC (assembly and compilation).
4) Build this DC.

Steps for referencing external library DC


1) Create a Webdynpro DC.
2) Select component Properties(Right Click->Development Component->Show In->Component
Properties)
3) Select Dependencies tab.
4) Add the external DC.
In this application we are going to export data that is rendered in a table in to different formats as mentioned
above.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 3
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

Snap Shots from the application and Exported Data


Webdynpro screen

Excel

Microsoft Office
Excel Worksheet

PowerPoint

Microsoft PowerPoint
Presentation

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 4
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

PDF

Adobe Acrobat
Document

Coding Part of the Dc

Creating PDF
public void exportToPDF( ) {
//@@begin exportToPDF()
Document document = null;
PdfWriter writer = null;
IWDResource resource = null;
ByteArrayOutputStream outputStream = null;
try
{
// creation of the document with a certain size and certain margins
document = new Document(PageSize.A4, 50, 50, 50, 50);
outputStream = new ByteArrayOutputStream();
// creation of the different writers
writer = PdfWriter.getInstance(document, outputStream);

// fonts
BaseFont baseFontTimes = BaseFont.createFont(BaseFont.TIMES_ROMAN,
"Cp1252", false);

// headers and footers must be added before the document is opened


HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: ", new Font(baseFontTimes)), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
document.setFooter(footer);

HeaderFooter header = new HeaderFooter(


new Phrase("Data Exported from the table", new
Font(baseFontTimes)), false);
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);

document.open();

// Table to display the data


Table table = new Table(4,6);
table.setTableFitsPage(true);

//Headers
for(Iterator<?> itr =
wdContext.nodeProducts().getNodeInfo().iterateAttributes(); itr.hasNext();)
{
IWDAttributeInfo attributeInfo = (IWDAttributeInfo)itr.next();
table.addCell(attributeInfo.getName());
}

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 5
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

//Data From the Table


for(int x=0; x<5; x++)
{
IProductsElement element =
wdContext.nodeProducts().getProductsElementAt(x);
table.addCell(String.valueOf(element.getID()));
table.addCell(element.getName());
table.addCell(element.getDescription());
table.addCell(String.valueOf(element.getPrice()));
}
//Adding table to the document
document.add(table);

document.close();

showPopUp(WDWebResourceType.PDF, outputStream, "PDF Out Put");

writer.close();
outputStream.close();
} catch (Exception ex) {
wdComponentAPI.getMessageManager().reportException(ex);
ex.printStackTrace();
}
//@@end
}
Creating XLS
public void exportToExcel( ) {
//@@begin exportToExcel()
ByteArrayOutputStream outputStream = null;
HSSFWorkbook wb = null;
HSSFSheet sheet = null;
try {
outputStream = new ByteArrayOutputStream();
//Work Book
wb = new HSSFWorkbook();
//Sheet
sheet = wb.createSheet("Table Contents");

int col=0;

//Column Headers
for(Iterator<?> itr =
wdContext.nodeProducts().getNodeInfo().iterateAttributes(); itr.hasNext();)
{
HSSFRow row = sheet.createRow(0);
IWDAttributeInfo attributeInfo = (IWDAttributeInfo)itr.next();
HSSFCell cell = row.createCell((short)col++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);

HSSFRichTextString richTextString = new


HSSFRichTextString(attributeInfo.getName());
cell.setCellValue(richTextString);
}

//Data from the table

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 6
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

for(int x=0; x<5; x++)


{
col=0;
HSSFRow row = sheet.createRow(x+1);
IProductsElement element =
wdContext.nodeProducts().getProductsElementAt(x);
HSSFCell cell = row.createCell((short)col++);
cell.setCellValue(element.getID());

cell = row.createCell((short)col++);
HSSFRichTextString richTextStringName = new
HSSFRichTextString(element.getName());
cell.setCellValue(richTextStringName);

cell = row.createCell((short)col++);
HSSFRichTextString richTextStringDesc = new
HSSFRichTextString(element.getDescription());
cell.setCellValue(richTextStringDesc);

cell = row.createCell((short)col++);
cell.setCellValue(element.getPrice());
}
wb.write(outputStream);

showPopUp(WDWebResourceType.XLS, outputStream, "XLS Out Put");

outputStream.close();
} catch ( IOException ex ) {
wdComponentAPI.getMessageManager().reportException(ex);
ex.printStackTrace();
}

//@@end
}
Creating PPT
public void exportToPPT( ) {
//@@begin exportToPPT()
ByteArrayOutputStream outputStream = null;
SlideShow ppt = null;
Slide slide = null;
try {
ppt = new SlideShow();
slide = ppt.createSlide();
TextBox title = slide.addTitle();
title.setText("Data Exported From Table");

org.apache.poi.hslf.model.Table table = new


org.apache.poi.hslf.model.Table(6, 4);
//set table borders
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(1.0);
table.setAllBorders(border);

table.setColumnWidth(0, 50);
table.setColumnWidth(1, 200);

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 7
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

table.setColumnWidth(2, 200);
table.setColumnWidth(3, 100);
table.moveTo(100, 100);

int col = 0;
int row = 0;
//Header for the Table
for(Iterator<?> itr =
wdContext.nodeProducts().getNodeInfo().iterateAttributes(); itr.hasNext();)
{
TableCell cell = table.getCell(0, col++);
IWDAttributeInfo attributeInfo = (IWDAttributeInfo)itr.next();
cell.setText(attributeInfo.getName());

RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(15);

cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}

// Column Contents
for(int x=0; x<5; x++)
{
col=0;
row = x + 1;
IProductsElement element =
wdContext.nodeProducts().getProductsElementAt(x);
TableCell cell = table.getCell(x+1, col++);
cell.setText(String.valueOf(element.getID()));

RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);

cell = table.getCell(row, col++);


cell.setText(element.getName());

rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);

cell = table.getCell(row, col++);


cell.setText(element.getDescription());

rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);

cell = table.getCell(row, col++);


cell.setText(String.valueOf(element.getPrice()));

rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 8
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

slide.addShape(table);

outputStream = new ByteArrayOutputStream();


ppt.write(outputStream);
showPopUp(WDWebResourceType.PPT, outputStream, "XLS Out Put");

outputStream.close();

} catch (Exception e) {
// TODO Auto-generated catch block
wdComponentAPI.getMessageManager().reportException(e);
e.printStackTrace();
}
//@@end
}

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 9
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

Development Component used


You can download the Dc’s used from here

Related Content
Apache POI
iText
JExcel
For more information, visit the User Interface Technology homepage.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 10
Exporting Data from Web Dynpro in Different Formats Using Open Source (POI, JExcel, iText) API’s

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 11

You might also like