4 * class to generate excel file from rows of data, and a configuration.
7 * $x = new Pman_Core_SimpleExcel($data_array, array())
13 new Pman_Core_SimpleExcel($data_array, array(
15 'format_name' => array( 'Align' => 'left' ), // etc...
17 'workbook' => 'name_of_workbook'm
20 array ( 'this', 'is', 'the' , 'first', 'row'),
21 array ( 'this', 'is', 'the' , 'seconde', 'row'), // ... etc.
22 array ( array( 'string', 'format_name') , 'another cell') , // with formating..
25 'merged_ranges' => array(
26 array($first_row, $first_col, $last_row, $last_col),
27 array($first_row, $first_col, $last_row, $last_col),
31 'header'=> "Thumbnail",
33 'dataFormat' => 'string' // to force a string..
35 'renderer' => array($this, 'getThumb'),
36 'txtrenderer' => function($value, $worksheet, $row, $col, $row_data) {
38 }, // for text content...
39 'color' => 'yellow', // set color for the cell which is a header element
40 'fillBlank' => 'gray', // set the color for the cell which is a blank area
42 //..... and ther rows...
45 // if this is set then it will add a tab foreach one.
47 workbook => '....' // ???
49 'leave_open' => false, // if you call addrows?? later..
55 function($value, $worksheet, $row, $col, $row_data)
57 // callbacks : txtrenderer
58 function($value, $worksheet, $row, $col, $row_data)
66 require_once 'Pman.php';
69 class Pman_Core_SimpleExcel extends Pman
72 var $worksheet_cfg = array();
74 var $formats = array();
75 var $workbook = false;
76 var $worksheet= false;
78 function Pman_Core_SimpleExcel($data,$cfg)
80 // print_r($cfg);exit;
81 require_once 'Spreadsheet/Excel/Writer.php';
82 // Creating a workbook
83 $outfile2 = $this->tempName('xls');
84 // var_dump($outfile2);
85 $workbook = new Spreadsheet_Excel_Writer($outfile2);
86 //$workbook = new Spreadsheet_Excel_Writer();
87 $workbook->setVersion(8);
88 // sending HTTP headers
89 $this->workbook = $workbook;
93 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
95 foreach($cfg['formats'] as $f=>$fcfg) {
97 $this->formats[$f] = & $workbook->addFormat();
98 foreach((array)$fcfg as $k=>$v) {
99 $this->formats[$f]->{'set' . $k}($v);
105 if (!empty($cfg['workbook'])) {
106 $this->buildPage( array(), $data,$cfg);
107 } elseif (!empty($cfg['workbooks'])) {
108 foreach($cfg['workbooks'] as $i =>$wcfg) {
109 $this->buildPage( array() , $data[$i],$wcfg);
113 // if workbooks == false - > the user can call buildpage..
116 if (!empty($cfg['leave_open'])) {
117 $this->outfile2 = $outfile2;
122 $this->outfile2 = $outfile2;
129 static function date($str)
132 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
137 function buildPage( $formats , $data, $cfg)
139 $workbook = $this->workbook;
140 //echo '<PRE>'; print_R($cfg);
141 // print_r($cfg);exit;
142 // Creating a worksheet
143 //print_R($cfg);exit;
144 // copy the config and alias so that book can be written to..
145 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
148 //$this->formats = (array)$formats;
150 foreach($formats as $k=>$fcfg) {
151 if (!isset($this->formats[$f])) {
152 $this->formats[$f] = & $workbook->addFormat();
154 if (is_a($fcfg,'Spreadsheet_Excel_Writer_Format')) {
155 continue; // skip!?!?
158 foreach((array)$fcfg as $k=>$v) {
159 $this->formats[$f]->{'set' . $k}($v);
163 if (isset($cfg['formats'])) {
165 foreach($cfg['formats'] as $f=>$fcfg) {
166 if (!isset($this->formats[$f])) {
167 $this->formats[$f] = & $workbook->addFormat();
169 foreach((array)$fcfg as $k=>$v) {
170 $this->formats[$f]->{'set' . $k}($v);
180 //var_dump($cfg['workbook']);
182 $worksheet = $workbook->addWorksheet($cfg['workbook']);
183 if (is_a($worksheet, 'PEAR_Error')) {
184 die($worksheet->toString());
186 //print_R($worksheet);
187 $worksheet->setInputEncoding('UTF-8');
189 if(!empty($cfg['merged_ranges'])){ // merge cell
190 $worksheet->_merged_ranges = $cfg['merged_ranges'];
193 $this->worksheet = $worksheet;
197 if (!empty($cfg['head'])) {
198 foreach($cfg['head'] as $row) {
199 foreach($row as $c => $col) {
200 if (is_array($col)) {
201 $format = isset($this->formats[$col[1]] ) ?$this->formats[$col[1]] : false;
202 $worksheet->write($start_row, $c, $col[0], $format);
206 $worksheet->write($start_row, $c, $col);
212 if(!isset($cfg['nonspacer'])){ $start_row++; }
216 foreach($cfg['cols'] as $c=>$col_cfg) {
217 if (is_string($col_cfg)) {
218 $cfg['cols'][$c] = array(
219 'header' => $col_cfg,
220 'dataIndex' => $col_cfg,
227 foreach($cfg['cols'] as $c=>$col_cfg) {
229 $format = isset($col_cfg['color']) && isset($this->formats[$col_cfg['color']]) ? $this->formats[$col_cfg['color']] : false;
230 $worksheet->write($start_row, $c, @$col_cfg['header'],$format);
231 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
234 $this->start_row = &$start_row;
242 foreach($cfg['cols'] as $c => $col_cfg) {
243 if (isset($col_cfg['renderer'])) {
251 if (is_object($data)) {
253 $count = $data->count();
256 while($data->fetch()) {
257 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
258 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
260 $start_row += $count;
263 foreach($data as $r=>$clo) {
264 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
265 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
268 /// call user render on any that are defined..
270 foreach($data as $r=>$cl) {
272 foreach($cfg['cols'] as $c=>$col_cfg) {
273 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
274 if (empty($cl[$col_cfg['dataIndex']])) {
277 if (!empty($col_cfg['renderer'])) {
279 if (is_a($col_cfg['renderer'], 'Closure')) {
280 $col_cfg['renderer']($cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
282 // not sure if row is correct here...!!!?
283 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
287 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
292 $start_row += count($data);
296 if (!empty($cfg['foot'])) {
297 foreach($cfg['foot'] as $row) {
298 foreach($row as $c => $col) {
299 // if it's an array? - formated ???
300 if (is_array($col)) {
301 $format = isset($this->formats[$col[1]] ) ? $this->formats[$col[1]] : false;
302 $worksheet->write($start_row, $c, $col[0], $format);
305 $worksheet->write($start_row, $c, $col);
316 function addLine($worksheet_name, $clo)
318 $cfg = $this->worksheet_cfg[$worksheet_name];
319 $start_row = $this->start_row;
320 $formats = (array)$this->formats;
321 $worksheet = $this->worksheet;
327 if (is_object($clo)) {
328 $cl = (array)$clo; // lossless converstion..
331 if (isset($cfg['row_height'])) {
332 $worksheet->setRow($start_row +$r, $cfg['row_height']);
335 $line_height = (isset($cfg['line_height'])) ? $cfg['line_height'] : 12;
338 foreach($cfg['cols'] as $c => $col_cfg) {
340 if(isset($col_cfg['dataIndex']) && isset($cl[$col_cfg['dataIndex']])){
341 $v = $cl[$col_cfg['dataIndex']] ;
344 if(isset($col_cfg['fillBlank'])){
345 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
350 if (!isset($cl[$col_cfg['dataIndex']])) {
353 if (isset($col_cfg['txtrenderer'])) {
355 if (is_a($col_cfg['txtrenderer'], 'Closure')) {
357 $v = $col_cfg['txtrenderer']($cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
359 $v = call_user_func($col_cfg['txtrenderer'],
360 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
367 if (isset($col_cfg['renderer'])) {
372 $v = @iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $v);
374 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
377 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
378 // print_R(array($start_row+$r, $c, $v, $format));exit;
379 // handle 0 prefixes..
380 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
382 $dataFormat == 'string' ) {
383 $worksheet->writeString($start_row+$r, $c, $v, $format);
386 $worksheet->write($start_row+$r, $c, $v, $format);
389 if(isset($col_cfg['autoHeight'])){
390 $vv = explode("\n", $v);
391 $height = MAX(count($vv) * $line_height, $height);;
392 $worksheet->setRow($start_row+$r, $height);
401 function send($fname)
403 if (!empty($this->workbook)) {
404 $this->workbook->close();
405 $this->workbook = false;
408 require_once 'File/Convert.php';
409 //var_Dump($this->outfile2);
410 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
411 $fn = $fc->convert("application/vnd.ms-excel");
413 $fc->serve('attachment',$fname); // can fix IE Mess