X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=SimpleExcel.php;h=6c36750ea2e301acfaab413f0d3bc9497c7b89a6;hp=bbfe41b8b725e515a0adc5fff48c6ed0ff256395;hb=HEAD;hpb=bc09a9a3ceb9108175cdddc87fad2917e58582e9 diff --git a/SimpleExcel.php b/SimpleExcel.php index bbfe41b8..0ca12fc6 100644 --- a/SimpleExcel.php +++ b/SimpleExcel.php @@ -47,7 +47,7 @@ new Pman_Core_SimpleExcel($data_array, array( workbook => '....' // ??? ), 'leave_open' => false, // if you call addrows?? later.. - + 'nonspacer' => false, // should add line between head and header row. )); callbacks: renderer @@ -74,8 +74,10 @@ class Pman_Core_SimpleExcel extends Pman var $formats = array(); var $workbook = false; var $worksheet= false; - - function Pman_Core_SimpleExcel($data,$cfg) + var $postRender = array(); + var $outfile2; + + function __construct($data,$cfg) { // print_r($cfg);exit; require_once 'Spreadsheet/Excel/Writer.php'; @@ -92,11 +94,14 @@ class Pman_Core_SimpleExcel extends Pman $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array(); + $this->formats['_default_date_format_'] = $workbook->addFormat();; + $this->formats['_default_date_format_']->setNumFormat('YYYY-MM-DD'); + foreach($cfg['formats'] as $f=>$fcfg) { $this->formats[$f] = & $workbook->addFormat(); foreach((array)$fcfg as $k=>$v) { - $this->formats[$f]->{'set' . $k}($v); + $this->formats[$f]->{'set' . $k}($v); } } @@ -239,6 +244,7 @@ class Pman_Core_SimpleExcel extends Pman if (empty($data)) { return; } + foreach($cfg['cols'] as $c => $col_cfg) { if (isset($col_cfg['renderer'])) { $hasRender = true; @@ -254,7 +260,7 @@ class Pman_Core_SimpleExcel extends Pman $data->find(); while($data->fetch()) { - $hasRenderRow = $this->addLine($cfg['workbook'], $clo); + $hasRenderRow = $this->addLine($cfg['workbook'], $data); $hasRender = ($hasRender || $hasRenderRow) ? true : false; } $start_row += $count; @@ -275,8 +281,7 @@ class Pman_Core_SimpleExcel extends Pman continue; } if (!empty($col_cfg['renderer'])) { - - if (is_a($col_cfg['renderer'], 'Closure')) { + if (is_a($col_cfg['renderer'], 'Closure')) { $col_cfg['renderer']($cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl); } else { // not sure if row is correct here...!!!? @@ -366,6 +371,18 @@ class Pman_Core_SimpleExcel extends Pman } if (isset($col_cfg['renderer'])) { $hasRender = true; + + $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : ''; + if (empty($cl[$col_cfg['dataIndex']])) { + continue; + } + $this->postRender[] = array( + $col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $start_row+$r+1, $c, $cl + ); + + + + continue; } @@ -377,12 +394,26 @@ class Pman_Core_SimpleExcel extends Pman $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false; // print_R(array($start_row+$r, $c, $v, $format));exit; // handle 0 prefixes.. - if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.') + + if ($dataFormat == 'date' || preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $v)) { + $dataFormat = 'date'; + $format = empty($format) ? $this->formats['_default_date_format_']: $format; + $ut_to_ed_diff = 86400 * 25569; + $gmt = strtotime('1970-01-01'); + + $v = (strtotime($v) + $ut_to_ed_diff - $gmt) / 86400; + // need to +8hrs to get real time.. + + + } + + if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.' ) || $dataFormat == 'string' ) { + //var_dump("Write ( {$r}, {$c} ) = " . $v); $worksheet->writeString($start_row+$r, $c, $v, $format); } else { - + //var_dump("Write String ( {$r}, {$c} ) = " . $v); $worksheet->write($start_row+$r, $c, $v, $format); } @@ -400,6 +431,20 @@ class Pman_Core_SimpleExcel extends Pman function send($fname) { + + if (!empty($this->postRender)) { + foreach($this->postRender as $ar) { + if (is_a($ar[0], 'Closure')) { + $ar[0]($ar[1], $ar[2], $ar[3], $ar[4], $ar[5]); + } else { + // not sure if row is correct here...!!!? + call_user_func($ar[0],$ar[1], $ar[2], $ar[3], $ar[4], $ar[5]); + } + } + + } + + if (!empty($this->workbook)) { $this->workbook->close(); $this->workbook = false;