I am using drupal 6, I have created a table using

$tableHeader = array (     
  array('data' => 'Ebooks','field' => 'etitle'),
  array('data' => 'Volumes','field' => 'Volume' ),        
  array('data' => 'Chapter','field' => 'ctitle'),
  array('data' => 'Total Sale'),

$TableRows[] = array (
  str_replace("&", "&" ,$row->ctitle ),                             
  round($row->totalsale,2).' $',

$output .= theme('table', $tableHeader, $TableRows);// get the pager
$output .= theme('pager', NULL, 5, 0);

How can I export it to MS Excel or CSV?


May the below custom code will helpful for you.

set the header

$filename= 'export.csv';
  drupal_set_header('Content-Type: text/csv');
  drupal_set_header('Content-Disposition: attachment; filename=' . $filename);

you can write a function(just an example below) by sending the row values, filename, and column title


function export_to_excel_page($row_array, $filename,$column_titles) {

  drupal_set_header('Content-Type: text/csv');
  drupal_set_header('Content-Disposition: attachment; filename=' . $filename);

  foreach($column_titles as $key => $value) {
           $keys[] =$value;         

  if($keys) print implode(',', $keys) ."\r\n";

    //fetching the field values
  foreach($row_array as $key => $value) {

   $values[] =$value; 

    print implode(',', $values) ."\r\n";


Here's an updated function that should work for D7.

 * Exports a CSV.
 * @param array $variables
 *   An associative array of data containing "header" and "rows" keys.
 *   This is ready to be passed to theme_table(). See api.drupal.org/node/22950.
 *   Also contains the key "filename" specifying the filename.
function _epc_standards_statistics_export_csv($variables) {
  drupal_add_http_header('Content-Type', 'text/csv; utf-8');
  drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $variables['filename']);

  $output = '';
  $keys = array();
  foreach ($variables['header'] as $key => $value) {
    $keys[] = $value['data'];
  if ($keys) {
    $output .= implode("\t", $keys) . "\n";
  foreach ($variables['rows'] as $value) {
    $output .= implode("\t", $value) . "\n";
  print $output;

If you are trying to export Nodes content, then you can able to use views_data_export module and export content using views !

This module is designed to provide a way to export large amounts of data from views. It provides a display plugin that can rendered progressively in a batch. Style plugins are included that support exporting in the following types:

CSV, Microsoft XLS, Microsoft DOC, Basic, TXT, XML.

If you want to export database tables data then you can consider data module.

Data module helps you model, manage and query related sets of tables. It offers an administration interface and a low level API for manipulating tables and accessing their contents. Data module provides Views integration for displaying table data and Drupal search integration for searching table content.

  • Dear Anil Sagar, thanks for suggestion, but i need to export data from custom tables. as i generated table from that data i want to export those tables, and i am not using views. – Sanjay dev Oct 17 '12 at 5:30
  • You can use phpmyadmin that works with mysql to export tables or query results to CSV format easily ! Try data module too as it tells it can export tables date to CSV! – Anil Sagar Oct 17 '12 at 5:36
  • I have to provide a UI to user that shows data and if they need then they can export data in csv or excel. i have completed the UI but i have to give export functionality to users to export the table content. – Sanjay dev Oct 17 '12 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.