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",
28 'renderer' => array($this, 'getThumb'),
29 * 'color' => 'yellow', // set color for the cell which is a header element
30 * 'fillBlank' => 'gray', // set the color for the cell which is a blank area
33 // if this is set then it will add a tab foreach one.
45 class Pman_Core_SimpleExcel extends Pman
48 var $worksheet_cfg = array();
50 var $formats = array();
51 var $workbook = false;
52 var $worksheet= false;
54 function Pman_Core_SimpleExcel($data,$cfg)
56 // print_r($cfg);exit;
57 require_once 'Spreadsheet/Excel/Writer.php';
58 // Creating a workbook
59 $outfile2 = $this->tempName('xls');
60 // var_dump($outfile2);
61 $workbook = new Spreadsheet_Excel_Writer($outfile2);
62 //$workbook = new Spreadsheet_Excel_Writer();
63 $workbook->setVersion(8);
64 // sending HTTP headers
68 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
69 foreach($cfg['formats'] as $f=>$fcfg) {
70 $formats[$f] = & $workbook->addFormat();
71 foreach($fcfg as $k=>$v) {
72 $formats[$f]->{'set' . $k}($v);
78 if (empty($cfg['workbooks'])) {
79 $this->buildpage( $workbook, $formats , $data,$cfg);
81 foreach($cfg['workbooks'] as $i =>$wcfg) {
82 $this->buildpage( $workbook, $formats , $data[$i],$wcfg);
87 if (!empty($cfg['leave_open'])) {
88 $this->workbook = $workbook;
89 $this->outfile2 = $outfile2;
94 $this->outfile2 = $outfile2;
99 static function date($str)
102 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
107 function buildpage($workbook, $formats , $data,$cfg)
109 //echo '<PRE>'; print_R($cfg);
110 // print_r($cfg);exit;
111 // Creating a worksheet
112 //print_R($cfg);exit;
113 // copy the config and alias so that book can be written to..
114 $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
116 $worksheet = $workbook->addWorksheet($cfg['workbook']);
117 if (is_a($worksheet, 'PEAR_Error')) {
118 die($worksheet->toString());
120 //print_R($worksheet);
121 $worksheet->setInputEncoding('UTF-8');
123 $this->worksheet = $worksheet;
127 if (!empty($cfg['head'])) {
128 foreach($cfg['head'] as $row) {
129 foreach($row as $c => $col) {
130 if (is_array($col)) {
131 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
132 $worksheet->write($start_row, $c, $col[0], $format);
136 $worksheet->write($start_row, $c, $col);
146 foreach($cfg['cols'] as $c=>$col_cfg) {
147 if (is_string($col_cfg)) {
148 $cfg['cols'][$c] = array(
149 'header' => $col_cfg,
150 'dataIndex' => $col_cfg,
158 foreach($cfg['cols'] as $c=>$col_cfg) {
159 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
160 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
161 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
165 $this->start_row = &$start_row;
169 // DB_DataObject::debugLevel(1);
170 foreach($data as $r=>$clo) {
172 $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
173 $hasRender = ($hasRender || $hasRenderRow) ? true : false;
176 /// call user render on any that are defined..
178 foreach($data as $r=>$cl) {
180 foreach($cfg['cols'] as $c=>$col_cfg) {
181 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
182 if (empty($cl[$col_cfg['dataIndex']])) {
185 if (isset($col_cfg['renderer'])) {
186 // not sure if row is correct here...!!!?
187 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
190 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
195 $start_row += count($data);
197 if (!empty($cfg['foot'])) {
198 foreach($cfg['foot'] as $row) {
199 foreach($row as $c => $col) {
200 // if it's an array? - formated ???
201 if (is_array($col)) {
202 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
203 $worksheet->write($start_row, $c, $col[0], $format);
206 $worksheet->write($start_row, $c, $col);
215 $this->formats = $formats;
222 function addLine($worksheet_name, $clo)
224 $cfg = $this->worksheet_cfg[$worksheet_name];
225 $start_row = $this->start_row;
226 $formats = $this->formats;
227 $worksheet = $this->worksheet;
235 if (is_object($clo)) {
236 $cl = (array)$clo; // lossless converstion..
239 if (isset($cfg['row_height'])) {
240 $worksheet->setRow($start_row +$r, $cfg['row_height']);
243 foreach($cfg['cols'] as $c=>$col_cfg) {
245 if(isset($cl[$col_cfg['dataIndex']])){
246 $v = $cl[$col_cfg['dataIndex']];
248 if(isset($col_cfg['fillBlank'])){
249 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
254 if (empty($cl[$col_cfg['dataIndex']])) {
257 if (isset($col_cfg['txtrenderer'])) {
258 $v = call_user_func($col_cfg['txtrenderer'],
259 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
265 if (isset($col_cfg['renderer'])) {
270 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
273 $format = isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false;
274 // print_R(array($start_row+$r, $c, $v, $format));exit;
275 $worksheet->write($start_row+$r, $c, $v, $format);
285 if (!empty($this->workbook)) {
286 $this->workbook->close();
287 $this->workbook = false;
290 require_once 'File/Convert.php';
291 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
292 $fn = $fc->convert("application/vnd.ms-excel");
293 $fc->serve('attachment',$fn); // can fix IE Mess