Export data to Excel wordpress

Export data to Excel wordpress

Wordpress

In order to export data to Excel, you need to connect PHPExcel.php. This library is needed download, and upload to your theme folder.
We connect it with the following code in the functions.php of your theme:

require_once 'Classes/PHPExcel.php';

Next we need to call the export function, I used the GET request:

if (isset($_GET['excel-export'])) {

}

You also need to think about security, for example, so that this request can only be performed by the administrator:

if (current_user_can('manage_options')) {

}

In the example, I will consider exporting user data, in principle, this option can be altered to fit your needs, because he is universal. The entire request code will look like this:

if (isset($_GET['excel-export'])) {

    if (current_user_can('manage_options')) {

        add_action('init', function () {

            error_reporting(E_ALL);

            if (PHP_SAPI == 'cli')
                die('This example should only be run from a Web Browser');

            require_once 'Classes/PHPExcel.php';

            $objPHPExcel = new PHPExcel();

            // Set properties
            $objPHPExcel->getProperties()->setTitle(esc_html__('Results', 'mytheme'));
            $objPHPExcel->getProperties()->setSubject(esc_html__('Results', 'mytheme'));
            $objPHPExcel->getProperties()->setDescription(esc_html__('Export users document for XLSX.', 'mytheme'));

            // WP_User_Query  get users by specified criteria
            $args02 = array(
                'role' => 'subscriber',
                'order' => 'ASC',
                'orderby' => 'display_name',
                'fields' => 'all',
            );

            // The User Query
            $blogusers02 = get_users($args02);
            $cell_counter = 1;

            // Paint the background with our headlines.
            $objPHPExcel->getActiveSheet()->getStyle('A1:AM1')->getFill()
                ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
                ->getStartColor()->setARGB('a5b6ca');

            // Sign the columns
            $objPHPExcel->getActiveSheet()->SetCellValue('A1', esc_html__('Name', 'mytheme'));
            $objPHPExcel->getActiveSheet()->SetCellValue('B1', esc_html__('E-mail', 'mytheme'));
            $objPHPExcel->getActiveSheet()->SetCellValue('C1', esc_html__('Profession', 'mytheme'));
            $objPHPExcel->getActiveSheet()->SetCellValue('D1', esc_html__('Role', 'mytheme'));
            $objPHPExcel->getActiveSheet()->SetCellValue('E1', esc_html__('Description', 'mytheme'));
            

              // Actually the cycle itself:
            foreach ($blogusers02 as $user) {
                $cell_counter++; // The counter, it is needed to move to the next line.


                $meta = get_user_meta($user->ID);
				// Fetching data from standard fields
                $role = $user->roles;
                $email = $user->user_email;
                 // Fetching data from custom fields
                $first_name = (isset($meta['user_first_name'][0]) && $meta['user_first_name'][0] != '') ? $meta['user_first_name'][0] : '';
                $profession = (isset($meta['user_profession'][0]) && $meta['user_profession'][0] != '') ? $meta['user_profession'][0] : '';
                $description = (isset($meta['user_description'][0]) && $meta['user_description'][0] != '') ? $meta['user_description'][0] : '';

                // Fill the table cells
                $objPHPExcel->setActiveSheetIndex(0);
                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $cell_counter . '', $first_name);
                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $cell_counter . '', $email);
                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $cell_counter . '', $profession);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $cell_counter . '', $description);



            }

            // Set automatic width for cells
            for ($col = 'A'; $col !== 'D'; $col++) {
                $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
            }

            // Give the tab name
            $objPHPExcel->getActiveSheet()->setTitle(esc_html__('Results', 'mytheme'));
			

            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="SbmChampions2019Survey.xlsx"');
            header('Cache-Control: max-age=0');

            // Save the file
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');

            exit();
        });

    }
}

As a result, an Excel document will be saved on your PC with user data.
In principle, instead of selecting user data, you can select anything, whether it is a page, post or product, and substitute the desired data.

If you need to get a document with two or more tabs, then you need to write the following code after the rename code of the first tab:

$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);

This code creates a new tab and makes it active.

FAQ

Common questions about WordPress project work and ongoing support.

What kinds of WordPress sites do you work with?

I work with business WordPress sites, WooCommerce stores, and projects that need to grow step by step — without unnecessary complexity or fragile solutions. This includes both new launches and existing sites that need refinements, support, or careful technical updates.

Can you improve an existing site without a full redesign?

Yes. Most tasks involve sites that are already live: new sections, functional improvements, WooCommerce changes, performance optimisation, or technical fixes. The goal is for the site to stay stable, manageable, and easy to develop further.

What types of tasks do you take on most often?

Most often this is custom WordPress development, Figma design implementation, WooCommerce refinements, support for live sites, fixing technical issues, and practical improvements over time. In selected scenarios I also help with light AI automations for content, enquiries, or internal admin workflows.

Do you work on WordPress speed and performance?

Yes. WordPress optimisation usually involves reviewing the theme, plugins, images, fonts, page structure, and the site's overall technical neatness. The aim is not a "magic button", but practical changes that make the site faster and easier to maintain.

Do you use AI when working on WordPress sites?

Yes, but only where it genuinely helps the process. AI works best for content drafts, FAQ or support assistants, enquiry handling, and small automations around an existing WordPress site. The approach is simple: AI should reduce routine work, not complicate the site.

What do I need to prepare to discuss a task or project?

Usually a short description of the site, the task, the desired outcome, and examples or technical constraints if you have them is enough. If the project is already live, it also helps to share the current site or a staging environment — this makes it easier to understand the scope of work.

Can I get in touch not for a new site, but for support or specific improvements?

Yes. I work not only on new builds, but also on live sites that need to be maintained, fixed, and improved gradually. This can include new pages, admin changes, WooCommerce refinements, small UX improvements, or technical maintenance.

Need help with a WordPress project?

If you need custom WordPress development, design implementation, or support for an existing site, I would be happy to review the project.