Xlexport

Top  Previous  Next

The procedure Xlexport can be used to export data from a GAMS program into a spreadsheet.  It writes data into spreadsheets only when it finds row and column labels that match the set elements within the specified target range.  Those elements also control element ordering.  Command format is that above requiring specification of

Name of the item in the GAMS program to have data exported from it,
The target spreadsheet,
The location in the target sheet including the sheet name and cell range.

There is also an optional merge parameter telling whether to leave preexisting data with nonmatching data elements alone.

Example:

(fromExcel.gms)

Solve tranport using lp minimizing totalcost ;

$libinclude xlexport transport.l myspread.xls output3!a1..d4

$libinclude xlexport transport.m myspread.xls output3!f1..i4

$libinclude xlexport transport.l myspread.xls output3!a6:d8  /m

$libinclude xlexport transport.l myspread.xls output3!f6:i9  /m

 

This copies the data for the solution levels (.l) or marginals (.m) of the variable named transport into the spreadsheet called myspread.xls into various ranges depending on the statement.  The first command exports the data after the solve statement into the sheet called output3 in the range a1 to d4.  The third command exports the data after the solve statement into the sheet called output3 in the range a6 to d8 merging in the results.

 

The output3 sheet before the import looks like

 

_img81

 

and the sheet afterward looks like

 

_img82

 

where the order of the results varies according the order in which the column labels and row labels appear in the spreadsheet.  Also note under the merge option that the data present in the spreadsheet before the export remain afterward when the labels do not match as in the case of the random column in column D rows 6-7.  The exports other than the first one do not fully match all of the column dimensionality of the transport.l item and thus only export subsets of the items.

Notes:

The use of Xlexport either requires the spreadsheet to be closed or it to be shared through the Excel Tools Share Workbook option.  However, if shared, the data put by GAMS will only be reflected in the workbook if you do a file save and the procedure may be very slow.  Often the best option is to close the workbook.
The use of Xlexport permits simplicity of data transfer but is not as versatile as the use of Gdxxrw as described below.
Xlexport requires that the set elements have already been specified in the spreadsheet and must be matched up.
Xlexport sends whatever data is current for an item at the stage of the program where the libinclude occurs and reflects any previous calculations and model solutions.
The merge option leaves prior data alone but does wipe out any formulas in the Xlexport range.
The range should be explicitly specified if one intends to not have the procedure wipe out the data (if merge is off) and formulae from one row below and one column to the left of the insertion point (if just j1 is specified then the spreadsheet is wiped from k2 to the bottom and left.