I would like to export certain set of data downloadable for users as CVS file.
I can make the data visible as table as normal content.
Then I add the headers. (http://www.plus2net.com/php_tutorial/php_file_download.php)
What happens is that all menus etc. are part of the file.

How can I get rid of the "normal" drupal content - menus etc. and be left with the actual data?

This issue has been asked in many forus, but I found no answer:

There is also views_bonus module, but the export module seems to be missing from that one.
What I read the ability to export CVS would be enabled through that.
http://drupal.org/project/views_bonus

In my situation the "raw" php-solution would be the most efficient.

Perhaps another way to put the same question is that how one can call a funtion within same node and get a function executed?
That function would then display headers + data.
That would solve the issue.

Comments

gpk’s picture

I do this in a module the following way:

I have a function that handles both the display of the tabular data in a page and the download of the same data. I construct the array $data which contains the tabular data, with the column headings in $header. Then I use $results = theme_table($header, $rows); to convert to HTML. I just return $results to have the table displayed in a page. For download, I use

  // Download the results as XLS (actually HTML format), return nothing to browser
  drupal_set_header('Content-type: application/vnd.ms-excel');
  drupal_set_header('Content-Disposition: attachment; filename="filename.xls"');
  $output = '<html>';
  $output .= '<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"></head>';
  $output .= '<body>';
  $output .= $results;
  $output .= "</body></html>";

  print $output;
  exit;

I found this works better than CSV if you have fields that can be interpreted as numbers but you don't want to be - e.g. numeric strings that begin with a 0. You have to add a space to the beginning of these - this prevents Excel interpreting the string as a number and stripping the leading 0.

If you want true CSV output then you shoud be able to modify the above accordingly. It should also work embedded within a page using PHP input format - you could use a query parameter ?download=1 or somesuch to distinguish the 2 cases.

gpk
----
www.alexoria.co.uk

lenkkivihko’s picture

Many thanks!
---------------------------------------------------------
Harjoituspäiväkirja - www.lenkkivihko.fi