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';
52 // Creating a workbook
53 $outfile2 = $pman->tempName('xls');
54 // var_dump($outfile2);
55 $workbook = new Spreadsheet_Excel_Writer($outfile2);
56 //$workbook = new Spreadsheet_Excel_Writer();
57 $workbook->setVersion(8);
58 // sending HTTP headers
62 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
63 foreach($cfg['formats'] as $f=>$fcfg) {
64 $formats[$f] = & $workbook->addFormat();
65 foreach($fcfg as $k=>$v) {
66 $formats[$f]->{'set' . $k}($v);
72 if (empty($cfg['workbooks'])) {
73 $this->buildpage( $workbook, $formats , $data,$cfg);
75 foreach($cfg['workbooks'] as $i =>$wcfg) {
76 $this->buildpage( $workbook, $formats , $data[$i],$wcfg);
86 $this->outfile2 = $outfile2;
90 function buildpage($workbook, $formats , $data,$cfg)
92 //echo '<PRE>'; print_R($cfg);
94 // Creating a worksheet
95 $worksheet = $workbook->addWorksheet($cfg['workbook']);
96 if (is_a($worksheet, 'PEAR_Error')) {
97 die($worksheet->toString());
99 //print_R($worksheet);
100 $worksheet->setInputEncoding('UTF-8');
106 if (!empty($cfg['head'])) {
107 foreach($cfg['head'] as $row) {
108 foreach($row as $c => $col) {
109 $worksheet->write($start_row, $c, $col);
121 foreach($cfg['cols'] as $c=>$col_cfg) {
122 if (is_string($col_cfg)) {
123 $cfg['cols'][$c] = array(
124 'header' => $col_cfg,
125 'dataIndex' => $col_cfg,
133 foreach($cfg['cols'] as $c=>$col_cfg) {
137 $worksheet->write($start_row, $c, $col_cfg['header']);
138 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
143 // DB_DataObject::debugLevel(1);
144 foreach($data as $r=>$clo) {
146 if (is_object($clo)) {
147 $cl = (array)$clo; // lossless converstion..
150 if (isset($cfg['row_height'])) {
151 $worksheet->setRow($start_row +$r, $cfg['row_height']);
154 foreach($cfg['cols'] as $c=>$col_cfg) {
155 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
156 if (empty($cl[$col_cfg['dataIndex']])) {
159 if (isset($col_cfg['txtrenderer'])) {
160 $v = call_user_func($col_cfg['txtrenderer'],
161 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
167 if (isset($col_cfg['renderer'])) {
172 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
173 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
175 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
176 $worksheet->write($start_row+$r, $c, $v, $format);
179 /// call user render on any that are defined..
181 foreach($data as $r=>$cl) {
183 foreach($cfg['cols'] as $c=>$col_cfg) {
184 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
185 if (empty($cl[$col_cfg['dataIndex']])) {
188 if (isset($col_cfg['renderer'])) {
189 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
192 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
197 $start_row += count($data);
199 if (!empty($cfg['foot'])) {
200 foreach($cfg['foot'] as $row) {
201 foreach($row as $c => $col) {
202 // if it's an array? - formated ???
203 $worksheet->write($start_row, $c, $col);
225 require_once 'File/Convert.php';
226 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
227 $fn = $fc->convert("application/vnd.ms-excel");
228 $fc->serve('attachment',$fn); // can fix IE Mess