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 * 'color' => 'yellow', // set color for the cell which is a header element
35 * 'fillBlank' => 'gray', // set the color for the cell which is a blank area
38 // if this is set then it will add a tab foreach one.
48 require_once 'Pman.php';
51 class Pman_Core_SimpleExcel extends Pman
54 var $worksheet_cfg = array();
56 var $formats = array();
57 var $workbook = false;
58 var $worksheet= false;
60 function Pman_Core_SimpleExcel($data,$cfg)
62 // print_r($cfg);exit;
63 require_once 'Spreadsheet/Excel/Writer.php';
64 // Creating a workbook
65 $outfile2 = $this->tempName('xls');
66 // var_dump($outfile2);
67 $workbook = new Spreadsheet_Excel_Writer($outfile2);
68 //$workbook = new Spreadsheet_Excel_Writer();
69 $workbook->setVersion(8);
70 // sending HTTP headers
71 $this->workbook = $workbook;
75 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
77 foreach($cfg['formats'] as $f=>$fcfg) {
79 $formats[$f] = & $workbook->addFormat();
80 foreach((array)$fcfg as $k=>$v) {
81 $formats[$f]->{'set' . $k}($v);
87 if (!empty($cfg['workbook'])) {
88 $this->buildPage( $formats , $data,$cfg);
89 } elseif (!empty($cfg['workbooks'])) {
90 foreach($cfg['workbooks'] as $i =>$wcfg) {
91 $this->buildPage( $formats , $data[$i],$wcfg);
95 // if workbooks == false - > the user can call buildpage..
98 if (!empty($cfg['leave_open'])) {
99 $this->outfile2 = $outfile2;
104 $this->outfile2 = $outfile2;
111 static function date($str)
114 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
119 function buildPage( $formats , $data, $cfg)
121 $workbook = $this->workbook;
122 //echo '<PRE>'; print_R($cfg);
123 // print_r($cfg);exit;
124 // Creating a worksheet
125 //print_R($cfg);exit;
126 // copy the config and alias so that book can be written to..
127 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
130 //$this->formats = (array)$formats;
132 if (isset($cfg['formats']) && empty($formats)) {
134 foreach($cfg['formats'] as $f=>$fcfg) {
136 $formats[$f] = & $workbook->addFormat();
137 foreach((array)$fcfg as $k=>$v) {
138 $formats[$f]->{'set' . $k}($v);
143 $this->formats = $formats;
148 //var_dump($cfg['workbook']);
150 $worksheet = $workbook->addWorksheet($cfg['workbook']);
151 if (is_a($worksheet, 'PEAR_Error')) {
152 die($worksheet->toString());
154 //print_R($worksheet);
155 $worksheet->setInputEncoding('UTF-8');
157 $this->worksheet = $worksheet;
161 if (!empty($cfg['head'])) {
162 foreach($cfg['head'] as $row) {
163 foreach($row as $c => $col) {
164 if (is_array($col)) {
165 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
166 $worksheet->write($start_row, $c, $col[0], $format);
170 $worksheet->write($start_row, $c, $col);
176 if(!isset($cfg['nonspacer'])){ $start_row++; }
180 foreach($cfg['cols'] as $c=>$col_cfg) {
181 if (is_string($col_cfg)) {
182 $cfg['cols'][$c] = array(
183 'header' => $col_cfg,
184 'dataIndex' => $col_cfg,
191 foreach($cfg['cols'] as $c=>$col_cfg) {
192 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
193 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
194 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
197 $this->start_row = &$start_row;
207 foreach($data as $r=>$clo) {
208 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
209 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
212 /// call user render on any that are defined..
214 foreach($data as $r=>$cl) {
216 foreach($cfg['cols'] as $c=>$col_cfg) {
217 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
218 if (empty($cl[$col_cfg['dataIndex']])) {
221 if (isset($col_cfg['renderer'])) {
222 // not sure if row is correct here...!!!?
223 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
226 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
231 $start_row += count($data);
233 if (!empty($cfg['foot'])) {
234 foreach($cfg['foot'] as $row) {
235 foreach($row as $c => $col) {
236 // if it's an array? - formated ???
237 if (is_array($col)) {
238 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
239 $worksheet->write($start_row, $c, $col[0], $format);
242 $worksheet->write($start_row, $c, $col);
252 if(!empty($cfg['merged_ranges'])){
253 $worksheet->_merged_ranges = $cfg['merged_ranges'];
256 $worksheet->setBorder(1);
260 function addLine($worksheet_name, $clo)
262 $cfg = $this->worksheet_cfg[$worksheet_name];
263 $start_row = $this->start_row;
264 $formats = (array)$this->formats;
265 $worksheet = $this->worksheet;
271 if (is_object($clo)) {
272 $cl = (array)$clo; // lossless converstion..
275 if (isset($cfg['row_height'])) {
276 $worksheet->setRow($start_row +$r, $cfg['row_height']);
279 foreach($cfg['cols'] as $c=>$col_cfg) {
281 if(isset($cl[$col_cfg['dataIndex']])){
282 $v = $cl[$col_cfg['dataIndex']];
284 if(isset($col_cfg['fillBlank'])){
285 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
290 if (!isset($cl[$col_cfg['dataIndex']])) {
293 if (isset($col_cfg['txtrenderer'])) {
294 $v = call_user_func($col_cfg['txtrenderer'],
295 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
301 if (isset($col_cfg['renderer'])) {
306 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
308 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
312 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
313 // print_R(array($start_row+$r, $c, $v, $format));exit;
314 // handle 0 prefixes..
315 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
317 $dataFormat == 'string' ) {
318 $worksheet->writeString($start_row+$r, $c, $v, $format);
321 $worksheet->write($start_row+$r, $c, $v, $format);
330 function send($fname)
332 if (!empty($this->workbook)) {
333 $this->workbook->close();
334 $this->workbook = false;
337 require_once 'File/Convert.php';
338 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
339 $fn = $fc->convert("application/vnd.ms-excel");
340 $fc->serve('attachment',$fname); // can fix IE Mess