Export to Excel or Ascii or Custom Format

Description

Export data to Excel or Ascii or Custom Format. The Custom format option allows you to define an Xbasic function that creates the file.

Action Javascript action available in the Grid Component that exports records in the Grid Component to Excel, Ascii, or a Custom File format.

images/exportExcelAsciiCustom.png

Export Data Properties 

  • Export type 

    Specify the format of the exported data.

    Export type
    Description
    Excel

    Export as an .xls (Excel 2003) document.

    Excel-XLSX

    Export as an .xlsx (Excel 2007) document.

    Comma delimited ascii

    Export as a comma delimited ASCII file.

    Tab delimited ascii

    Export as a tab delimited ASCII file.

    Custom

    Export to a custom format defined with an Xbasic function.

  • Custom format Xbasic function 

    Shown if Export type is Custom. The name of the Xbasic function that creates the custom export file. The function must be defined in the Xbasic Function Declarations section.

    The following variables are available to your Xbasic function:

    e.tbl

    Pointer to the table or record set of the from which data is to be exported.

    e.itbl

    In the case of DBF data, a pointer to the index after the table has been queried.

    e.datatype

    Either 'sql' or 'dbf', depending on what type of table you are exporting data from.

    e.exportfilename

    Name of the file you must create with the exported data.

    Your function can set the e.abort variable to .t. halt file download. For example:

    function exportFormat as v (e as p)
        ' Abort export
        e.abort = .t.
    end function

    The example below loops through all records in a SQL result set and creates a custom export file:

    function exportFormat as v (e as p)
        'Example (for SQL data):
        'Loop through all records in the result set to create the custom export file.
        DIM txt AS C = ""
        DIM flagLoop AS L = .t.
        WHILE flagLoop
            txt = txt + e.tbl.data("field1") + e.tbl.data("field2") + crlf()
            flagLoop = e.tbl.nextRow()
        END WHILE
    
        ' Create file
        file.from_string(e.exportFilename,txt)
    
    end function

    The Show Function Prototypes link at the bottom of the Action Javascript dialog can be used to generate a function prototype. Enter the name of the Xbasic function you want to create, then click Show Function Prototypes. Next, select Server Side - Custom Export. Click Copy Code to Clipboard to copy the function prototype. Then, click Open Xbasic Function Declarations and paste the function definition.

  • Source 

    Specify the source of the data export:

    Source
    Description
    1 - All Fields in Grid Component

    Exports all data displayed in the Grid part of the component. Fields that are not displayed in the Grid part are not exported.

    2 - All Fields in Grid Query

    Exports all fields from the query the Grid component is based on. This includes fields that may not be displayed in the Grid part.

    3 - Selected Fields in Grid Query

    Export all data from a list of field specified in the Field list property.

    4 - Custom

    Export data from the Grid or a custom SQL or DBF query defined using an Xbasic function.

  • Client-side filename 

    Specify the default filename to use when saving the file on the user's computer. The user can override the suggested name. If you set the name to <Default>, Alpha Anywhere will generate the filename.

  • Zip file 

    Specify if the exported file should be zipped before it is downloaded to the user's computer.

  • Export hidden fields 

    Should 'hidden' fields be exported? ('Hidden' fields are not necessarily actually hidden when the Grid is displayed - they could be shown in another column's Free-form layout section).

  • Use Grid column headings 

    Should the column headings in the export file be the same as the Grid Column Headings? If unchecked, the field name is used.

  • Field list 

    The list of fields to export.

    The column headings in the Excel file will use the field name. If you want to specify different column headings from the fields, you can use the following syntax to define a custom column heading:

    fieldname [column heading]

    For example:

    firstname [First Name]
  • Record selection 

    Specify if only the records in the current Grid query should be exported, or all records in the Grid. Choices include Records in current query, All records, and Current record only.

  • Maximum number of records 

    Specify the maximum number of records to export. Set to 0 for no limit.

Custom Export Properties 

These options are shown if the Source is set to 4 (Custom).

  • Custom type 

    Specify if the query is against DBF tables or a SQL database. Choices include DBF, SQL.

  • DBF Table 

    Specify the name of the DBF table.

  • Field names 

    Specify the names of the fields in the DBF table that you want to export.

  • Filter - DBF 

    Specify the filter expression.

  • Order - DBF 

    Specify the filter expression.

  • Connection string 

    Specify the connection string.

  • SQL Select 

    Specify the SQL Select statement.

  • Filter - SQL 

    Specify the filter expression. (This is in addition to any WHERE clause defined in your SELECT statement.)

  • Order - SQL 

    Specify the filter expression.

  • Arguments 

    You can use arguments in the filter expressions that you define. Using arguments for variable data makes it easier to write your filter expressions.

  • Argument bindings 

    Specify where each argument that have defined gets its value. Arguments can get their values from a value in the current Grid row, a session variable, or a literal value.

Events - Server-Side Properties 

  • After export 

    The name of an Xbasic function to call after the records have been exported and the export file has been created, but before the file has been sent to the browser. Function is defined in Xbasic Function Declarations section.

    e.exportFilename

    The name of the file that was created. This file will be downloaded to the user's computer.

    e.tmpl

    Grid properties

    e.rtc

    Runtime calculations - you can use this to store data to be passed to other server side events. This variable cannot be used to persist state information.

    Your Xbasic function can set the following variables:

    e.abort

    Set to .t. to prevent the file from being downloaded.

    e.rtc.abortAfterExportJavascriptEvent

    Set to .t. to suppress the AfterExport JavaScript event from firing.

    For example, to prevent the export file from being downloaded:

    function afterExport as v (e as p)
        ' Abort the file export
        e.abort = .t.
    end function

    JavaScript can optionally be returned from the Xbasic Function in the e.rtc variable. For example:

    function afterExport as v (e as p)
        ' Display a popup alert on the client:
        e.rtc.A_AjaxResponses[].text = "alert('Export complete.');"
        e.rtc.A_AjaxResponses[..].id = 100000
    end function

    The Show Function Prototypes link at the bottom of the Action Javascript dialog can be used to generate a function prototype. Enter the name of the Xbasic function you want to create, then click Show Function Prototypes. Next, select Server Side - After Export. Click Copy Code to Clipboard to copy the function prototype. Then, click Open Xbasic Function Declarations and paste the function definition.

Events - Client-Side Properties 

  • Before export 

    Specify the name of the JavaScript function to call before records are exported. This function must return true, or false. If false, export is aborted. Function is defined in Javascript Function Declarations section.

    This function can be used to prevent exporting data if certain conditions are not met. It can also be used to open a "wait" message:

    function canDoExport() {
        // Show wait message:
        var title = "Exporting...";
        var message = "Please wait for the export to complete.";
        var buttons = "none";
        var onClose = function () {};
    
        A5.msgBox.show(title, message, buttons, onClose);
    }

    The Show Function Prototypes link at the bottom of the Action Javascript dialog can be used to generate a function prototype. Enter the name of the JavaScript function you want to create, then click Show Function Prototypes. Next, select Client Side - Before Export. Click Copy Code to Clipboard to copy the function prototype.

  • After export 

    Specify the name of the JavaScript function to call before after the records have been exported and the file has been downloaded to the browser. Function is defined in Javascript Function Declarations section.

    This function can be used to trigger another action, such as closing a wait message shown when the export started:

    function afterExport() {
        // Hide wait message:
        A5.msgBox.hide();
    }

    The Show Function Prototypes link at the bottom of the Action Javascript dialog can be used to generate a function prototype. Enter the name of the JavaScript function you want to create, then click Show Function Prototypes. Next, select Client Side - After Export. Click Copy Code to Clipboard to copy the function prototype.

Troubleshooting 

  • I can't download the exported file from the server 

    Only files whose file extension is listed in the Allowed filenames property in the Project Properties can be downloaded from the Application Server. If the file type you've specified in this action is not listed, the file may not download when the action completes.

    To resolve this issue, open the Web Projects Control Panel add the file extension (e.g. .txt, .xls, .zip) to the Project Properties > File Download > Allowed filenames property.

Limitations

Grid Component Only

See Also