-2

I'm using a Drupal 9 website and would like to export a list of the users into an excel spreadsheet. Would using the Views data export module be the best way to achieve this?

1
  • 4
    if your requirement is to only export users to excel, but not to import - Yes views data export is one of the best option. Commented Aug 16, 2021 at 6:52

2 Answers 2

2

Assuming you are willing to use drush: Create a PHP script, named something like users_to_csv.php

<?php

$fields = [
            'name',
            'mail',
            'langcode',
            'preferred_langcode',
            'timezone',
            'created',
            'changed',
          ];

$users = \Drupal::entityTypeManager()
            ->getStorage('user')
            ->loadByProperties([
              // some properties here if you care to restrict the users list
            ]);

foreach ($users as $user ) {

  foreach ($fields as $field) {

    echo $user->get($field)->getValue()[0]['value'] . ', ';

  }

  echo PHP_EOL;

}

Then run it with

drush php:script users_to_csv.php > users.csv

The fields list is up to You.

1

If it's a quick on-off, you could write a simple View and highlight and copy (Control C) the Users list on the page to a text file, or use Views Data Export and export as a CSV.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.