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')
39 class Pman_Core_SimpleExcel extends Pman
44 function Pman_Core_SimpleExcel($data,$cfg)
47 require_once 'Spreadsheet/Excel/Writer.php';
49 // Creating a workbook
50 $outfile2 = $pman->tempName('xls');
51 // var_dump($outfile2);
52 $workbook = new Spreadsheet_Excel_Writer($outfile2);
53 //$workbook = new Spreadsheet_Excel_Writer();
54 $workbook->setVersion(8);
55 // sending HTTP headers
59 $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
60 foreach($cfg['formats'] as $f=>$fcfg) {
61 $formats[$f] = & $workbook->addFormat();
62 foreach($fcfg as $k=>$v) {
63 $formats[$f]->{'set' . $k}($v);
70 // Creating a worksheet
71 $worksheet =& $workbook->addWorksheet($cfg['workbook']);
72 $worksheet->setInputEncoding('UTF-8');
78 if (!empty($cfg['head'])) {
79 foreach($cfg['head'] as $row) {
80 foreach($row as $c => $col) {
81 $worksheet->write($start_row, $c, $col);
97 foreach($cfg['cols'] as $c=>$col_cfg) {
98 $worksheet->write($start_row, $c, $col_cfg['header']);
99 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
104 // DB_DataObject::debugLevel(1);
105 foreach($data as $r=>$clo) {
107 if (is_object($clo)) {
108 $cl = (array)$clo; // lossless converstion..
111 if (isset($cfg['row_height'])) {
112 $worksheet->setRow($start_row +$r, $cfg['row_height']);
115 foreach($cfg['cols'] as $c=>$col_cfg) {
116 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
117 if (empty($cl[$col_cfg['dataIndex']])) {
120 if (isset($col_cfg['txtrenderer'])) {
121 $v = call_user_func($col_cfg['txtrenderer'],
122 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
128 if (isset($col_cfg['renderer'])) {
133 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
134 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
136 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
137 $worksheet->write($start_row+$r, $c, $v, $format);
140 /// call user render on any that are defined..
142 foreach($data as $r=>$cl) {
144 foreach($cfg['cols'] as $c=>$col_cfg) {
145 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
146 if (empty($cl[$col_cfg['dataIndex']])) {
149 if (isset($col_cfg['renderer'])) {
150 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
153 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
158 $start_row += count($data);
160 if (!empty($cfg['foot'])) {
161 foreach($cfg['foot'] as $row) {
162 foreach($row as $c => $col) {
163 // if it's an array? - formated ???
164 $worksheet->write($start_row, $c, $col);
176 $this->outfile2 = $outfile2;
185 require_once 'File/Convert.php';
186 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
187 $fn = $fc->convert("application/vnd.ms-excel");
188 $fc->serve('attachment',$fn); // can fix IE Mess