public
Last active — forked from ihumanable/Excel.php

Simple Excel Writer in PHP

  • Download Gist
Excel.php
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php
 
/**
* Simple excel writer class with no external dependencies, drop it in and have fun
* @author Matt Nowack
* @license Unlicensed
* @version 1.0
*/
class Excel {
private $col;
private $row;
private $data;
private $title;
/**
* Safely encode a string for use as a filename
* @param string $title The title to use for the file
* @return string The file safe title
*/
static function filename($title) {
$result = strtolower(trim($title));
$result = str_replace("'", '', $result);
$result = preg_replace('#[^a-z0-9_]+#', '-', $result);
$result = preg_replace('#\-{2,}#', '-', $result);
return preg_replace('#(^\-+|\-+$)#D', '', $result);
}
/**
* Builds a new Excel Spreadsheet object
* @return Excel The Spreadsheet
*/
function __construct($title) {
$this->title = $title;
$this->col = 0;
$this->row = 0;
$this->data = '';
$this->bofMarker();
}
/**
* Transmits the proper headers to cause a download to occur and to identify the file properly
* @return nothing
*/
function headers() {
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=" . Excel::filename($this->title) . ".xls ");
header("Content-Transfer-Encoding: binary ");
}
function send() {
$this->eofMarker();
$this->headers();
echo $this->data;
}
/**
* Writes the Excel Beginning of File marker
* @see pack()
* @return nothing
*/
private function bofMarker() {
$this->data .= pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
/**
* Writes the Excel End of File marker
* @see pack()
* @return nothing
*/
private function eofMarker() {
$this->data .= pack("ss", 0x0A, 0x00);
}
/**
* Moves internal cursor left by the amount specified
* @param optional integer $amount The amount to move left by, defaults to 1
* @return integer The current column after the move
*/
function left($amount = 1) {
$this->col -= $amount;
if($this->col < 0) {
$this->col = 0;
}
return $this->col;
}
/**
* Moves internal cursor right by the amount specified
* @param optional integer $amount The amount to move right by, defaults to 1
* @return integer The current column after the move
*/
function right($amount = 1) {
$this->col += $amount;
return $this->col;
}
/**
* Moves internal cursor up by amount
* @param optional integer $amount The amount to move up by, defaults to 1
* @return integer The current row after the move
*/
function up($amount = 1) {
$this->row -= $amount;
if($this->row < 0) {
$this->row = 0;
}
return $this->row;
}
/**
* Moves internal cursor down by amount
* @param optional integer $amount The amount to move down by, defaults to 1
* @return integer The current row after the move
*/
function down($amount = 1) {
$this->row += $amount;
return $this->row;
}
/**
* Moves internal cursor to the top of the page, row = 0
* @return nothing
*/
function top() {
$this->row = 0;
}
/**
* Moves internal cursor all the way left, col = 0
* @return nothing
*/
function home() {
$this->col = 0;
}
/**
* Writes a number to the Excel Spreadsheet
* @see pack()
* @param integer $value The value to write out
* @return nothing
*/
function number($value) {
$this->data .= pack("sssss", 0x203, 14, $this->row, $this->col, 0x0);
$this->data .= pack("d", $value);
}
/**
* Writes a string (or label) to the Excel Spreadsheet
* @see pack()
* @param string $value The value to write out
* @return nothing
*/
function label($value) {
$length = strlen($value);
$this->data .= pack("ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length);
$this->data .= $value;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.