Purpose
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.

Solution
- 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:

Result:

GuiXT Script SAPMM08N.D0201.txt:

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

InutScript "savetoexcel.txt:

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

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

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

// Variables
Set V[absrow] 1 // Absolute row number
Set 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
  endif

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

  label new_screen

Screen SAPMM08N.0201

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

   Set V[relrow] 1

   label new_row

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

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

  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
  endif

  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]" 

Components
InputAssistant