method Grid.Export ([Destination as Variant], [Options as Variant])
Exports the control's data to an array, CSV, or HTML format, or saves it to an ADO or DAO recordset

TypeDescription
Destination as Variant A Variant expression that defines the destination, either as a file or format to be created, or as an object for exporting data. The Destination parameter specifies the format to be created as follows:
  • {string} "array", indicates that the Export method returns the control's data as a two-dimensional array
  • {string} if "htm" or "html", the control returns the HTML content as a string, including the CSS styles
  • {string} Any file-name that ends on ".htm" or ".html" creates the file with the HTML format inside
  • {object} An ADOR, ADODB, or DAO recordset where the data is saved or exported. The second field of the Options parameter specifies the columns to export. The "unique" option identifies the column that must not contain duplicate entries and is used by ADO/DAO to synchronize records. The Export method returns an empty string if no error occurs; otherwise, it returns the error itself.
  • missing, empty, or any other case the Export returns the control's data in CSV format.

No error occurs, if the Export method can not create the file.

Options as Variant A String expression that defines the options for exporting data, separated by the | character in the format "items|columns|separator|unicode", where items can be:
  • "all", displays all items, including the hidden or collapsed items
  • "vis", includes only the visible items, excluding child items of collapsed elements, non-visible items (such as those with a height of 0), and items that do not match the current filter (default if missing)
  • "sel", exports only the selected-items
  • "chk", exports all checked and visible items (the first column in the columns list specifies the index of the column used to query the checkbox state)
  • "leaf",  exports only the leaf items of the control, excluding parent or grouped items
  • "only[byindex](type1,type2,!type3,...)", allows filtering specific item types for export, where types lists the items to include, separated by commas. For example, "only(sortable, selectable, !divider)" exports only sortable and selectable items while excluding dividers. Supported types include: locked, selected, visible, checked, sortable, selectable, divider, leaf, and expanded. The checked type can specify a column index using brackets. For example, "checked[1]" exports items that are checked in the column with index 1. You can exclude a type by prefixing it with !
ReturnDescription
VariantThe result of the Export method is a:
  • {array} two-dimensional array, if the Destination is "array". For instance Export("array","vis") method exports the control's data as it is displayed into a two-dimensional array (zero-based). The result includes the columns headers into the first line, while the rest of lines contains the control's visible data. For instance, Export("array", "vis")(1, 5) returns the value of the cell on the second column and fifth row.
  • {string}, indicates the format being exported, which could be CSV or HTML based on the Destination parameter. If the Destination is an ADO or DAO recordset, it returns an empty string or an error
The Export method exports the control's data to a two-dimensional array, CSV, HTML, or an ADO/DAO recordset. It can include columns from selected, visible, checked, or all items. By default, all items are exported unless a filter is applied, in which case only visible items are included. The CSV format contains only raw data, while the HTML format preserves the visual appearance using CSS styles.

For instance:

Let's say we have the following control's DATA:

The following screen shot shows the control's DATA in CSV format:

The following screen shot shows the control's DATA in HTML format:

The Options parameter consists a list of fields separated by | character, in the following order:

  1. The first field specifies the scope of items to export and can be one of the following options: all, vis, sel, chk, leaf or only.
    • The all option exports all items, including hidden or collapsed ones, and is used as the default if the field is missing.
    • The vis option exports only visible items, excluding child items of collapsed elements or items that are not visible (e.g., items with a height of 0). The sel option exports only the items that are currently selected.
    • The chk option exports all checked and visible items; when using this option, the first column in the columns list must indicate the index of the column used to query the checkbox state.
    • The leaf option exports only the leaf items of the control, excluding parent or grouped items, ensuring that only the lowest-level data entries are included in the export.
    • The "only" keyword allows filtering specific item types for export. The item types are listed in parentheses, separated by commas. Supported types include: locked, selected, visible, checked, sortable, selectable, divider, leaf, and expanded, as explained below.
      Syntax:

      only[byindex](type1,type2,!type3,...)

      Supported scopes:
      • locked, includes items that are locked to the top or bottom of the control, meaning they remain fixed in place and are not scrollable
      • selected, includes items that are currently selected
      • visible, includes items that are included in the current filter (i.e., items that are not hidden due to filtering)
      • checked, includes items that are checked. a column index can be specified using brackets, e.g., "checked[1]" exports items checked in column 1
      • sortable, includes items that can be reordered (sortable items)
      • selectable, includes items that can be selected
      • divider, includes divider items, which are typically non-interactive items used for separation
      • leaf, includes items that do not have child elements (leaf nodes in a tree structure)
      • expanded, includes items that are currently expanded, showing their child elements

      The "byindex" flag can be specified inside square brackets ([]) to indicate that the items should be listed by their original index order rather than their current display order.

      Negating a scope (!scope)

          Prefixing a scope with ! excludes items of that type.

      Examples:

          "only(selected)", exports only selected items
          "only(!leaf,selected)", exports only selected parent items (excluding leaf items)
          "only(sortable,selectable,!divider)", exports sortable and selectable items, but not dividers
          "only(checked[1])", exports only items that are checked in column index 1
          "only(locked)", exports only items that are locked to the top or bottom (not scrollable)
          "only(visible)", exports only items that match the current filter criteria
          "only[byindex](selected)", exports selected items in their original dataset order rather than their current display order

  2. The second field specifies the columns to be exported. If omitted, all visible columns are included. Columns are listed by index, separated by commas, determining their order in the exported data. The first column in the list is used as the queried column when the chk option is applied. Additionally, each column can specify whether to export the cell's value (default for Array), caption (cap), or checkbox state (chk). For example, "0(cap),1(cap),2,3" exports captions for the first two columns and values for the rest, while "0(chk)" exports the checkbox state of the first column. When saving data to an ADO or DAO recordset, the unique option identifies a column that must not contain duplicate entries and is used by ADO/DAO to synchronize records. For example, the Options property "only(selectable,sortable,!divider)|0(unique),1(cap),2(cap),3,4,5" indicates that the Export method will export only selectable, sortable, and non-divider items, and columns from 0 to 5. Columns 1 and 2 will be exported by caption, while the others will be exported by value. Additionally, the first column (0) is designated as having no duplicates. It is recommended to specify a unique column when exporting to ADO/DAO recordsets. The Export method returns an empty string if no error occurs; otherwise, it returns the error itself.
  3. the third field indicates the character to separate the fields inside each exported line [tab character-if missing]. This field is valid, only when exporting to a CSV format
  4. the forth field could be ansi or unicode, which indicates the character-set to save the control's content to Destination. For instance, Export( Destination,"|||unicode" ) saves the control's content to destination in UNICODE format (two-bytes per character ). By default, the Export method creates an ANSI file (one-byte character)

The Destination parameter indicates the file to be created where exported date should be saved. For instance, Export( "c:\temp\export.html") exports the control's DATA to export.html file in HTML format, or Export( "","sel|0,1|;") returns the cells from columns 0, 1 from the selected items, to a CSV format using the ; character as a field separator.

The "CSV" refers to any file that:

The "HTML" refers to any file that:

You can use the Copy/CopyTo to export the control's view to clipboard/EMF/BMP/JPG/PNG/GIF or PDF format.