/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/handlers/excel.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
require($EXCEL_WRITER_PATH . "Spreadsheet/Excel/Writer.php");
 
3
 
 
4
class EXCELHandler extends DATAHandler {
 
5
 var $xls;
 
6
 var $xls_time_format;
 
7
 var $xls_time_subsec_format;
 
8
 var $xls_data_format;
 
9
 var $sheet;
 
10
 
 
11
 var $row;
 
12
 
 
13
 var $time_format;
 
14
 var $time_subsec_format;
 
15
 var $data_format;
 
16
 var $time_width;
 
17
 var $data_width;
 
18
 
 
19
 var $string_format;
 
20
 var $tmpfile = false;
 
21
 
 
22
 var $subseconds;
 
23
  
 
24
 const MAX_TITLE_CHARS = 31;
 
25
 
 
26
 function __construct(&$opts = NULL, STREAMHandler $h  = NULL) {
 
27
    global $CSV_DATE_FORMAT;
 
28
    global $EXCEL_DATE_FORMAT;
 
29
    global $EXCEL_SUBSEC_FORMAT;
 
30
    
 
31
    $this->content_type = "application/vnd.ms-excel";
 
32
    $this->extension = "xls"; 
 
33
 
 
34
    parent::__construct($opts, $h);
 
35
    
 
36
    $this->multigroup = true;
 
37
    $this->filewriter = true;
 
38
 
 
39
    if ($opts) {
 
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'];
 
46
    }
 
47
 
 
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;
 
54
    
 
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;
 
59
    }
 
60
 }
 
61
 
 
62
 function SequenceStart($flags = 0) {
 
63
    parent::SequenceStart($flags);
 
64
    
 
65
    if ((!$this->h)||($this->h->stdout)) {
 
66
        $this->tmpfile = false;
 
67
        $this->xls = new Spreadsheet_Excel_Writer();
 
68
    } else {
 
69
        $this->tmpfile = GetTmpFile("adei_stream_excel_", "xls");
 
70
        $this->xls = new Spreadsheet_Excel_Writer($this->tmpfile);
 
71
    }
 
72
 
 
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);
 
78
    }
 
79
    $this->xls_data_format = $this->xls->addFormat();
 
80
    $this->xls_data_format->setNumFormat($this->data_format);
 
81
 }
 
82
 
 
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()));
 
88
    }
 
89
 
 
90
    $this->subseconds = $subseconds;
 
91
    $this->row = 0;
 
92
    
 
93
    parent::GroupStart($title, $subseconds, $flags);
 
94
 }
 
95
 
 
96
 function SequenceEnd($flags = 0) {
 
97
    $this->xls->close();
 
98
    
 
99
    if ($this->tmpfile) {
 
100
        if ($this->filemode)
 
101
            $this->h->WriteData($this->tmpfile);
 
102
        else
 
103
            $this->h->WriteFile($this->tmpfile);
 
104
        unlink($this->tmpfile);
 
105
    }
 
106
    
 
107
    parent::SequenceEnd($flags);
 
108
    
 
109
 }
 
110
 
 
111
 function TimeHeader($flags = 0) {
 
112
    $this->sheet->write(0,0,"Date/Time");
 
113
    $this->sheet->setColumn(0,0,$this->time_width);
 
114
 }
 
115
 
 
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);
 
119
 }
 
120
 
 
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);
 
126
        } else {
 
127
            $subsec = strchr(sprintf("%.6F", $time), '.');
 
128
            $val = date($this->string_format, $time) . $subsec;
 
129
            $this->sheet->write(++$this->row, 0, $val);
 
130
        }
 
131
    } else {
 
132
        $val = ($time + 2209161600) / 86400; /* 86400 * 25569 */
 
133
        $this->sheet->write(++$this->row, 0, $val, $this->xls_time_format);
 
134
    }
 
135
 }
 
136
 
 
137
 function DataValue(&$value, $i, $flags = 0) {
 
138
    $this->sheet->write($this->row, $i + 1, $value, $this->xls_data_format);
 
139
 }
 
140
 
 
141
}
 
142
 
 
143
?>
 
 
b'\\ No newline at end of file'