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 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题