Skip to Content
Author's profile photo Durga Rao Vaddadi

Table Data Download To Excel Sheet.

Dear All,

Here table data we can download into excel sheet.I hope this is help for others.

methode –>DownloadToExcel( java.lang.String key, java.lang.String tableName, com.sap.tc.webdynpro.progmodel.api.IWDView currentView, java.lang.String fileName, com.sap.tc.webdynpro.progmodel.api.IWDNode dataSource )

this method having five parameters.Those are

  • java.lang.String key
  • java.lang.String tableName
  • com.sap.tc.webdynpro.progmodel.api.IWDView currentView
  • java.lang.String fileName
  • com.sap.tc.webdynpro.progmodel.api.IWDNode dataSource

The above five parameters we need to pass the data after we can download.


Initially checks whether the datasource is empty. only if the datasource contains some values the table data could be exported.

if(!dataSource.isEmpty())

  {

  String tableData=””;

  Iterator it;

  int elimination=-1;

  int elimination1=0;

  String column_type = “”;

  String column_visib = “”;

  int curr_ld = dataSource.getLeadSelection();

  //Collecting the Node attributes name of the Table

  ColumnInfo = new ArrayList();

  ArrayList cellEditors = new ArrayList();

  ArrayList columnpros = new ArrayList();

  it= dataSource.getNodeInfo().iterateAttributes();

  IWDTable table = (IWDTable)currentView.getElement(tableName);

  ArrayList used_column_ID = new ArrayList();

  IWDAbstractTableColumn abstractTableColumn[] = table.getGroupedColumns();

  int tableColum_Size =abstractTableColumn.length;

  while(it.hasNext())

  ColumnInfo.add(((IWDAttributeInfo)it.next()).getName());

  //Converting the attribute name and value in to String

  try

  {

  for(int HeadCol=0;HeadCol<tableColum_Size;HeadCol++)

  {

  IWDTableColumn column = (IWDTableColumn)currentView.getElement(abstractTableColumn[HeadCol].getId());

  column_type = column.getTableCellEditor().getClass().getName();

  column_type = column_type.substring(column_type.lastIndexOf(“.”)+1,column_type.length());

  if(column.getVisible() == WDVisibility.VISIBLE)

  {

  if((column_type.equalsIgnoreCase(“TextView”) || column_type.equalsIgnoreCase(“InputField”)) || column_type.equalsIgnoreCase(“LinkToAction”))

  {

  used_column_ID.add(used_column_ID.size(),column.getId());

  cellEditors.add(cellEditors.size(),column.getTableCellEditor().getId());

  columnpros.add(columnpros.size(),column_type);

  tableData=tableData+abstractTableColumn[HeadCol].getHeader().getText()+”\t”;

  }

  }

  }

  for(int Row=0;Row<dataSource.size();Row++)

  {

  dataSource.setLeadSelection(Row);

  if(!columnpros.isEmpty())

  {

  tableData=tableData+”\n”;

  }

  for(int Col=0;Col<used_column_ID.size();Col++)

  {

  IWDTableColumn column = (IWDTableColumn)currentView.getElement(used_column_ID.get(Col).toString());

  if(column.getVisible() == WDVisibility.VISIBLE)

  {

  if(columnpros.get(Col).equals(“TextView”))

  {

  IWDTextView txtview = (IWDTextView)currentView.getElement(cellEditors.get(Col).toString());

  tableData = tableData+txtview.getText()+”\t”;

  }

  if(columnpros.get(Col).equals(“InputField”))

  {

  IWDInputField inpField = (IWDInputField)currentView.getElement(cellEditors.get(Col).toString());

  tableData = tableData+inpField.getValue()+”\t”;

  }

  if(columnpros.get(Col).equals(“LinkToAction”))

  {

  IWDLinkToAction linkToAction = (IWDLinkToAction)currentView.getElement(cellEditors.get(Col).toString());

  tableData = tableData+linkToAction.getText()+”\t”;

  }

  }

  }

  }

  /*

  * Finally sets the leadselection of the table to the original one from where the export started

  */

  dataSource.setLeadSelection(curr_ld);

  }

  catch(Exception Ex)

  {

  wdComponentAPI.getMessageManager().reportException(Ex.getMessage(),false);

  }

  //writing the collected string in to ByteArrayOutputStream

  ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

  try

  {

  os.write(tableData.getBytes());  

  }

  catch (IOException exception)

  {

  wdComponentAPI.getMessageManager().reportException(“IO Exception”+exception.getMessage(),false);

  }

  //converting the stream to byte

  tabledata_byte = os.toByteArray();

  // Show the excel or download window

  /*

  *  Initializing the resource variables

  */

  IWDResource resource = null;

  IWDResource wordResource = null;

  try

  {

  //Creating resource by binding the byte data obtained from the WebDynPro Table and displaying  in the window

  if(key.equalsIgnoreCase(“Export To Microsoft Excel”))

  {

  resource = WDResourceFactory.createCachedResource(new ByteArrayInputStream(tabledata_byte), fileName, WDWebResourceType.XLS, false);

  wdComponentAPI.getWindowManager().createNonModalExternalWindow(resource.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()), resource.getResourceName()).show();

  }

  else

  {

  wordResource = WDResourceFactory.createResource(new ByteArrayInputStream(tabledata_byte),fileName,WDWebResourceType.DOC,true);

  wdContext.currentContextElement().setTableResource(resource);

  IWDWindow window = wdComponentAPI.getWindowManager().createNonModalExternalWindow(wordResource.toString(),”Word Window”);

  window.show();

  }

  }

  catch(Exception Ex)

  {

  wdComponentAPI.getMessageManager().reportException(“In Exception”+Ex.getLocalizedMessage(),false);

  }

  }

  else

  {

  wdComponentAPI.getMessageManager().reportException(“The Table is empty”,true);

  }

Table Data Downloading to PDF Format.

How to export table data to PDF file

Thanks

BR,

Durga Rao

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.