/
ExcelWriter.php
67 lines (62 loc) · 2.34 KB
/
ExcelWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Exports results in Microsoft Excel format. By default the Writer sends
* HTTP headers and the file contents via HTTP. For testing purposes a
* file name can be to the constructor which will cause the ExcelWriter to
* output to a file.
*/
class ExcelWriter extends Writer
{
private $workbook;
private $currentSheet;
private $separator;
private $hasOutputHeader;
private $rowCounter;
private $forceDownload = true;
/**
* The presence of a filename will cause the writer to output to
* a file rather than send.
*
* @param string $filename
* @return ExcelWriter
*/
public function __construct()
{
require_once(APPPATH.'/third_party/xlsx_writer/xlsxwriter.class.php');
$this->separator = '~|';
$this->hasOutputHeader = false;
$this->rowCounter = 0;
}
public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions)
{
parent::init($survey, $sLanguageCode, $oOptions);
$this->workbook = new XLSXWriter();
$this->workbook->setTempDir(Yii::app()->getConfig('tempdir'));
$worksheetName = $survey->languageSettings['surveyls_title'];
$worksheetName = mb_substr(str_replace(array('*', ':', '/', '\\', '?', '[', ']'), array(' '), $worksheetName), 0, 31, 'utf-8'); // Remove invalid characters
if ($worksheetName == '') {
$worksheetName = 'survey_'.$survey->id;
}
$this->currentSheet = $worksheetName;
$this->forceDownload = !($oOptions->output == 'file');
}
protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
if (!$this->hasOutputHeader) {
$this->workbook->writeSheetRow($this->currentSheet, $headers);
$this->hasOutputHeader = true;
}
$this->workbook->writeSheetRow($this->currentSheet, $values);
}
public function close()
{
$this->workbook->writeToFile($this->filename);
if ($this->forceDownload) {
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"{$this->webfilename}.xlsx\"");
header('Content-Length: '.filesize($this->filename));
readfile($this->filename);
}
return $this->workbook;
}
}