Score:-3

How do I export a list of users into an excel spreadsheet in Drupal 9

us flag

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?

Krishna Mohan avatar
sh flag
if your requirement is to only export users to excel, but not to import - Yes views data export is one of the best option.
Score:1
br flag

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.

Score:0
co flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.