SAP
SAP
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
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
Excel
Microsoft Office
Excel Worksheet
PowerPoint
Microsoft PowerPoint
Presentation
Adobe Acrobat
Document
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);
document.open();
//Headers
for(Iterator<?> itr =
wdContext.nodeProducts().getNodeInfo().iterateAttributes(); itr.hasNext();)
{
IWDAttributeInfo attributeInfo = (IWDAttributeInfo)itr.next();
table.addCell(attributeInfo.getName());
}
document.close();
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);
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);
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");
table.setColumnWidth(0, 50);
table.setColumnWidth(1, 200);
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);
rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);
rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);
rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(12);
slide.addShape(table);
outputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
wdComponentAPI.getMessageManager().reportException(e);
e.printStackTrace();
}
//@@end
}
Related Content
Apache POI
iText
JExcel
For more information, visit the User Interface Technology homepage.