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 if (is_array($col)) {
109 $worksheet->write($start_row, $c, $col[0], $col[1]);
113 $worksheet->write($start_row, $c, $col);
125 foreach($cfg['cols'] as $c=>$col_cfg) {
126 if (is_string($col_cfg)) {
127 $cfg['cols'][$c] = array(
128 'header' => $col_cfg,
129 'dataIndex' => $col_cfg,
137 foreach($cfg['cols'] as $c=>$col_cfg) {
141 $worksheet->write($start_row, $c, $col_cfg['header']);
142 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
147 // DB_DataObject::debugLevel(1);
148 foreach($data as $r=>$clo) {
150 if (is_object($clo)) {
151 $cl = (array)$clo; // lossless converstion..
154 if (isset($cfg['row_height'])) {
155 $worksheet->setRow($start_row +$r, $cfg['row_height']);
158 foreach($cfg['cols'] as $c=>$col_cfg) {
159 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
160 if (empty($cl[$col_cfg['dataIndex']])) {
163 if (isset($col_cfg['txtrenderer'])) {
164 $v = call_user_func($col_cfg['txtrenderer'],
165 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
171 if (isset($col_cfg['renderer'])) {
176 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
177 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
179 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
180 $worksheet->write($start_row+$r, $c, $v, $format);
183 /// call user render on any that are defined..
185 foreach($data as $r=>$cl) {
187 foreach($cfg['cols'] as $c=>$col_cfg) {
188 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
189 if (empty($cl[$col_cfg['dataIndex']])) {
192 if (isset($col_cfg['renderer'])) {
193 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
196 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
201 $start_row += count($data);
203 if (!empty($cfg['foot'])) {
204 foreach($cfg['foot'] as $row) {
205 foreach($row as $c => $col) {
206 // if it's an array? - formated ???
207 if (is_array($col)) {
208 $worksheet->write($start_row, $c, $col[0], $col[1]);
211 $worksheet->write($start_row, $c, $col);
228 require_once 'File/Convert.php';
229 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
230 $fn = $fc->convert("application/vnd.ms-excel");
231 $fc->serve('attachment',$fn); // can fix IE Mess