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]
23 * merged_ranges : array(
24 * array($first_row, $first_col, $last_row, $last_col),
25 * * array($first_row, $first_col, $last_row, $last_col),
29 'header'=> "Thumbnail",
31 * 'dataFormat' => 'string' // to force a string..
33 'renderer' => array($this, 'getThumb'),
34 'txtrenderer' => array($this, 'cleanValue'), // for
35 * 'color' => 'yellow', // set color for the cell which is a header element
36 * 'fillBlank' => 'gray', // set the color for the cell which is a blank area
39 // if this is set then it will add a tab foreach one.
46 // callbacks: renderer
48 function($value, $worksheet, $row, $col, $row_data)
50 // callbacks : txtrenderer
51 function($value, $worksheet, $row, $col, $row_data)
59 require_once 'Pman.php';
62 class Pman_Core_SimpleExcel extends Pman
65 var $worksheet_cfg = array();
67 var $formats = array();
68 var $workbook = false;
69 var $worksheet= false;
71 function Pman_Core_SimpleExcel($data,$cfg)
73 // print_r($cfg);exit;
74 require_once 'Spreadsheet/Excel/Writer.php';
75 // Creating a workbook
76 $outfile2 = $this->tempName('xls');
77 // var_dump($outfile2);
78 $workbook = new Spreadsheet_Excel_Writer($outfile2);
79 //$workbook = new Spreadsheet_Excel_Writer();
80 $workbook->setVersion(8);
81 // sending HTTP headers
82 $this->workbook = $workbook;
86 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
88 foreach($cfg['formats'] as $f=>$fcfg) {
90 $this->formats[$f] = & $workbook->addFormat();
91 foreach((array)$fcfg as $k=>$v) {
92 $this->formats[$f]->{'set' . $k}($v);
98 if (!empty($cfg['workbook'])) {
99 $this->buildPage( array(), $data,$cfg);
100 } elseif (!empty($cfg['workbooks'])) {
101 foreach($cfg['workbooks'] as $i =>$wcfg) {
102 $this->buildPage( array() , $data[$i],$wcfg);
106 // if workbooks == false - > the user can call buildpage..
109 if (!empty($cfg['leave_open'])) {
110 $this->outfile2 = $outfile2;
115 $this->outfile2 = $outfile2;
122 static function date($str)
125 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
130 function buildPage( $formats , $data, $cfg)
132 $workbook = $this->workbook;
133 //echo '<PRE>'; print_R($cfg);
134 // print_r($cfg);exit;
135 // Creating a worksheet
136 //print_R($cfg);exit;
137 // copy the config and alias so that book can be written to..
138 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
141 //$this->formats = (array)$formats;
143 foreach($formats as $k=>$fcfg) {
144 if (!isset($this->formats[$f])) {
145 $this->formats[$f] = & $workbook->addFormat();
147 if (is_a($fcfg,'Spreadsheet_Excel_Writer_Format')) {
148 continue; // skip!?!?
151 foreach((array)$fcfg as $k=>$v) {
152 $this->formats[$f]->{'set' . $k}($v);
156 if (isset($cfg['formats'])) {
158 foreach($cfg['formats'] as $f=>$fcfg) {
159 if (!isset($this->formats[$f])) {
160 $this->formats[$f] = & $workbook->addFormat();
162 foreach((array)$fcfg as $k=>$v) {
163 $this->formats[$f]->{'set' . $k}($v);
173 //var_dump($cfg['workbook']);
175 $worksheet = $workbook->addWorksheet($cfg['workbook']);
176 if (is_a($worksheet, 'PEAR_Error')) {
177 die($worksheet->toString());
179 //print_R($worksheet);
180 $worksheet->setInputEncoding('UTF-8');
182 if(!empty($cfg['merged_ranges'])){ // merge cell
183 $worksheet->_merged_ranges = $cfg['merged_ranges'];
186 $this->worksheet = $worksheet;
190 if (!empty($cfg['head'])) {
191 foreach($cfg['head'] as $row) {
192 foreach($row as $c => $col) {
193 if (is_array($col)) {
194 $format = isset($this->formats[$col[1]] ) ?$this->formats[$col[1]] : false;
195 $worksheet->write($start_row, $c, $col[0], $format);
199 $worksheet->write($start_row, $c, $col);
205 if(!isset($cfg['nonspacer'])){ $start_row++; }
209 foreach($cfg['cols'] as $c=>$col_cfg) {
210 if (is_string($col_cfg)) {
211 $cfg['cols'][$c] = array(
212 'header' => $col_cfg,
213 'dataIndex' => $col_cfg,
220 foreach($cfg['cols'] as $c=>$col_cfg) {
222 $format = isset($col_cfg['color']) && isset($this->formats[$col_cfg['color']]) ? $this->formats[$col_cfg['color']] : false;
223 $worksheet->write($start_row, $c, @$col_cfg['header'],$format);
224 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
227 $this->start_row = &$start_row;
237 foreach($data as $r=>$clo) {
238 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
239 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
242 /// call user render on any that are defined..
244 foreach($data as $r=>$cl) {
246 foreach($cfg['cols'] as $c=>$col_cfg) {
247 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
248 if (empty($cl[$col_cfg['dataIndex']])) {
251 if (isset($col_cfg['renderer'])) {
252 // not sure if row is correct here...!!!?
253 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
256 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
261 $start_row += count($data);
263 if (!empty($cfg['foot'])) {
264 foreach($cfg['foot'] as $row) {
265 foreach($row as $c => $col) {
266 // if it's an array? - formated ???
267 if (is_array($col)) {
268 $format = isset($this->formats[$col[1]] ) ? $this->formats[$col[1]] : false;
269 $worksheet->write($start_row, $c, $col[0], $format);
272 $worksheet->write($start_row, $c, $col);
283 function addLine($worksheet_name, $clo)
285 $cfg = $this->worksheet_cfg[$worksheet_name];
286 $start_row = $this->start_row;
287 $formats = (array)$this->formats;
288 $worksheet = $this->worksheet;
294 if (is_object($clo)) {
295 $cl = (array)$clo; // lossless converstion..
298 if (isset($cfg['row_height'])) {
299 $worksheet->setRow($start_row +$r, $cfg['row_height']);
302 $line_height = (isset($cfg['line_height'])) ? $cfg['line_height'] : 12;
305 foreach($cfg['cols'] as $c => $col_cfg) {
307 if(isset($col_cfg['dataIndex']) && isset($cl[$col_cfg['dataIndex']])){
308 $v = $cl[$col_cfg['dataIndex']] ;
311 if(isset($col_cfg['fillBlank'])){
312 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
317 if (!isset($cl[$col_cfg['dataIndex']])) {
320 if (isset($col_cfg['txtrenderer'])) {
321 $v = call_user_func($col_cfg['txtrenderer'],
322 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
328 if (isset($col_cfg['renderer'])) {
333 $v = @iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $v);
335 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
338 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
339 // print_R(array($start_row+$r, $c, $v, $format));exit;
340 // handle 0 prefixes..
341 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
343 $dataFormat == 'string' ) {
344 $worksheet->writeString($start_row+$r, $c, $v, $format);
347 $worksheet->write($start_row+$r, $c, $v, $format);
350 if(isset($col_cfg['autoHeight'])){
351 $vv = explode("\n", $v);
352 $height = MAX(count($vv) * $line_height, $height);;
353 $worksheet->setRow($start_row+$r, $height);
362 function send($fname)
364 if (!empty($this->workbook)) {
365 $this->workbook->close();
366 $this->workbook = false;
369 require_once 'File/Convert.php';
370 var_Dump($this->outfile2);
371 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
372 $fn = $fc->convert("application/vnd.ms-excel");
374 $fc->serve('attachment',$fname); // can fix IE Mess