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

drupal 7 : create xls file with views bulk operation

我正在尝试使用 action_info 函数中的 PHPExcel 模块生成一个 excel 文件,代码如下:

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 {

    }
}

}

这在只有一个节点时工作得很好,但当有多个节点时,它会为每个节点生成一个新文件,这也很好,但我的目的是为所有节点创建一个文件。已经好几天了,我真的希望有人能把我引向正确的方向。

提前致谢

这是使用 views module, views_data_export 模块、2 行 PHP 代码和一些 jQuery.

代码行的解决方案

只需按照以下步骤操作:

1st 安装 viewsviews_data_export 模块

第 2(a) 创建视图页面和数据导出,this video 将帮助您根据 过滤器

2nd (b) 不要忘记在将用于获取 NID 的 views page 中添加 nid 字段

2nd (c) 现在,创建 另一个视图数据导出 (又是 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 部分,它应该看起来像这样 Attach to: none.

2nd (d) 现在,在视图中添加 nid 作为 contextual filter 和选择提供默认值 = Content ID from URL like this and check the checkbox Allow multiple values like this

3rdtemplate.php OR 的 tpl 顶部某处添加两行 PHP 代码查看页面 如果你已经创建了(顺便说一句,我是用名为 views-view--export-multiple-rows--page.tpl.php 的 tpl 做的)。

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

4th 在将在此页面上呈现的 JS 文件中添加以下 jQuery 代码,如 custom.js 和 **更改 类 **

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" />');
});

如果您正确执行所有这些步骤,我相信您已经完成了:

  • 导出所有行
  • 导出筛选的行
  • 导出特定行

我希望这对您有所帮助,或者至少给您一个想法。

谢谢