Views Data Export

Export Views results to various file formats including CSV, JSON, XML, and XLS/XLSX with support for batch processing of large datasets.

views_data_export
104,558 sites
158
drupal.org

Install

Drupal 11, 10, 9 v8.x-1.8
composer require 'drupal/views_data_export:8.x-1.8'

Overview

The Views Data Export module extends Drupal's Views module to enable exporting view results to multiple file formats. It provides a dedicated "Data export" display plugin that can be attached to existing Views displays, allowing users to download filtered and sorted data in their preferred format.

The module supports two export methods: Standard (single request, suitable for small datasets) and Batch (processes data in chunks, ideal for large exports exceeding 2000 rows). Batch processing prevents memory exhaustion and server timeouts when exporting large amounts of data.

Key capabilities include configurable filename with token support, automatic file download after export completion, flexible redirect options after batch completion, and integration with Facets and Search API modules. The module also provides a Drush command for programmatic exports and supports customization through hooks.

Features

  • Exports Views data to CSV format via csv_serialization module with configurable delimiter, enclosure, escape character, HTML stripping, whitespace trimming, encoding, and BOM options
  • Exports Views data to JSON format using Drupal's built-in serialization
  • Exports Views data to XML format with configurable encoding, root node name, item node name, and pretty-print output options
  • Exports Views data to Microsoft Excel XLS/XLSX formats via optional xls_serialization module
  • Standard export method for small datasets processed in a single HTTP request
  • Batch export method for large datasets (>2000 rows) that processes data in configurable batch sizes to prevent memory exhaustion
  • Configurable export limit to restrict the maximum number of rows exported
  • Attach data export icons to other Views displays (Page, Block) for easy user access
  • Token support in filenames for dynamic file naming based on view context and date tokens
  • Automatic download option that triggers file download immediately after export completion
  • Flexible redirect configuration after batch completion: custom path, specific display, or front page
  • Query parameter preservation option to maintain filters when redirecting after export
  • Private and public file storage options for controlling access to generated export files
  • Integration with Facets module to respect facet selections in Search API views during export
  • Search API display plugin integration for proper handling of Search API-based views
  • Drush command (vde) for programmatic and scheduled exports from command line
  • Row-level customization through hook_views_data_export_row_alter() hook
  • Click-to-sort support in exported data maintaining table sort functionality

Use Cases

Export User Data for Compliance Requests

Create a View of user accounts with relevant fields (username, email, registration date, roles). Add a Data export display configured for CSV format with batch processing. Set the filename to 'user-export-[date:custom:Y-m-d].csv'. Administrators can use exposed filters to find specific users and download their data for GDPR or compliance requests.

Scheduled Content Audit Reports

Configure a View showing content with fields like title, author, created date, and status. Add a Data export display with CSV format and batch processing enabled. Use the Drush command in a cron job: 'drush vde content_audit data_export --output-file=/var/reports/content-$(date +%Y%m%d).csv' to generate daily audit reports automatically.

E-commerce Order Export Integration

Create a View of commerce orders with customer information, line items, and totals. Configure Data export with CSV format using semicolon delimiter for European Excel compatibility. Enable the 'Only use Symfony serializer->encode method' option for faster performance on large order datasets. Staff can export filtered orders for accounting systems.

Search API Results Export with Facets

For a Search API-powered product catalog View, add a Data export display. Configure the Facet source setting to match the attached page display's facet source. Users can apply facet filters (category, price range, attributes) on the page display, and the export will respect all selected facets when generating the CSV.

XML Feed Generation for External Systems

Configure a Data export display with XML format for content syndication. Set custom root_node_name ('products') and item_node_name ('product') to match the consuming system's requirements. Enable pretty-print for human-readable output during development. The resulting XML feed can be consumed by external inventory or catalog systems.

Multi-format Download Page

Create a page View with exposed filters for a data dashboard. Add multiple Data export displays - one for CSV, one for JSON, one for XML. Attach all three to the page display. Users see multiple download icons and can choose their preferred format while using the same filter criteria.

Tips

  • For very large exports (100k+ rows), consider reducing batch size to 500 and increasing PHP memory_limit to prevent out-of-memory errors during PhpSpreadsheet operations for XLS format.
  • Use token replacements like [date:custom:Ymd-His] in filenames to create unique files for each export and prevent caching issues.
  • Configure private file storage for sensitive data exports to ensure files are only accessible to authorized users through Drupal's access control.
  • When using batch mode, always add a unique sort criterion (like node ID ascending) to ensure consistent results across batch iterations.
  • The 'Only use Symfony serializer->encode method' option can significantly improve CSV export performance but only works with flat data structures - avoid with referenced entities or multi-value fields.
  • Test export functionality with a small dataset first before running on production data to verify field mappings and formatting options.
  • For scheduled exports via Drush, use the --uid option with an administrator account to ensure all content is accessible regardless of publication status.

Technical Details

Hooks 1
hook_views_data_export_row_alter

Allows modules to alter individual rows before they are exported. Called during the rendering process for each row in the view results.

Drush Commands 1
drush views_data_export:views-data-export

Executes a views_data_export display and writes the output to stdout or a file. Supports both standard and batch export methods.

Troubleshooting 8
Batch export shows 'Could not write to temporary output file' error

Ensure the private file system is properly configured in settings.php with a valid file_private_path. Verify the web server has write permissions to both public:// and private:// directories. Check if the views_data_export subdirectory can be created.

Export file is missing rows or has duplicate entries in batch mode

Sort the view on a unique identifier field (nid, uid, or entity ID). Without consistent sorting, batch pagination may skip or duplicate rows. This is documented in issue #3120563.

JSON automatic download doesn't work

Automatic download is intentionally disabled for JSON format to prevent browsers from displaying raw JSON data. Users must manually click the download link for JSON exports.

CSV opens incorrectly in Excel with merged columns

Configure the CSV delimiter to match your locale's list separator. European versions of Excel may expect semicolons instead of commas. Enable UTF-8 BOM option if special characters appear corrupted.

Export is slow or times out for large datasets

Switch from Standard to Batch export method. Set batch size between 500-2000 rows depending on server resources. Enable 'Only use Symfony serializer->encode method' for CSV exports of flat data.

Facet filters not applied to export

Ensure the Facet source setting in the data export display matches the facet source of the page display it's attached to. The facet source ID format is typically 'search_api:views_page__VIEW_ID__DISPLAY_ID'.

Drush export command returns empty results

Use the --uid option to run the export as a user with appropriate permissions. Anonymous or the Drush user may not have access to the content being exported.

XML export has wrong encoding or node names

Configure XML settings in the style options. Set encoding to UTF-8 for international characters. Customize root_node_name and item_node_name to match your XML schema requirements.

Security Notes 5
  • Export files contain potentially sensitive data. Use private file storage (private://) rather than public when exporting user information, orders, or other confidential content.
  • The module implements hook_file_access() to restrict download access to files generated by the current user's export session. However, files in public:// are accessible via direct URL.
  • Drush command --uid option allows running exports as any user - restrict shell access to trusted administrators only.
  • Configure View access permissions appropriately as data export inherits the View's access settings. Users can only export data they can see in the attached display.
  • Be cautious with automatic download option in shared or public computer environments as files download without additional confirmation.