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=>$cl) {
106 if (is_object($cl)) {
107 $cl = $cl->toArray();
110 if (isset($cfg['row_height'])) {
111 $worksheet->setRow($start_row +$r, $cfg['row_height']);
114 foreach($cfg['cols'] as $c=>$col_cfg) {
115 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
116 if (empty($cl[$col_cfg['dataIndex']])) {
119 if (isset($col_cfg['txtrenderer'])) {
120 $v = call_user_func($col_cfg['txtrenderer'],
121 $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
127 if (isset($col_cfg['renderer'])) {
132 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
133 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
135 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
136 $worksheet->write($start_row+$r, $c, $v, $format);
139 /// call user render on any that are defined..
141 foreach($data as $r=>$cl) {
143 foreach($cfg['cols'] as $c=>$col_cfg) {
144 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
145 if (empty($cl[$col_cfg['dataIndex']])) {
148 if (isset($col_cfg['renderer'])) {
149 call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
152 // echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
157 $start_row += count($data);
159 if (!empty($cfg['foot'])) {
160 foreach($cfg['foot'] as $row) {
161 foreach($row as $c => $col) {
162 // if it's an array? - formated ???
163 $worksheet->write($start_row, $c, $col);
175 $this->outfile2 = $outfile2;
184 require_once 'File/Convert.php';
185 $fc= new File_Convert($this->outfile2, "application/vnd.ms-excel");
186 $fn = $fc->convert("application/vnd.ms-excel");
187 $fc->serve('attachment',$fn); // can fix IE Mess