This is a quick module that can export a view as a Comma Separated Values (CSV) file, or as a fake Doc (HTML table with msword header) file. It is similar to, but not the same as this extension to the 4.7 views_bonus module, but this module works in drupal 5.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

wrunt’s picture

FileSize
182 bytes

Here's the .info

appel’s picture

Thanks wrunt, seems to work well for me on Drupal 5.1.

webchick’s picture

Oh this looks great! Subscribing.

Robardi56’s picture

Hi,
this is exactly what I was looking for, but i'm not sure how to install this ?

Should I install views bonus pack first ?

If not, should I just dump the views_bonus_export.module.txt in my module folder ? And what to do with the .info file ?

Thanks,
Brakkar

Boris Mann’s picture

Ah, yes, this would be an excellent addition to the Views Bonus Pack

webchick’s picture

Status: Needs review » Reviewed & tested by the community

This did the job for us on the OSCMS site fantastically. It had a couple little bugs but nothing too major that couldn't get cleaned up in subsequent patches (exporting empty lines and the like -- could also have been something stupid I was doing).

dmitrig01’s picture

Checking this tonight. If I like it, I will commit it. :)

dalin’s picture

In the original module above, you need to clone a view and change it to the style "Views Bonus: CSV file" or "Views Export: Doc file" and then link to that from the original view. This is not a good workflow if you want to be able to export any view. So I've added a perm for 'export views as CSV and DOC' and if the current user has access, show buttons at the bottom of every view for exporting.

dalin’s picture

FileSize
402 bytes

Here's the .png
Taken from serach_export.module so I'm pretty sure that it's GPL

dalin’s picture

FileSize
323 bytes

and a doc.png
I just made this so it's GPL

dmitrig01’s picture

Status: Reviewed & tested by the community » Needs work

I have some comments:
1) great idea
2) the permissions should be separate for doc and cvs

hintbw’s picture

Status: Needs work » Needs review
FileSize
5.49 KB

I have attached an updated module with the following changes:

1. Permissions for export to CSV and DOC have been separated out
2. CSV and Doc export from existing views now supports filters as well (it didn't before)

Essentially the module will now export from any view and it will honor the currents status of any exposed filters that are set for that view. It would be great if someone could review it and if we could at least get an initial commit of this module into the views bonus back once code review has happened.

hintbw’s picture

I should add that the module I attached in #12 is an updated version of the module dalin attached in #8.

goose2000’s picture

Category: feature » support

Uh, this is a little confusing. Is there a module package to download with code and graphics etc...

solimeno’s picture

Hello everyone,

Although I'm getting in on this thread late, I do want to say "Thank You" to all for this great idea and implementation. It is a perfect fit for my application. I did run into a bit of a glitch though:

using the module code from post #12 by hintbw I encountered an error message indicating "missing argument 3" on line 161. In looking at the code (and I am a rank amateur, so bear with me) it appeared to be an issue with the $output variable. To test out my hypothesis I deleted this from line 161, leaving only two arguments. I saved the changed module and then it seemed to work fine. From a view of a full table (no filters) I can export both cvs and doc formats nicely. However, in attempting to do any filtering with a view I get a "Page not found" result.

It looks like I took a small step forward but met with limited success (fixed one thing and broke another). Can any of you help me out to fix this?

Bob

solimeno’s picture

I forgot to mention that I am currently using Drupal 4.7. Although I saw that this modules works with version 5 and is not the same as a similar one written explicitly for 4.7, I wanted to incorporate the cvs and doc button features that were developed here. As I stated in my previous post, my simple mod (deleting the $output argument from line 161) works very well with a full table view but not with a filtered view.

solimeno’s picture

Category: support » feature

Something that would make this module even better would be improved handling of multiple value fields. I've experimented a bit this afternoon with a csv export of a record with a multiple value field (numerical data) and unfortunately all values get dumped into a single "cell" when imported into a spreadsheet.

Ideally, since this is a one->many relationship the export could output consecutive rows (obviously repeating single value vield data) for as many multiple values in a m.v. field. This is useful, for example, in compiling statistics of repetitive measurements stored in this fashion for a single experimental sample. The view is displayed nicely in Drupal and in the doc export, but the csv export is a problem if you want to work with values in a spreadsheet.

Thinking out loud: Would it be "better" (easier) to create calculated, single value fields in Drupal and then export that versus what I described above?

John Morahan’s picture

I updated the module to fix the problems mentioned in #15 and a couple of other little things I noticed. New version attached, based on #12.

solimeno’s picture

Component: Code » User interface
Category: feature » bug

I upgraded my development site from drupal 4.7.x to drupal 5.1, and I cannot seem to get this module to work at all. I've tried using the module code posted in #12 (that was still buggy but working in drupal 4.7.x) and also the newest code patch in #18, but I've never been able to see either version operational with the CSV and DOC buttons since the upgrade to drupal 5.1 -- the views, views_bonus, and other modules are all in drupal-5.1/sites/all/modules and I placed the views_bonus_export.module file in the views_bonus folder.

Here's the status report for my site (if this helps):

Drupal 5.1
Configuration file Protected
Cron maintenance tasks Last run 9 min 2 sec ago
You can run cron manually.
Database schema Up to date
Drupal core update status Up to date
File system Writable (private download method)
Module update status Up to date
MySQL database 5.0.32
PHP 4.4.4-8+etch3
Unicode library PHP Mbstring Extension
Web server Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/4.4.4-8+etch3 mod_perl/2.0.2 Perl/v5.8.8

John Morahan’s picture

Both versions work in 5.1 for me. Did you remember to add the views_bonus_export.info file?

goose2000’s picture

Hi, have it and it installed ok me thinks on 5.1 . Um, but how do I add this to my view? I missed some instruction somewhere?

goose2000’s picture

Opps Sorry, just found it. ReadMe.txt eventually in the works no doubt.

It's very cool, thank you.

solimeno’s picture

Ooops! I was indeed missing the .info file. Works GREAT! Thank you, John.

dmitrig01’s picture

could you please file a *real* patch, and try to make this work with http://drupal.org/node/93868?

jasonwhat’s picture

+1, this is a great feature and it would be best to get it into CVS there is versioning and more users testing, etc.

dmitrig01’s picture

Category: bug » feature
Priority: Normal » Critical
Status: Needs review » Needs work

PLEASE ATTACH A PATCH

emilyf’s picture

I have tried integrating this with my site. Installed the .info and .module files (latest). Turned on the feature and gave permissions to export/import. However, I don't see how to actually use it. I've tried it from the import/export module interface, and I've tried it from views > export. It all seems the same as it was. My understanding is that this will actually allow me to export the DATA of the view. Am I wrong? Can anyone provide further instruction as to the use of this?
Thank you.

solimeno’s picture

FileSize
41.88 KB

I've successfully used this feature on a website I developed recently and it works very well.

To actually use the export functions you need to ensure that you have the "csv.png" and "doc.png" files in your views_bonus module folder. You can download them from posts #9 and #10 above. Also be sure that the "Bonus:Views Export" box is checked in your site's admin/module page (I think you said you already did this, but it's a good idea to check).

Next, just display any view of the database that you have created for your site with the views module. You should see the two (CSV and DOC) buttons on the display. Clicking either of these will execute the export to the respective format. I've attached a screenshot to show what my site looks like ...

vijoy’s picture

I've just spent 2 days troubleshooting this so I thought I would post my experience with finally getting the CSV export working. Make sure the filenames are simply 'views_bonus_export.info' and 'views_bonus_export.module'... get rid of the the added 'filter_fix' (or any other text in future reiterations) on the end-- it seems to prevent proper operation. Hope this helps.

emilyf’s picture

Thanks much (screenshot appreciated); adding the .png files to the views_bonus folder fixed it, along with making sure I moved the views csv export module/info files into that folder as well. And lastly, rename csv_0.png from above to just csv.png. That finally got it working. Thanks again for the help! Works great.

dmitrig01’s picture

Version: 6.x-1.x-dev » 4.7.x-1.x-dev
Component: User interface » Code
Priority: Critical » Minor

Ok, look guys. I an the one who's going to be committing this patch. I am getting pretty angry. This is not a support queue. I would appreciate through tests of a patch in here, as well as a patch itself. You guys are running my tolerance down.

dmitrig01’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev

Ok... I'll be nice and roll a patch, but I am expecting reviews

dalin’s picture

FileSize
402 bytes

as per Dimitri's request I made a new csv.png to match the styling of the doc.png.

Also note that with recent versions of Views, my original patch will show the view twice, one on top of the other. The fix is to remove the last line of the function views_bonus_export_views_post_view so that it returns nothing.

dalin’s picture

FileSize
358 bytes

whoops, here's the real new csv.png

Also looks like #18 fixes the return $output problem.

dalin’s picture

Here are new versions of the icons in a nicer font, plus an XML icon, plus a GIMP .XCF file so that anyone can tweak.

RobLoach’s picture

Version: 5.x-1.x-dev » 6.x-1.x-dev
Component: Code » Views Export
Status: Needs work » Fixed

I'm sticking this into the CVS. Commit 75802. If you have any other problems with is, make some new issues.

RobLoach’s picture

Version: 6.x-1.x-dev » 5.x-1.x-dev

dmitdrig01 requested it to be in DRUPAL-5.

amitaibu’s picture

@ROB,
Either I miss something, but I don't see the new commit 75802 in the Version 5.x-1.x-dev (or HEAD).
It shows as Last updated: July 3, 2007 - 02:16

RobLoach’s picture

The last message died.... Commit 75803 puts it into DRUPAL-5. Is it working for you now?

amitaibu’s picture

Shouldn't I see this then updated here Under the views_bonus 5.x-1.x-dev?

If I'm doing something wrong, I'm sorry - I don't want to turn it into a support issue; It's only that I think there is a problem here...

RobLoach’s picture

The dev releases are only updated when the CVS script is run. I suggest downloading it via CVS.

Anonymous’s picture

Status: Fixed » Closed (fixed)
jason.fisher’s picture

Not going to re-open this, but using CSV export with 27,000 nodeprofile nodes resulted in "Allowed memory size .. exhausted" errors, even with the memory limit set to 544MB.

My solution is relatively simple and should be considered by anyone with a large amount of data needing export.

In views_bonus_export.module, replace this function with the code below.. (or override it if you prefer)


/**
 *  Main Function to export a view as CSV
 */
function theme_views_bonus_export_csv($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  $fields = _views_get_fields();

  // headings row
  $headings = array();
  foreach ($view->field as $field) {
    if ($fields[$field['id']]['visible'] !== false) {
      $headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
    }
  }

  drupal_set_header('Content-Type: text/x-comma-separated-values');
  drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
  print implode(',', $headings) ."\r\n";

  // one row for each node
  foreach ($nodes as $node) {
    $values = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        // I don't believe this was really necessary.
        // $value = $field;
        $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        // Replace preg_replace with strip_tags.  My output generates at 40KB/s instead of 20KB/s with this change alone.
        $value = strip_tags($value);
        $value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
        $value = str_replace('"', '""', $value); // escape " characters
        $value = decode_entities($value);
        $values[] = '"' . $value . '"';
      }
    }

    // Output immediately instead of using $output .=
    print implode(',', $values) . "\r\n";
    // Flush immediately so the user can start downloading and gets an immediate response.
    @ob_flush(); @flush();
   // This flushes the node_load static $nodes array() cache, fixing the memory limit problem.
   node_load(array(), NULL, TRUE);


  }

  module_invoke_all('exit');
  exit;
}


FYI -- I am posting this to the views_bonus support page, please discuss this patch here: http://drupal.org/node/228506

light-blue’s picture

I am using cck address, and I needed unique columns for State, Zip, etc. instead of one column including all of it. Below is how I did it. It's a horrible hack, but it works. I'm posting it here just in case it helps somebody else...

/**
 *  Main Function to export a view as CSV
 */
function theme_views_bonus_export_csv($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  $fields = _views_get_fields();
  drupal_set_header('Content-Type: text/x-comma-separated-values');
  drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');

  // headings row
  $headings = array();
  foreach ($view->field as $field) {
    if ($fields[$field['id']]['visible'] !== false) {
      //light-blue's change
      if ($field['field']=='field_property_address_street1') {
        $headings[]='Street1';
        $headings[]='Street2';
        $headings[]='Apt';
        $headings[]='City';
        $headings[]='State';
        $headings[]='Zip';
      }
      else
        $headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
    }
  }
  $comma = t(',');
  print implode($comma, $headings) ."\r\n";

  // one row for each node
  foreach ($nodes as $node) {
    $values = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        //light-blue's change
        if ($field['field']=='field_property_address_street1') {
          $values[]=$node->node_data_field_property_address_field_property_address_street1;
          $values[]=$node->node_data_field_property_address_field_property_address_street2;
          $values[]=$node->node_data_field_property_address_field_property_address_apt;
          $values[]=$node->node_data_field_property_address_field_property_address_city;
          $values[]=$node->node_data_field_property_address_field_property_address_state;
          $values[]=$node->node_data_field_property_address_field_property_address_zip;
        }else
          $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        $values[] = '"' . str_replace('"', '""', decode_entities(strip_tags($value))) . '"';
      }
    }
    print implode($comma, $values) ."\r\n";
  }
  module_invoke_all('exit');
  exit;
}
cybershan’s picture

Is it only works in "register_globals" set disabled state?

gregaltuna’s picture

Version: 5.x-1.x-dev » 6.x-1.1

Can/would anyone help me get to this point? I installed the bonus pack, bulk operations... but can't seem to figure out how to get the "csv, et. al" buttons at the bottom of the view. Would someone mind giving me a general step-by-step??

mgifford’s picture

I'm curious how transportable this method will be to D7 as well.

giorgio79’s picture

ceci123’s picture

I need to export the search results as CSV or Doc file.

Do you think I could use this to accomplish this task?

Thanks

Ceci

bmango’s picture

@giorgio79 - I've installed the datatables module, which is working fine, but I don't know where the tabletools plugin folder should go to add the toolbar. Can you tell me in which directory the main TableTools folder should go? I added the "T" to the "Set sDOM Initialization Parameter" in the DataTables settings but the bar is not showing up.

Any help much appreciated.