init
[Pman.Core] / SimpleExcel.php
1 <?php
2
3 /**
4  * class to generate excel file from rows of data, and a configuration.
5  * 
6  * cfg:
7  *     formats 
8  *          name : [ Align : left, .... ]
9  *     workbook : nameof 
10  *     close :  array(
11             array(
12                 'header'=> "Thumbnail",
13                 'dataIndex'=> 'id',
14                 'width'=>  75,
15                 'renderer' => array($this, 'getThumb')
16             ),
17  */
18  
19  
20  
21
22
23 class Pman_Core_SimpleExcel extends Pman
24 {
25     
26     
27     
28     function Pman_Core_SimpleExcel($data,$cfg)
29     {
30      //  print_r($cfg);
31         require_once 'Spreadsheet/Excel/Writer.php';
32         $pman = new Pman();
33         // Creating a workbook
34         $outfile2 = $pman->tempName('xls');
35        // var_dump($outfile2);
36         $workbook = new Spreadsheet_Excel_Writer($outfile2);
37         //$workbook = new Spreadsheet_Excel_Writer();
38         $workbook->setVersion(8);
39         // sending HTTP headers
40         
41         $formats = array();
42        
43         $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
44         foreach($cfg['formats'] as $f=>$fcfg) {
45             $formats[$f] = & $workbook->addFormat();
46             foreach($fcfg as $k=>$v) {
47                 $formats[$f]->{'set' . $k}($v);
48             }
49              
50         }
51          
52
53         
54         // Creating a worksheet
55         $worksheet =& $workbook->addWorksheet($cfg['workbook']);
56         $worksheet->setInputEncoding('UTF-8'); 
57          
58         foreach($cfg['cols'] as $c=>$col_cfg) {
59             $worksheet->write(0, $c, $col_cfg['header']);
60             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
61              
62         }
63            //     DB_DataObject::debugLevel(1);
64         foreach($data as $r=>$cl) {
65             
66             if (isset($cfg['row_height'])) {
67                 $worksheet->setRow($r+1, $cfg['row_height']);
68                }
69             
70             foreach($cfg['cols']  as $c=>$col_cfg) {
71                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
72                 if (empty($cl[$col_cfg['dataIndex']])) {
73                     continue;
74                 }
75                 if (isset($col_cfg['txtrenderer'])) {
76                     $v = call_user_func($col_cfg['txtrenderer'], 
77                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
78                     if ($v === false) {
79                         continue;
80                     }
81                   //  var_dump($v);
82                 }
83                 if (isset($col_cfg['renderer'])) {
84                     continue;
85                 }
86                 
87                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
88                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
89                 
90           //    echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
91                 $worksheet->write($r+1, $c, $v, $format);
92             }
93         }
94         
95         foreach($data as $r=>$cl) {
96         
97             foreach($cfg['cols']   as $c=>$col_cfg) {
98                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
99                 if (empty($cl[$col_cfg['dataIndex']])) {
100                     continue;
101                 }
102                 if (isset($col_cfg['renderer'])) {
103                     call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
104                     
105                 }
106               //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
107          
108             }
109         }
110            $workbook->close();
111         $this->outfile2 = $outfile2;
112          
113     }
114     
115     function send($fn)
116     {
117         
118      
119        
120         require_once 'File/Convert.php';
121         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
122         $fn = $fc->convert("application/vnd.ms-excel"); 
123         $fc->serve('attachment',$fn); // can fix IE Mess
124     }
125      
126     
127 }