Project

General

Profile

Paste
Download (1.74 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / includes / exporters / webform_exporter_excel_delimited.inc @ 7b2d1845

1
<?php
2

    
3
/**
4
 * The Excel exporter currently is just a tab-delimited export.
5
 */
6
class webform_exporter_excel_delimited extends webform_exporter_delimited {
7

    
8
  /**
9
   * {@inheritdoc}
10
   */
11
  public function __construct($options) {
12
    $options['delimiter'] = '\t';
13
    parent::__construct($options);
14
  }
15

    
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public function bof(&$file_handle) {
20
    $output = '';
21

    
22
    // Include at BOM at the beginning of the file for Little Endian.
23
    // This makes tab-separated imports work correctly in MS Excel.
24
    if (function_exists('mb_convert_encoding')) {
25
      $output = chr(255) . chr(254);
26
    }
27
    @fwrite($file_handle, $output);
28
  }
29

    
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function add_row(&$file_handle, array $data, $row_count) {
34
    foreach ($data as $key => $value) {
35
      // Escape inner quotes and wrap all contents in new quotes.
36
      $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';
37

    
38
      // Remove <script> tags, which mysteriously cause Excel not to import.
39
      $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
40
    }
41
    $row = implode($this->delimiter, $data) . "\n";
42

    
43
    if (function_exists('mb_convert_encoding')) {
44
      $row = mb_convert_encoding($row, 'UTF-16LE', 'UTF-8');
45
    }
46

    
47
    @fwrite($file_handle, $row);
48
  }
49

    
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function set_headers($filename) {
54
    drupal_add_http_header('Content-Type', 'application/x-msexcel');
55
    drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $this->get_filename($filename));
56
    drupal_add_http_header('Pragma', 'public');
57
    drupal_add_http_header('Cache-Control', 'max-age=0');
58
  }
59

    
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function get_filename($filename) {
64
    return $filename . '.xls';
65
  }
66

    
67
}