Search code examples
phpdrupal-7phpexcelxlsvbo

drupal 7 : create xls file with views bulk operation


I'm trying to generate an excel file using PHPExcel Module in the action_info function with the following code :

function mymodule_export_data_action(&$object, $context = array()) {
if (isset($object->uid)) {
    $uid = $object->uid;
}
elseif (isset($context['uid'])) {
    $uid = $context['uid'];
}
if ($uid) {
    module_load_include('inc', 'phpexcel');
    $filename = 'mymodule--download-' . uniqid() . '.xls';
    $filepath = variable_get('file_public_path', conf_path() . '/files') . '/' . $filename;
    $result = phpexcel_export(
            array('Nom', 'Prenom', 'Date de naissance', 'Adresse email'), 
                    array(
                    array('A1', 'B1'),
                    array('A2', 'B2'),
                  ), $filepath);
    if ($result === PHPEXCEL_SUCCESS) {
        drupal_set_message(l('Click to download', $filepath));
    }
    else {

    }
}

}

This is working pretty fine when having just one node, but when there's more than one it generates a new file for each one, which also good but my purpose is to have one file for all nodes. It has been days and I really hope for someone to put me in the right direction.

Thank you in advance


Solution

  • Here is the solution by using views module, views_data_export module, 2 lines of PHP code and some lines of jQuery.

    Just follow these steps:

    1st Install views and views_data_export modules

    2nd (a) Create views page and data export, this video will help you to export the data according to the filter(s)

    2nd (b) Don't forget to add nid field in the views page that will use to get NIDs

    2nd (c) Now, create one more views data export (that again starts here, if you need) and create exporting PATH different than the first data export (created on step 2nd (a)) but keep remember you don't have to update Attach to option under the DATA EXPORT SETTINGS section, it should looks like this Attach to: none.

    2nd (d) Now, add nid as contextual filter in the views and choose Provide default value = Content ID from URL like this and check the checkbox Allow multiple values like this

    3rd Add two lines of PHP code somewhere in the template.php OR top of the tpl of views page if you have already created (by the way I did it with the tpl named as views-view--export-multiple-rows--page.tpl.php).

    if($_SERVER['REQUEST_METHOD'] == 'POST') {                 
        drupal_goto("export_selected/".implode(',', $_POST['export']));
    }
    

    4th Add below jQuery code in the JS file that will be render on this page like custom.js and **change classes **

    jQuery(function($){
        jQuery('.feed-icon a:first-child').text('Export All');
    
        jQuery('td.views-field-nid').each(function() { //class of NID added in step 2nd (b)
            var t = jQuery(this);
            var id = jQuery.trim(t.text());
            t.html('<input type="checkbox" name="export[]" value="" />');
            t.find('input').val(id);
        });
        //Below .view-export-multiple-rows class is of views class (main views page)
        $('.view-export-multiple-rows').wrap('<form method="POST" class="export-form"></form>');
        $('.export-form .view-content').append('<input type="submit" value="Export Selected" name="submit" />');
    });
    

    If you follow all these steps properly, I believe you have done with:

    • To export all rows
    • To export filtered rows
    • To export specific rows

    I hope this will help you or at least give you an idea.

    Thanks