4 * class to generate excel file from rows of data, and a configuration.
8 * name : [ Align : left, .... ]
12 'header'=> "Thumbnail",
15 'renderer' => array($this, 'getThumb')
23 class Pman_Core_SimpleExcel extends Pman
28 function Pman_Core_SimpleExcel($data,$cfg)
31 require_once 'Spreadsheet/Excel/Writer.php';
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
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);
54 // Creating a worksheet
55 $worksheet =& $workbook->addWorksheet($cfg['workbook']);
56 $worksheet->setInputEncoding('UTF-8');
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);
63 // DB_DataObject::debugLevel(1);
64 foreach($data as $r=>$cl) {
66 if (isset($cfg['row_height'])) {
67 $worksheet->setRow($r+1, $cfg['row_height']);
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']])) {
75 if (isset($col_cfg['txtrenderer'])) {
76 $v = call_user_func($col_cfg['txtrenderer'],
77 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
83 if (isset($col_cfg['renderer'])) {
87 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
88 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
90 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
91 $worksheet->write($r+1, $c, $v, $format);
95 foreach($data as $r=>$cl) {
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']])) {
102 if (isset($col_cfg['renderer'])) {
103 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
106 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
111 $this->outfile2 = $outfile2;
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