Framework  3.9
excel.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
45 {
46  static $handler = ExcelFile;
47 
48  static function create($filename)
49  {
50  // Discard any previously generated output
51  ob_clean();
52  trace("Creating new Excel Handler: ".ExcelFileWriter::$handler, 3);
53  return new ExcelFileWriter::$handler($filename);
54  }
55 
56  static function RegisterHandler($handlerClass)
57  {
58  ExcelFileWriter::$handler = $handlerClass;
59  }
60 }
61 
67 interface IExcelFile
68 {
75  function writeNumber($row, $col, $value);
76 
84  function writeText($row, $col, $value, $wrap = false);
85 
93  function writePercentage($row, $col, $value);
94 
102  function writeCurrency($row, $col, $value);
103 
110  function writeHeading($row, $col, $value);
111 
118  function writeSubheading($row, $col, $value);
119 
126  function writeFooter($row, $col, $value);
127 
134  function writeFooterNumber($row, $col, $value);
135 
140  function setWorksheetTitle($title);
141 
146  function addWorksheet($title);
147 
152  function abort($error);
153 
157  function close();
158 
162  function send();
163 }
164 
173 class ExcelFile implements IExcelFile
174 {
176  var $open;
177  var $content;
178 
184  {
185  $this->filename = $filename;
186  ob_start();
187  $this->open = true;
188  $this->_xlsBOF();
189  }
190 
191 
192  function _xlsBOF()
193  {
194  echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
195  }
196 
197  function _xlsEOF()
198  {
199  echo pack("ss", 0x0A, 0x00);
200  }
201 
208  function writeNumber($row, $col, $value)
209  {
210  echo pack("sssss", 0x203, 14, $row, $col, 0x0);
211  echo pack("d", $value);
212  }
213 
221  function writeText($row, $col, $value, $wrap = false)
222  {
223  $len = strlen($value);
224  echo pack("ssssss", 0x204, 8 + $len, $row, $col, 0x0, $len);
225  echo $value;
226  }
227 
228  function writeHeading($row, $col, $value)
229  {
230  return $this->writeText($row, $col, $value);
231  }
232 
233  function writeSubheading($row, $col, $value)
234  {
235  return $this->writeText($row, $col, $value);
236  }
237 
238  function writeFooter($row, $col, $value)
239  {
240  return $this->writeText($row, $col, $value);
241  }
242 
243  function writeFooterNumber($row, $col, $value)
244  {
245  return $this->writeNumber($row, $col, $value);
246  }
247 
248  function writePercentage($row, $col, $value)
249  {
250  $this->writeText($row, $col, $value);
251  }
252 
253  function writeCurrency($row, $col, $value)
254  {
255  $this->writeNumber($row, $col, $value);
256  }
257 
258  function setWorksheetTitle($title)
259  {
260  throw new Exception("Multiple worksheets are not supported with the built-in Excel writer. Install PHPExcel.");
261  }
262 
263  function addWorksheet($title)
264  {
265  throw new Exception("Multiple worksheets are not supported with the built-in Excel writer. Install PHPExcel.");
266  }
267 
268  function abort($error)
269  {
270  ob_end_clean();
271  ajaxReturn($error);
272  }
273 
274  function close()
275  {
276  $this->_xlsEOF();
277  $this->content = ob_get_contents();
278  ob_end_clean();
279  $this->open = false;
280  }
281 
282  function send()
283  {
284  if ($this->open)
285  {
286  $this->close();
287  }
288 
289  session_cache_limiter("private_no_expire, must-revalidate");
290  header("Content-Type: application/vnd.msexcel");
291  header("Content-Disposition: attachment;filename=$this->filename");
292  header("Pragma: private");
293  header("Content-Transfer-Encoding: binary");
294  header("Content-Length: ".strlen($this->content));
295 
296  echo $this->content;
297  }
298 }
299 
Generate a binary format Microsoft Excel file for download.
Definition: excel.inc:174
abort($error)
Abort processing, output the specified error string.
Definition: excel.inc:268
_xlsBOF()
Definition: excel.inc:192
_xlsEOF()
Definition: excel.inc:197
writeNumber($row, $col, $value)
Write a number to the cell at the specified row and column.
Definition: excel.inc:208
send()
Transmit the spreadsheet to the client.
Definition: excel.inc:282
writeCurrency($row, $col, $value)
Write a number to the cell at the specified row and column with currency formatting.
Definition: excel.inc:253
close()
Prevent further output to the spreadsheet.
Definition: excel.inc:274
writeFooterNumber($row, $col, $value)
Write number with column footer styles.
Definition: excel.inc:243
writeHeading($row, $col, $value)
Write text with column heading styles.
Definition: excel.inc:228
setWorksheetTitle($title)
Set the title of the currently active worksheet.
Definition: excel.inc:258
ExcelFile($filename)
Create a new ExcelFile object.
Definition: excel.inc:183
writeFooter($row, $col, $value)
Write text with column footer styles.
Definition: excel.inc:238
writeText($row, $col, $value, $wrap=false)
Write text to the cell at the specified row and column.
Definition: excel.inc:221
addWorksheet($title)
Adds a new worksheet to the Excel file.
Definition: excel.inc:263
writePercentage($row, $col, $value)
Write a number to the cell at the specified row and column and format as a percentage.
Definition: excel.inc:248
writeSubheading($row, $col, $value)
Write text with column sub-heading styles.
Definition: excel.inc:233
ExcelFileWriter is an abstract factory to allow different Excel libraries to be used at the programme...
Definition: excel.inc:45
static $handler
Definition: excel.inc:46
static RegisterHandler($handlerClass)
Definition: excel.inc:56
static create($filename)
Definition: excel.inc:48
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
ajaxReturn($msg=null)
Returns a string output and exits the script cleanly.
Definition: functions.inc:1783
Minimum base interface that must be provided by an ExcelFile implementation.
Definition: excel.inc:68
writeFooter($row, $col, $value)
Write text with column footer styles.
send()
Transmit the spreadsheet to the client.
addWorksheet($title)
Adds a new worksheet to the Excel file.
writeText($row, $col, $value, $wrap=false)
Write text to the cell at the specified row and column.
writeHeading($row, $col, $value)
Write text with column heading styles.
close()
Prevent further output to the spreadsheet.
setWorksheetTitle($title)
Set the title of the currently active worksheet.
writeSubheading($row, $col, $value)
Write text with column sub-heading styles.
abort($error)
Abort processing, output the specified error string.
writePercentage($row, $col, $value)
Write a number to the cell at the specified row and column and format as a percentage.
writeNumber($row, $col, $value)
Write a number to the cell at the specified row and column.
writeFooterNumber($row, $col, $value)
Write number with column footer styles.
writeCurrency($row, $col, $value)
Write a number to the cell at the specified row and column with currency formatting.