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]
25 'header'=> "Thumbnail",
27 * 'dataFormat' => 'string' // to force a string..
29 'renderer' => array($this, 'getThumb'),
30 * 'color' => 'yellow', // set color for the cell which is a header element
31 * 'fillBlank' => 'gray', // set the color for the cell which is a blank area
34 // if this is set then it will add a tab foreach one.
44 require_once 'Pman.php';
47 class Pman_Core_SimpleExcel extends Pman
50 var $worksheet_cfg = array();
52 var $formats = array();
53 var $workbook = false;
54 var $worksheet= false;
56 function Pman_Core_SimpleExcel($data,$cfg)
58 // print_r($cfg);exit;
59 require_once 'Spreadsheet/Excel/Writer.php';
60 // Creating a workbook
61 $outfile2 = $this->tempName('xls');
62 // var_dump($outfile2);
63 $workbook = new Spreadsheet_Excel_Writer($outfile2);
64 //$workbook = new Spreadsheet_Excel_Writer();
65 $workbook->setVersion(8);
66 // sending HTTP headers
67 $this->workbook = $workbook;
71 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
73 foreach($cfg['formats'] as $f=>$fcfg) {
74 $formats[$f] = (array) $fcfg;
75 $formats[$f] = & $workbook->addFormat();
76 foreach((array)$fcfg as $k=>$v) {
77 $formats[$f]->{'set' . $k}($v);
83 if (!empty($cfg['workbook'])) {
84 $this->buildPage( $formats , $data,$cfg);
85 } elseif (!empty($cfg['workbooks'])) {
86 foreach($cfg['workbooks'] as $i =>$wcfg) {
87 $this->buildPage( $formats , $data[$i],$wcfg);
91 // if workbooks == false - > the user can call buildpage..
94 if (!empty($cfg['leave_open'])) {
95 $this->outfile2 = $outfile2;
100 $this->outfile2 = $outfile2;
107 static function date($str)
110 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
115 function buildPage( $formats , $data, $cfg)
117 $workbook = $this->workbook;
118 //echo '<PRE>'; print_R($cfg);
119 // print_r($cfg);exit;
120 // Creating a worksheet
121 //print_R($cfg);exit;
122 // copy the config and alias so that book can be written to..
123 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
125 $this->formats = $formats;
127 if (isset($cfg['formats']) && empty($formats)) {
128 $this->formats = $cfg['formats'];
130 //var_dump($cfg['workbook']);
132 $worksheet = $workbook->addWorksheet($cfg['workbook']);
133 if (is_a($worksheet, 'PEAR_Error')) {
134 die($worksheet->toString());
136 //print_R($worksheet);
137 $worksheet->setInputEncoding('UTF-8');
139 $this->worksheet = $worksheet;
143 if (!empty($cfg['head'])) {
144 foreach($cfg['head'] as $row) {
145 foreach($row as $c => $col) {
146 if (is_array($col)) {
147 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
148 $worksheet->write($start_row, $c, $col[0], $format);
152 $worksheet->write($start_row, $c, $col);
158 if(!isset($cfg['nonspacer'])){ $start_row++; }
162 foreach($cfg['cols'] as $c=>$col_cfg) {
163 if (is_string($col_cfg)) {
164 $cfg['cols'][$c] = array(
165 'header' => $col_cfg,
166 'dataIndex' => $col_cfg,
173 foreach($cfg['cols'] as $c=>$col_cfg) {
174 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
175 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
176 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
179 $this->start_row = &$start_row;
189 foreach($data as $r=>$clo) {
190 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
191 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
194 /// call user render on any that are defined..
196 foreach($data as $r=>$cl) {
198 foreach($cfg['cols'] as $c=>$col_cfg) {
199 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
200 if (empty($cl[$col_cfg['dataIndex']])) {
203 if (isset($col_cfg['renderer'])) {
204 // not sure if row is correct here...!!!?
205 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
208 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
213 $start_row += count($data);
215 if (!empty($cfg['foot'])) {
216 foreach($cfg['foot'] as $row) {
217 foreach($row as $c => $col) {
218 // if it's an array? - formated ???
219 if (is_array($col)) {
220 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
221 $worksheet->write($start_row, $c, $col[0], $format);
224 $worksheet->write($start_row, $c, $col);
237 function addLine($worksheet_name, $clo)
239 $cfg = $this->worksheet_cfg[$worksheet_name];
240 $start_row = $this->start_row;
241 $formats = $this->formats;
242 $worksheet = $this->worksheet;
248 if (is_object($clo)) {
249 $cl = (array)$clo; // lossless converstion..
252 if (isset($cfg['row_height'])) {
253 $worksheet->setRow($start_row +$r, $cfg['row_height']);
256 foreach($cfg['cols'] as $c=>$col_cfg) {
258 if(isset($cl[$col_cfg['dataIndex']])){
259 $v = $cl[$col_cfg['dataIndex']];
261 if(isset($col_cfg['fillBlank'])){
262 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
267 if (!isset($cl[$col_cfg['dataIndex']])) {
270 if (isset($col_cfg['txtrenderer'])) {
271 $v = call_user_func($col_cfg['txtrenderer'],
272 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
278 if (isset($col_cfg['renderer'])) {
283 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
285 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
286 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
287 // print_R(array($start_row+$r, $c, $v, $format));exit;
288 // handle 0 prefixes..
289 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
291 $dataFormat == 'string' ) {
292 $worksheet->writeString($start_row+$r, $c, $v, $format);
295 $worksheet->write($start_row+$r, $c, $v, $format);
304 function send($fname)
306 if (!empty($this->workbook)) {
307 $this->workbook->close();
308 $this->workbook = false;
311 require_once 'File/Convert.php';
312 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
313 $fn = $fc->convert("application/vnd.ms-excel");
314 $fc->serve('attachment',$fname); // can fix IE Mess