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')
31 // if this is set then it will add a tab foreach one.
43 class Pman_Core_SimpleExcel extends Pman
48 function Pman_Core_SimpleExcel($data,$cfg)
50 // print_r($cfg);exit;
51 require_once 'Spreadsheet/Excel/Writer.php';
52 // Creating a workbook
53 $outfile2 = $this->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);
82 $this->outfile2 = $outfile2;
87 static function date($str)
90 return (strtotime($str . ' UTC') + (86400 * 25569)) / 86400;
95 function buildpage($workbook, $formats , $data,$cfg)
97 //echo '<PRE>'; print_R($cfg);
98 // print_r($cfg);exit;
99 // Creating a worksheet
100 $worksheet = $workbook->addWorksheet($cfg['workbook']);
101 if (is_a($worksheet, 'PEAR_Error')) {
102 die($worksheet->toString());
104 //print_R($worksheet);
105 $worksheet->setInputEncoding('UTF-8');
111 if (!empty($cfg['head'])) {
112 foreach($cfg['head'] as $row) {
113 foreach($row as $c => $col) {
114 if (is_array($col)) {
115 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
116 $worksheet->write($start_row, $c, $col[0], $format);
120 $worksheet->write($start_row, $c, $col);
132 foreach($cfg['cols'] as $c=>$col_cfg) {
133 if (is_string($col_cfg)) {
134 $cfg['cols'][$c] = array(
135 'header' => $col_cfg,
136 'dataIndex' => $col_cfg,
144 foreach($cfg['cols'] as $c=>$col_cfg) {
145 $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
146 $worksheet->write($start_row, $c, $col_cfg['header'],$format);
147 $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
152 // DB_DataObject::debugLevel(1);
153 foreach($data as $r=>$clo) {
155 if (is_object($clo)) {
156 $cl = (array)$clo; // lossless converstion..
159 if (isset($cfg['row_height'])) {
160 $worksheet->setRow($start_row +$r, $cfg['row_height']);
163 foreach($cfg['cols'] as $c=>$col_cfg) {
164 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
165 if (empty($cl[$col_cfg['dataIndex']])) {
166 if(isset($data['fillBlank'])){
167 $worksheet->write($start_row+$r, $c, '', $formats[$data['fillBlank']]);
171 if (isset($col_cfg['txtrenderer'])) {
172 $v = call_user_func($col_cfg['txtrenderer'],
173 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
179 if (isset($col_cfg['renderer'])) {
184 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
186 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
188 $worksheet->write($start_row+$r, $c, $v, $format);
191 /// call user render on any that are defined..
193 foreach($data as $r=>$cl) {
195 foreach($cfg['cols'] as $c=>$col_cfg) {
196 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
197 if (empty($cl[$col_cfg['dataIndex']])) {
200 if (isset($col_cfg['renderer'])) {
201 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
204 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
209 $start_row += count($data);
211 if (!empty($cfg['foot'])) {
212 foreach($cfg['foot'] as $row) {
213 foreach($row as $c => $col) {
214 // if it's an array? - formated ???
215 if (is_array($col)) {
216 $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
217 $worksheet->write($start_row, $c, $col[0], $format);
220 $worksheet->write($start_row, $c, $col);
237 require_once 'File/Convert.php';
238 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
239 $fn = $fc->convert("application/vnd.ms-excel");
240 $fc->serve('attachment',$fn); // can fix IE Mess