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] = & $workbook->addFormat();
75 foreach($fcfg as $k=>$v) {
76 $formats[$f]->{'set' . $k}($v);
82 if (!empty($cfg['workbook'])) {
83 $this->buildPage( $formats , $data,$cfg);
84 } elseif (!empty($cfg['workbooks'])) {
85 foreach($cfg['workbooks'] as $i =>$wcfg) {
86 $this->buildPage( $formats , $data[$i],$wcfg);
90 // if workbooks == false - > the user can call buildpage..
93 if (!empty($cfg['leave_open'])) {
94 $this->outfile2 = $outfile2;
99 $this->outfile2 = $outfile2;
106 static function date($str)
109 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
114 function buildPage( $formats , $data, $cfg)
116 $workbook = $this->workbook;
117 //echo '<PRE>'; print_R($cfg);
118 // print_r($cfg);exit;
119 // Creating a worksheet
120 //print_R($cfg);exit;
121 // copy the config and alias so that book can be written to..
122 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
124 $this->formats = $formats;
125 //var_dump($cfg['workbook']);
127 $worksheet = $workbook->addWorksheet($cfg['workbook']);
128 if (is_a($worksheet, 'PEAR_Error')) {
129 die($worksheet->toString());
131 //print_R($worksheet);
132 $worksheet->setInputEncoding('UTF-8');
134 $this->worksheet = $worksheet;
138 if (!empty($cfg['head'])) {
139 foreach($cfg['head'] as $row) {
140 foreach($row as $c => $col) {
141 if (is_array($col)) {
142 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
143 $worksheet->write($start_row, $c, $col[0], $format);
147 $worksheet->write($start_row, $c, $col);
153 if(!isset($cfg['nonspacer'])){ $start_row++; }
157 foreach($cfg['cols'] as $c=>$col_cfg) {
158 if (is_string($col_cfg)) {
159 $cfg['cols'][$c] = array(
160 'header' => $col_cfg,
161 'dataIndex' => $col_cfg,
168 foreach($cfg['cols'] as $c=>$col_cfg) {
169 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
170 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
171 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
174 $this->start_row = &$start_row;
180 foreach($data as $r=>$clo) {
181 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
182 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
185 /// call user render on any that are defined..
187 foreach($data as $r=>$cl) {
189 foreach($cfg['cols'] as $c=>$col_cfg) {
190 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
191 if (empty($cl[$col_cfg['dataIndex']])) {
194 if (isset($col_cfg['renderer'])) {
195 // not sure if row is correct here...!!!?
196 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
199 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
204 $start_row += count($data);
206 if (!empty($cfg['foot'])) {
207 foreach($cfg['foot'] as $row) {
208 foreach($row as $c => $col) {
209 // if it's an array? - formated ???
210 if (is_array($col)) {
211 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
212 $worksheet->write($start_row, $c, $col[0], $format);
215 $worksheet->write($start_row, $c, $col);
228 function addLine($worksheet_name, $clo)
230 $cfg = $this->worksheet_cfg[$worksheet_name];
231 $start_row = $this->start_row;
232 $formats = $this->formats;
233 $worksheet = $this->worksheet;
239 if (is_object($clo)) {
240 $cl = (array)$clo; // lossless converstion..
243 if (isset($cfg['row_height'])) {
244 $worksheet->setRow($start_row +$r, $cfg['row_height']);
247 foreach($cfg['cols'] as $c=>$col_cfg) {
249 if(isset($cl[$col_cfg['dataIndex']])){
250 $v = $cl[$col_cfg['dataIndex']];
252 if(isset($col_cfg['fillBlank'])){
253 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
258 if (!isset($cl[$col_cfg['dataIndex']])) {
261 if (isset($col_cfg['txtrenderer'])) {
262 $v = call_user_func($col_cfg['txtrenderer'],
263 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
269 if (isset($col_cfg['renderer'])) {
274 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
276 $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
277 $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
278 // print_R(array($start_row+$r, $c, $v, $format));exit;
279 // handle 0 prefixes..
280 if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.')
282 $dataFormat == 'string' ) {
283 $worksheet->writeString($start_row+$r, $c, $v, $format);
286 $worksheet->write($start_row+$r, $c, $v, $format);
295 function send($fname)
297 if (!empty($this->workbook)) {
298 $this->workbook->close();
299 $this->workbook = false;
302 require_once 'File/Convert.php';
303 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
304 $fn = $fc->convert("application/vnd.ms-excel");
305 $fc->serve('attachment',$fname); // can fix IE Mess