4 * class to generate excel file from rows of data, and a configuration.
7 * $x = new Pman_Core_SimpleExcel(array())
13 * name : [ Align : left, .... ]
24 'header'=> "Thumbnail",
27 'renderer' => array($this, 'getThumb')
30 // if this is set then it will add a tab foreach one.
42 class Pman_Core_SimpleExcel extends Pman
47 function Pman_Core_SimpleExcel($data,$cfg)
50 require_once 'Spreadsheet/Excel/Writer.php';
51 // Creating a workbook
52 $outfile2 = $this->tempName('xls');
53 // var_dump($outfile2);
54 $workbook = new Spreadsheet_Excel_Writer($outfile2);
55 //$workbook = new Spreadsheet_Excel_Writer();
56 $workbook->setVersion(8);
57 // sending HTTP headers
61 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
62 foreach($cfg['formats'] as $f=>$fcfg) {
63 $formats[$f] = & $workbook->addFormat();
64 foreach($fcfg as $k=>$v) {
65 $formats[$f]->{'set' . $k}($v);
71 if (empty($cfg['workbooks'])) {
72 $this->buildpage( $workbook, $formats , $data,$cfg);
74 foreach($cfg['workbooks'] as $i =>$wcfg) {
75 $this->buildpage( $workbook, $formats , $data[$i],$wcfg);
85 $this->outfile2 = $outfile2;
89 function buildpage($workbook, $formats , $data,$cfg)
91 //echo '<PRE>'; print_R($cfg);
93 // Creating a worksheet
94 $worksheet = $workbook->addWorksheet($cfg['workbook']);
95 if (is_a($worksheet, 'PEAR_Error')) {
96 die($worksheet->toString());
98 //print_R($worksheet);
99 $worksheet->setInputEncoding('UTF-8');
105 if (!empty($cfg['head'])) {
106 foreach($cfg['head'] as $row) {
107 foreach($row as $c => $col) {
108 $worksheet->write($start_row, $c, $col);
120 foreach($cfg['cols'] as $c=>$col_cfg) {
121 if (is_string($col_cfg)) {
122 $cfg['cols'][$c] = array(
123 'header' => $col_cfg,
124 'dataIndex' => $col_cfg,
132 foreach($cfg['cols'] as $c=>$col_cfg) {
136 $worksheet->write($start_row, $c, $col_cfg['header']);
137 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
142 // DB_DataObject::debugLevel(1);
143 foreach($data as $r=>$clo) {
145 if (is_object($clo)) {
146 $cl = (array)$clo; // lossless converstion..
149 if (isset($cfg['row_height'])) {
150 $worksheet->setRow($start_row +$r, $cfg['row_height']);
153 foreach($cfg['cols'] as $c=>$col_cfg) {
154 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
155 if (empty($cl[$col_cfg['dataIndex']])) {
158 if (isset($col_cfg['txtrenderer'])) {
159 $v = call_user_func($col_cfg['txtrenderer'],
160 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
166 if (isset($col_cfg['renderer'])) {
171 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
172 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
174 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
175 $worksheet->write($start_row+$r, $c, $v, $format);
178 /// call user render on any that are defined..
180 foreach($data as $r=>$cl) {
182 foreach($cfg['cols'] as $c=>$col_cfg) {
183 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
184 if (empty($cl[$col_cfg['dataIndex']])) {
187 if (isset($col_cfg['renderer'])) {
188 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
191 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
196 $start_row += count($data);
198 if (!empty($cfg['foot'])) {
199 foreach($cfg['foot'] as $row) {
200 foreach($row as $c => $col) {
201 // if it's an array? - formated ???
202 $worksheet->write($start_row, $c, $col);
219 require_once 'File/Convert.php';
220 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
221 $fn = $fc->convert("application/vnd.ms-excel");
222 $fc->serve('attachment',$fn); // can fix IE Mess