doujiaozhan2413 2018-03-26 17:10
浏览 83
已采纳

drupal 7:使用视图批量操作创建xls文件

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

  • 写回答

1条回答 默认 最新

  • douyong1285 2018-03-28 16:30
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行