Sei sulla pagina 1di 3

Exporting Webdynpro Table to CSV File.

1)Paste the below code in a Java file named ExportTableCSV.java.Place the file in
some package(ex.com.hcl.ui.utils) in src/packages.

package com.hcl.ui.utils;
/*

* Created on Feb 13, 2008

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

* @Author : Sudhir Gorantla

* @Company : HCL Technologies Ltd.,

*/

import java.io.UnsupportedEncodingException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import com.sap.tc.webdynpro.progmodel.api.IWDAttributeInfo;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;

public class ExportTableCSV {

private IWDNode wdTableNode = null;

public ExportTableCSV(IWDNode tableNode) {

this.wdTableNode = tableNode;
}

public Map<String, Object> DownloadToCSV() {

byte[] bytes = null;String linktoFile = null;

StringBuffer err = new StringBuffer();

StringBuffer csv_file = new StringBuffer();

Map<String, Object> resultMap = new HashMap<String, Object>();


int noOfElements = wdTableNode.size();

List<? extends IWDAttributeInfo> attributesInfoList =


wdTableNode.getNodeInfo().getAttributes();

for (int i = 0;i < wdTableNode.size(); i++) {

IWDNodeElement nodeElement = wdTableNode.getElementAt(i);

Iterator<? extends IWDAttributeInfo> attributesIterator =


attributesInfoList .iterator();

while (attributesIterator.hasNext()) {

IWDAttributeInfo attributeInfo = attributesIterator.next();

csv_file.append(nodeElement.getAttributeAsText(attributeInfo.getName(
)));

if (attributesIterator.hasNext()){

csv_file.append(",");

try{

bytes = csv_file.toString().getBytes("UTF-8");

catch (UnsupportedEncodingException e){

err.append("" + e.getCause());

resultMap.put("data", bytes);

resultMap.put("error", err);

return resultMap;

}
2)Insert a UI Element of type FileDownload.Create a context value attribute named
fileResource of type com.sap.ide.webdynpro.uielementdefinitions.Resource.Bind the
"resource" attribute of the FileDownload UI Element to already created fileResource
value attribute.Change the "behaviour" property of FileDownload UI element to
"allowSave".

In wdDoModifyView() paste the following code

if (firstTime){

IWDResource resource =WDResourceFactory.createResource((byte[])new


ExportTableCSV(wdContext .nodeTableData
()).DownloadToCSV(). get("data"), "File",WDWebResourceType.TXT);

wdContext.currentContextElement(). setFileResource(resource);

where TableData node is the dataSource of the table whoose data has to be exported
to a CSV File.

3)Now Click on the FileDownload UI Element to download the CSV File.

Potrebbero piacerti anche