Export selected data from an table control to an excel file

Many SAP ERP transactions display data in tabular form (table control). In some cases, users may want to download this data to Excel in order to display it in a different format, print it or process it further.

Here we present a simple method how to create an Excel spreadsheet from the SAP ERP table using an InputScript. The user selects some or all lines of the table and then calls the InputScript by clicking on the corresponding pushbutton. The script scrolls through the entire table, collects all selected rows and generates an xls file. The Start statement is then used to call Excel with this file.

- Generate a .xls file in .csv format

Transaction MIR6:
The user selects the rows that he or she wants to have in the Excel table. Then he clicks on the button "Download selected data". The system collects all selected data and starts Excel:


GuiXT Script SAPMM08N.D0201.txt:

Pushbutton (toolbar) "Download selected data" process="SaveToExcel.txt" 

InutScript "savetoexcel.txt:

Parameter Filename "C:\temp\Invoices.xls"
"&U[Filename]" -output 

// Column headings
V[C1] "Document number"
V[C2] "Invoicing party"
V[C3] "Name of invoicing party"
V[C4] "Gross amount"

AppendFile "&U[Filename]" C1 C2 C3 C4

// Variables
et V[absrow] 1 // Absolute row number
et V[relrow] 1 // Relative row number

Screen SAPMM08N.0201

GetTableAttribute T[Table] FirstVisibleRow=FVisRow _
LastVisibleRow=LVisRow LastRow=LastRow

  // First row on screen?
 if V[FVisRow=1]
   goto new_row

    // scroll to first line
 Enter "/ScrollToLine=1" Table="T[Table]"   

  label new_screen

Screen SAPMM08N.0201

  GetTableAttribute T[Table] FirstVisibleRow=FVisRow _
astVisibleRow=LVisRow LastRow=LastRow

   Set V[relrow] 1

   label new_row

  // end of table?
 if V[absrow>&V[LastRow]]
   goto end_of_table

    // end of screen?
 if V[absrow>&V[LVisRow]]
   Enter "/ScrollToLine=&V[absrow]" Table="T[Table]"
   goto new_screen

  Set V[selected] "&cell[Table,0,&V[relrow]]"

    // Selected?

  if V[selected=X]
   Set V[C1] "&cell[Table,Doc. no.,&V[relrow]]"
   Set V[C2] "&cell[Table,Invoicing party,&V[relrow]]"
   Set V[C3] "&cell[Table,Name of invoicing party,&V[relrow]]"
   Set V[C4] "&cell[Table,Gross amount,&V[relrow]]"
   AppendFile "&U[Filename]" C1 C2 C3 C4

  Set V[absrow] &V[absrow] + 1
 Set V[relrow] &V[relrow] + 1
 goto new_row 

  label end_of_table 

  CloseFile "&U[filename]"

    // Display file
 Start "&U[filename]" 

  // Back to line 1
 Enter "/ScrollToLine=1" Table="T[Table]"