2
require($EXCEL_WRITER_PATH . "Spreadsheet/Excel/Writer.php");
4
class EXCELHandler extends DATAHandler {
7
var $xls_time_subsec_format;
14
var $time_subsec_format;
24
const MAX_TITLE_CHARS = 31;
26
function __construct(&$opts = NULL, STREAMHandler $h = NULL) {
27
global $CSV_DATE_FORMAT;
28
global $EXCEL_DATE_FORMAT;
29
global $EXCEL_SUBSEC_FORMAT;
31
$this->content_type = "application/vnd.ms-excel";
32
$this->extension = "xls";
34
parent::__construct($opts, $h);
36
$this->multigroup = true;
37
$this->filewriter = true;
40
$this->time_format = $opts['date_format'];
41
$this->time_width = $opts['date_width'];
42
$this->time_subsec_format = $opts['subsec_format'];
43
$this->time_subsec_width = $opts['subsec_width'];
44
$this->data_format = $opts['value_format'];
45
$this->data_width = $opts['value_width'];
48
if (!$this->time_format) $this->time_format = $EXCEL_DATE_FORMAT;
49
if (!$this->time_width) $this->time_width = 20;
50
if (!$this->time_subsec_format) $this->time_subsec_format = $EXCEL_SUBSEC_FORMAT;
51
if (!$this->time_subsec_width) $this->time_width = 26;
52
if (!$this->data_format) $this->data_format = "0.0000E+##";
53
if (!$this->data_width) $this->data_width = 12;
55
if (preg_match('/^\s*text\s*(\((.*)\))?\s*$/i', $this->time_subsec_format, $m)) {
56
$this->time_subsec_format = false;
57
if ($m[2]) $this->string_format = $m[2];
58
else $this->string_format = $CSV_DATE_FORMAT;
62
function SequenceStart($flags = 0) {
63
parent::SequenceStart($flags);
65
if ((!$this->h)||($this->h->stdout)) {
66
$this->tmpfile = false;
67
$this->xls = new Spreadsheet_Excel_Writer();
69
$this->tmpfile = GetTmpFile("adei_stream_excel_", "xls");
70
$this->xls = new Spreadsheet_Excel_Writer($this->tmpfile);
73
$this->xls_time_format = $this->xls->addFormat();
74
$this->xls_time_format->setNumFormat($this->time_format);
75
if ($this->time_subsec_format) {
76
$this->xls_time_subsec_format = $this->xls->addFormat();
77
$this->xls_time_subsec_format->setNumFormat($this->time_subsec_format);
79
$this->xls_data_format = $this->xls->addFormat();
80
$this->xls_data_format->setNumFormat($this->data_format);
83
function GroupStart($title, $subseconds = false, $flags = 0) {
84
$val = substr(preg_replace('/[^\w\d_]+/', '_', $title), 0, EXCELHandler::MAX_TITLE_CHARS);
85
$this->sheet = $this->xls->addWorksheet($val);
86
if ($this->sheet instanceof PEAR_Error) {
87
throw new ADEIException(translate("Error encountered while creating Excel sheet (%s). Info: %s", $title, $this->sheet->toString()));
90
$this->subseconds = $subseconds;
93
parent::GroupStart($title, $subseconds, $flags);
96
function SequenceEnd($flags = 0) {
101
$this->h->WriteData($this->tmpfile);
103
$this->h->WriteFile($this->tmpfile);
104
unlink($this->tmpfile);
107
parent::SequenceEnd($flags);
111
function TimeHeader($flags = 0) {
112
$this->sheet->write(0,0,"Date/Time");
113
$this->sheet->setColumn(0,0,$this->time_width);
116
function DataHeader(&$header, $i, $flags = 0) {
117
$this->sheet->write(0, $i + 1, $header);
118
$this->sheet->setColumn(0, $i + 1, $this->data_width);
121
function TimeValue(&$time, $flags = 0) {
122
if ($this->subseconds) {
123
if ($this->time_subsec_format) {
124
$val = dsMathPreciseAdd($time, 2209161600) / 86400; /* 86400 * 25569 */
125
$this->sheet->write(++$this->row, 0, $val, $this->xls_time_subsec_format);
127
$subsec = strchr(sprintf("%.6F", $time), '.');
128
$val = date($this->string_format, $time) . $subsec;
129
$this->sheet->write(++$this->row, 0, $val);
132
$val = ($time + 2209161600) / 86400; /* 86400 * 25569 */
133
$this->sheet->write(++$this->row, 0, $val, $this->xls_time_format);
137
function DataValue(&$value, $i, $flags = 0) {
138
$this->sheet->write($this->row, $i + 1, $value, $this->xls_data_format);
b'\\ No newline at end of file'