4 * class to generate excel file from rows of data, and a configuration.
7 * $x = new Pman_Core_SimpleExcel(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' => array($this, 'cleanValue'), // for
37 'color' => 'yellow', // set color for the cell which is a header element
38 'fillBlank' => 'gray', // set the color for the cell which is a blank area
40 //..... and ther rows...
43 // if this is set then it will add a tab foreach one.
45 workbook => '....' // ???
47 'leave_open' => false, // if you call addrows?? later..
53 function($value, $worksheet, $row, $col, $row_data)
55 // callbacks : txtrenderer
56 function($value, $worksheet, $row, $col, $row_data)
64 require_once 'Pman.php';
67 class Pman_Core_SimpleExcel extends Pman
70 var $worksheet_cfg = array();
72 var $formats = array();
73 var $workbook = false;
74 var $worksheet= false;
76 function Pman_Core_SimpleExcel($data,$cfg)
78 // print_r($cfg);exit;
79 require_once 'Spreadsheet/Excel/Writer.php';
80 // Creating a workbook
81 $outfile2 = $this->tempName('xls');
82 // var_dump($outfile2);
83 $workbook = new Spreadsheet_Excel_Writer($outfile2);
84 //$workbook = new Spreadsheet_Excel_Writer();
85 $workbook->setVersion(8);
86 // sending HTTP headers
87 $this->workbook = $workbook;
91 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
93 foreach($cfg['formats'] as $f=>$fcfg) {
95 $this->formats[$f] = & $workbook->addFormat();
96 foreach((array)$fcfg as $k=>$v) {
97 $this->formats[$f]->{'set' . $k}($v);
103 if (!empty($cfg['workbook'])) {
104 $this->buildPage( array(), $data,$cfg);
105 } elseif (!empty($cfg['workbooks'])) {
106 foreach($cfg['workbooks'] as $i =>$wcfg) {
107 $this->buildPage( array() , $data[$i],$wcfg);
111 // if workbooks == false - > the user can call buildpage..
114 if (!empty($cfg['leave_open'])) {
115 $this->outfile2 = $outfile2;
120 $this->outfile2 = $outfile2;
127 static function date($str)
130 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
135 function buildPage( $formats , $data, $cfg)
137 $workbook = $this->workbook;
138 //echo '<PRE>'; print_R($cfg);
139 // print_r($cfg);exit;
140 // Creating a worksheet
141 //print_R($cfg);exit;
142 // copy the config and alias so that book can be written to..
143 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
146 //$this->formats = (array)$formats;
148 foreach($formats as $k=>$fcfg) {
149 if (!isset($this->formats[$f])) {
150 $this->formats[$f] = & $workbook->addFormat();
152 if (is_a($fcfg,'Spreadsheet_Excel_Writer_Format')) {
153 continue; // skip!?!?
156 foreach((array)$fcfg as $k=>$v) {
157 $this->formats[$f]->{'set' . $k}($v);
161 if (isset($cfg['formats'])) {
163 foreach($cfg['formats'] as $f=>$fcfg) {
164 if (!isset($this->formats[$f])) {
165 $this->formats[$f] = & $workbook->addFormat();
167 foreach((array)$fcfg as $k=>$v) {
168 $this->formats[$f]->{'set' . $k}($v);
178 //var_dump($cfg['workbook']);
180 $worksheet = $workbook->addWorksheet($cfg['workbook']);
181 if (is_a($worksheet, 'PEAR_Error')) {
182 die($worksheet->toString());
184 //print_R($worksheet);
185 $worksheet->setInputEncoding('UTF-8');
187 if(!empty($cfg['merged_ranges'])){ // merge cell
188 $worksheet->_merged_ranges = $cfg['merged_ranges'];
191 $this->worksheet = $worksheet;
195 if (!empty($cfg['head'])) {
196 foreach($cfg['head'] as $row) {
197 foreach($row as $c => $col) {
198 if (is_array($col)) {
199 $format = isset($this->formats[$col[1]] ) ?$this->formats[$col[1]] : false;
200 $worksheet->write($start_row, $c, $col[0], $format);
204 $worksheet->write($start_row, $c, $col);
210 if(!isset($cfg['nonspacer'])){ $start_row++; }
214 foreach($cfg['cols'] as $c=>$col_cfg) {
215 if (is_string($col_cfg)) {
216 $cfg['cols'][$c] = array(
217 'header' => $col_cfg,
218 'dataIndex' => $col_cfg,
225 foreach($cfg['cols'] as $c=>$col_cfg) {
227 $format = isset($col_cfg['color']) && isset($this->formats[$col_cfg['color']]) ? $this->formats[$col_cfg['color']] : false;
228 $worksheet->write($start_row, $c, @$col_cfg['header'],$format);
229 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
232 $this->start_row = &$start_row;
242 foreach($data as $r=>$clo) {
243 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
244 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
247 /// call user render on any that are defined..
249 foreach($data as $r=>$cl) {
251 foreach($cfg['cols'] as $c=>$col_cfg) {
252 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
253 if (empty($cl[$col_cfg['dataIndex']])) {
256 if (isset($col_cfg['renderer'])) {
257 // not sure if row is correct here...!!!?
258 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
261 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
266 $start_row += count($data);
268 if (!empty($cfg['foot'])) {
269 foreach($cfg['foot'] as $row) {
270 foreach($row as $c => $col) {
271 // if it's an array? - formated ???
272 if (is_array($col)) {
273 $format = isset($this->formats[$col[1]] ) ? $this->formats[$col[1]] : false;
274 $worksheet->write($start_row, $c, $col[0], $format);
277 $worksheet->write($start_row, $c, $col);
288 function addLine($worksheet_name, $clo)
290 $cfg = $this->worksheet_cfg[$worksheet_name];
291 $start_row = $this->start_row;
292 $formats = (array)$this->formats;
293 $worksheet = $this->worksheet;
299 if (is_object($clo)) {
300 $cl = (array)$clo; // lossless converstion..
303 if (isset($cfg['row_height'])) {
304 $worksheet->setRow($start_row +$r, $cfg['row_height']);
307 $line_height = (isset($cfg['line_height'])) ? $cfg['line_height'] : 12;
310 foreach($cfg['cols'] as $c => $col_cfg) {
312 if(isset($col_cfg['dataIndex']) && isset($cl[$col_cfg['dataIndex']])){
313 $v = $cl[$col_cfg['dataIndex']] ;
316 if(isset($col_cfg['fillBlank'])){
317 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
322 if (!isset($cl[$col_cfg['dataIndex']])) {
325 if (isset($col_cfg['txtrenderer'])) {
326 $v = call_user_func($col_cfg['txtrenderer'],
327 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
333 if (isset($col_cfg['renderer'])) {
338 $v = @iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $v);
340 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
343 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
344 // print_R(array($start_row+$r, $c, $v, $format));exit;
345 // handle 0 prefixes..
346 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
348 $dataFormat == 'string' ) {
349 $worksheet->writeString($start_row+$r, $c, $v, $format);
352 $worksheet->write($start_row+$r, $c, $v, $format);
355 if(isset($col_cfg['autoHeight'])){
356 $vv = explode("\n", $v);
357 $height = MAX(count($vv) * $line_height, $height);;
358 $worksheet->setRow($start_row+$r, $height);
367 function send($fname)
369 if (!empty($this->workbook)) {
370 $this->workbook->close();
371 $this->workbook = false;
374 require_once 'File/Convert.php';
375 //var_Dump($this->outfile2);
376 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
377 $fn = $fc->convert("application/vnd.ms-excel");
379 $fc->serve('attachment',$fname); // can fix IE Mess