4 * class to generate excel file from rows of data, and a configuration.
7 * $x = new Pman_Core_SimpleExcel(array())
13 * name : [ Align : left, .... ]
21 [ "a", ["test", "left"] ] << sub array [text, formatname]
25 'header'=> "Thumbnail",
27 * 'dataFormat' => 'string' // to force a string..
29 'renderer' => array($this, 'getThumb'),
30 * 'color' => 'yellow', // set color for the cell which is a header element
31 * 'fillBlank' => 'gray', // set the color for the cell which is a blank area
34 // if this is set then it will add a tab foreach one.
44 require_once 'Pman.php';
47 class Pman_Core_SimpleExcel extends Pman
50 var $worksheet_cfg = array();
52 var $formats = array();
53 var $workbook = false;
54 var $worksheet= false;
56 function Pman_Core_SimpleExcel($data,$cfg)
58 // print_r($cfg);exit;
59 require_once 'Spreadsheet/Excel/Writer.php';
60 // Creating a workbook
61 $outfile2 = $this->tempName('xls');
62 // var_dump($outfile2);
63 $workbook = new Spreadsheet_Excel_Writer($outfile2);
64 //$workbook = new Spreadsheet_Excel_Writer();
65 $workbook->setVersion(8);
66 // sending HTTP headers
67 $this->workbook = $workbook;
71 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
73 foreach($cfg['formats'] as $f=>$fcfg) {
75 $formats[$f] = & $workbook->addFormat();
76 foreach((array)$fcfg as $k=>$v) {
77 $formats[$f]->{'set' . $k}($v);
83 if (!empty($cfg['workbook'])) {
84 $this->buildPage( $formats , $data,$cfg);
85 } elseif (!empty($cfg['workbooks'])) {
86 foreach($cfg['workbooks'] as $i =>$wcfg) {
87 $this->buildPage( $formats , $data[$i],$wcfg);
91 // if workbooks == false - > the user can call buildpage..
94 if (!empty($cfg['leave_open'])) {
95 $this->outfile2 = $outfile2;
100 $this->outfile2 = $outfile2;
107 static function date($str)
110 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
115 function buildPage( $formats , $data, $cfg)
117 $workbook = $this->workbook;
118 //echo '<PRE>'; print_R($cfg);
119 // print_r($cfg);exit;
120 // Creating a worksheet
121 //print_R($cfg);exit;
122 // copy the config and alias so that book can be written to..
123 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
126 //$this->formats = (array)$formats;
128 if (isset($cfg['formats']) && empty($formats)) {
130 foreach($cfg['formats'] as $f=>$fcfg) {
132 $formats[$f] = & $workbook->addFormat();
133 foreach((array)$fcfg as $k=>$v) {
134 $formats[$f]->{'set' . $k}($v);
139 $this->formats = $formats;
144 //var_dump($cfg['workbook']);
146 $worksheet = $workbook->addWorksheet($cfg['workbook']);
147 if (is_a($worksheet, 'PEAR_Error')) {
148 die($worksheet->toString());
150 //print_R($worksheet);
151 $worksheet->setInputEncoding('UTF-8');
153 $this->worksheet = $worksheet;
157 if (!empty($cfg['head'])) {
158 foreach($cfg['head'] as $row) {
159 foreach($row as $c => $col) {
160 if (is_array($col)) {
161 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
162 $worksheet->write($start_row, $c, $col[0], $format);
166 $worksheet->write($start_row, $c, $col);
172 if(!isset($cfg['nonspacer'])){ $start_row++; }
176 foreach($cfg['cols'] as $c=>$col_cfg) {
177 if (is_string($col_cfg)) {
178 $cfg['cols'][$c] = array(
179 'header' => $col_cfg,
180 'dataIndex' => $col_cfg,
187 foreach($cfg['cols'] as $c=>$col_cfg) {
188 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
189 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
190 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
193 $this->start_row = &$start_row;
203 foreach($data as $r=>$clo) {
204 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
205 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
208 /// call user render on any that are defined..
210 foreach($data as $r=>$cl) {
212 foreach($cfg['cols'] as $c=>$col_cfg) {
213 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
214 if (empty($cl[$col_cfg['dataIndex']])) {
217 if (isset($col_cfg['renderer'])) {
218 // not sure if row is correct here...!!!?
219 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
222 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
227 $start_row += count($data);
229 if (!empty($cfg['foot'])) {
230 foreach($cfg['foot'] as $row) {
231 foreach($row as $c => $col) {
232 // if it's an array? - formated ???
233 if (is_array($col)) {
234 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
235 $worksheet->write($start_row, $c, $col[0], $format);
238 $worksheet->write($start_row, $c, $col);
251 function addLine($worksheet_name, $clo)
253 $cfg = $this->worksheet_cfg[$worksheet_name];
254 $start_row = $this->start_row;
255 $formats = (array)$this->formats;
256 $worksheet = $this->worksheet;
262 if (is_object($clo)) {
263 $cl = (array)$clo; // lossless converstion..
266 if (isset($cfg['row_height'])) {
267 $worksheet->setRow($start_row +$r, $cfg['row_height']);
270 foreach($cfg['cols'] as $c=>$col_cfg) {
272 if(isset($cl[$col_cfg['dataIndex']])){
273 $v = $cl[$col_cfg['dataIndex']];
275 if(isset($col_cfg['fillBlank'])){
276 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
281 if (!isset($cl[$col_cfg['dataIndex']])) {
284 if (isset($col_cfg['txtrenderer'])) {
285 $v = call_user_func($col_cfg['txtrenderer'],
286 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
292 if (isset($col_cfg['renderer'])) {
297 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
299 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
303 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
304 // print_R(array($start_row+$r, $c, $v, $format));exit;
305 // handle 0 prefixes..
306 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
308 $dataFormat == 'string' ) {
309 $worksheet->writeString($start_row+$r, $c, $v, $format);
312 $worksheet->write($start_row+$r, $c, $v, $format);
321 function send($fname)
323 if (!empty($this->workbook)) {
324 $this->workbook->close();
325 $this->workbook = false;
328 require_once 'File/Convert.php';
329 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
330 $fn = $fc->convert("application/vnd.ms-excel");
331 $fc->serve('attachment',$fname); // can fix IE Mess