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
50 function Pman_Core_SimpleExcel($data,$cfg)
52 // print_r($cfg);exit;
53 require_once 'Spreadsheet/Excel/Writer.php';
54 // Creating a workbook
55 $outfile2 = $this->tempName('xls');
56 // var_dump($outfile2);
57 $workbook = new Spreadsheet_Excel_Writer($outfile2);
58 //$workbook = new Spreadsheet_Excel_Writer();
59 $workbook->setVersion(8);
60 // sending HTTP headers
64 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
65 foreach($cfg['formats'] as $f=>$fcfg) {
66 $formats[$f] = & $workbook->addFormat();
67 foreach($fcfg as $k=>$v) {
68 $formats[$f]->{'set' . $k}($v);
74 if (empty($cfg['workbooks'])) {
75 $this->buildpage( $workbook, $formats , $data,$cfg);
77 foreach($cfg['workbooks'] as $i =>$wcfg) {
78 $this->buildpage( $workbook, $formats , $data[$i],$wcfg);
83 if (empty($cfg['leave_open'])) {
85 $this->outfile2 = $outfile2;
90 $this->outfile2 = $outfile2;
95 static function date($str)
98 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
103 function buildpage($workbook, $formats , $data,$cfg)
105 //echo '<PRE>'; print_R($cfg);
106 // print_r($cfg);exit;
107 // Creating a worksheet
108 $worksheet = $workbook->addWorksheet($cfg['workbook']);
109 if (is_a($worksheet, 'PEAR_Error')) {
110 die($worksheet->toString());
112 //print_R($worksheet);
113 $worksheet->setInputEncoding('UTF-8');
119 if (!empty($cfg['head'])) {
120 foreach($cfg['head'] as $row) {
121 foreach($row as $c => $col) {
122 if (is_array($col)) {
123 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
124 $worksheet->write($start_row, $c, $col[0], $format);
128 $worksheet->write($start_row, $c, $col);
140 foreach($cfg['cols'] as $c=>$col_cfg) {
141 if (is_string($col_cfg)) {
142 $cfg['cols'][$c] = array(
143 'header' => $col_cfg,
144 'dataIndex' => $col_cfg,
152 foreach($cfg['cols'] as $c=>$col_cfg) {
153 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
154 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
155 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
160 // DB_DataObject::debugLevel(1);
161 foreach($data as $r=>$clo) {
163 if (is_object($clo)) {
164 $cl = (array)$clo; // lossless converstion..
167 if (isset($cfg['row_height'])) {
168 $worksheet->setRow($start_row +$r, $cfg['row_height']);
171 foreach($cfg['cols'] as $c=>$col_cfg) {
173 if(isset($cl[$col_cfg['dataIndex']])){
174 $v = $cl[$col_cfg['dataIndex']];
176 if(isset($col_cfg['fillBlank'])){
177 $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
182 if (empty($cl[$col_cfg['dataIndex']])) {
185 if (isset($col_cfg['txtrenderer'])) {
186 $v = call_user_func($col_cfg['txtrenderer'],
187 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
193 if (isset($col_cfg['renderer'])) {
198 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
200 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
202 $worksheet->write($start_row+$r, $c, $v, $format);
205 /// call user render on any that are defined..
207 foreach($data as $r=>$cl) {
209 foreach($cfg['cols'] as $c=>$col_cfg) {
210 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
211 if (empty($cl[$col_cfg['dataIndex']])) {
214 if (isset($col_cfg['renderer'])) {
215 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
218 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
223 $start_row += count($data);
225 if (!empty($cfg['foot'])) {
226 foreach($cfg['foot'] as $row) {
227 foreach($row as $c => $col) {
228 // if it's an array? - formated ???
229 if (is_array($col)) {
230 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
231 $worksheet->write($start_row, $c, $col[0], $format);
234 $worksheet->write($start_row, $c, $col);
249 function addLine($worksheet, $data)
261 require_once 'File/Convert.php';
262 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
263 $fn = $fc->convert("application/vnd.ms-excel");
264 $fc->serve('attachment',$fn); // can fix IE Mess