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

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条