Column headers are not quoted, so if you have a header like "número" the columns get screwed.

Another problem is the fact that "ú" appears as "ú"

Comments

enboig’s picture

I have solved the the problem, and I have also added an option to choose the output as UTF8 or ANSI


<?php
// $Id: views-bonus-export-csv.tpl.php,v 1.5 2009/05/09 18:44:31 neclimdul Exp $
/**
 * @file views-view-table.tpl.php
 * Template to display a view as a table.
 *
 * - $title : The title of this group of rows.  May be empty.
 * - $rows: An array of row items. Each row is an array of content
 *   keyed by field ID.
 * - $header: an array of haeaders(labels) for fields.
 * - $themed_rows: a array of rows with themed fields.
 * - $items:
 * - $seperator: The seperator used to seperate fields. generally comma's but
 *   sometimes people use other values in CSVs.
 * @ingroup views_templates
 */

// Print out header row, if option was selected.
if ($options['header']) {
  if ($options['quote']) {
    switch ($options['encoding']) {
      case 'utf8':
        print utf8_decode('"'.implode('"'.$seperator.'"', $header) . "\"\r\n");
      break;
      default:
        print '"'.implode('"'.$seperator.'"', $header) . "\"\r\n";
      break;

    }
  }
  else {
    switch ($options['encoding']) {
      case 'utf8':
        print utf8_decode(implode($seperator, $header) . "\r\n");
      break;
      default:
        print implode($seperator, $header) . "\r\n";
      break;
    }
  }
}

// Print out exported items.
foreach ($themed_rows as $count => $item_row):
  switch ($options['encoding']) {
    case 'utf8':
      print implode($seperator, $item_row) . "\r\n";
    break;
    default:
      print utf8_decode(implode($seperator, $item_row) . "\r\n");
    break;
  }
endforeach;

<?php
// $Id: views_bonus_plugin_style_export_csv.inc,v 1.7 2009/05/09 20:52:10 neclimdul Exp $
/**
 * @file
 * Plugin include file for export style plugin.
 */

/**
 * Generalized style plugin for export plugins.
 *
 * @ingroup views_style_plugins
 */
class views_bonus_plugin_style_export_csv extends views_bonus_plugin_style_export {
  /**
   * Initialize plugin.
   *
   * Set feed image for shared rendering later.
   */
  function init(&$view, &$display, $options = NULL) {
    parent::init($view, $display, $options = NULL);
    $this->feed_image = drupal_get_path('module', 'views_bonus_export') . '/images/csv.png';
  }

  /**
   * Set options fields and default values.
   *
   * @return
   * An array of options information.
   */
  function option_definition() {
    $options = parent::option_definition();

    $options['filename'] = array(
      'default' => 'view-%view.csv',
      'translatable' => FALSE,
    );
    $options['quote'] = array(
      'default' => TRUE,
      'translatable' => TRUE,
    );
    $options['seperator'] = array(
      'default' => ',',
      'translatable' => TRUE,
    );
    $options['header'] = array(
      'default' => TRUE,
      'translatable' => FALSE,
    );

    return $options;
  }

  /**
   * Options form mini callback.
   *
   * @param $form
   * Form array to add additional fields to.
   * @param $form_state
   * State of the form.
   * @return
   * None.
   */
  function options_form(&$form, &$form_state) {
    $form['filename'] = array(
      '#type' => 'textfield',
      '#title' => t('CSV filename'),
      '#default_value' => $this->options['filename'],
      '#description' => t('The filename that will be suggested to the browser for downloading purposes. %view will be replaced with the view name.'),
      '#process' => array('views_process_dependency'),
      '#dependency' => array('edit-style-options-override' => array(FALSE)),
    );
    $form['seperator'] = array(
      '#type' => 'textfield',
      '#title' => t('Seperator'),
      '#default_value' => !empty($this->options['seperator']) ? $this->options['seperator'] : ',',
      '#description' => t('This is the seperator that is used to seperate fields. CSV implies comma seperated fields so this should not be changed unless you have specific requirements'),
    );
    $form['quote'] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['quote']),
      '#title' => t('Quote values. Useful for output that might contain your seperator as part of one of the values.'),
    );
    $form['header'] = array(
      '#type' => 'checkbox',
      '#title' => t('Make first row a list of column headers.'),
      '#default_value' => !empty($this->options['header']),
    );
    $form['encoding'] = array(
      '#type' => 'radios',
      '#default_value' => (empty($this->options['encoding']) ? 'utf8' : $this->options['encoding']),
      '#title' => t('Codification of the output file'),
      '#options' => array (
        'utf8' => 'UTF-8',
        'ansi' => 'ANSI',
      ),
    );
  }
}


neclimdul’s picture

Status: Active » Closed (duplicate)

Quoting is a duplicate of #512630: "Quote values" does not quote header cells. The other thing would be a separate feature request.