**/ class twIf_Convert_Excel extends twIf_Object_Access { const MIME_TYPE = 'application/vnd.ms-excel'; const SUFFIX = 'xls'; // {{{ export /** * @param array $Handles * @param array $Fields * @param string $WorksheetName (optional) * @param object $Workbook (optional) * * @access public * @return string **/ public static function export ($Handles, $Fields, $WorksheetName = null, $Workbook = null, $Finalize = null) { // Setup the export-destination if (!is_object ($Workbook)) { if (isset ($_SERVER ['TMPDIR'])) $tmpDir = $_SERVER ['TMPDIR']; elseif (isset ($_ENV ['TMPDIR'])) $tmpDir = $_ENV ['TMPDIR']; elseif (is_writeable ('/tmp')) $tmpDir = '/tmp'; else return false; $fn = tempnam ($tmpDir, 'twIfConvertExcelExport'); $Workbook = new Spreadsheet_Excel_Writer ($fn); $Finalize = true; } elseif ($Finalize === null) $Finalize = false; if ($WorksheetName === null) { $WorksheetName = 'twIf_Convert_Excel'; foreach ($Handles as $Handle) if (is_object ($Handle)) { $WorksheetName .= ' / ' . get_class ($Handle); break; } } $Worksheet = $Workbook->addWorksheet ($WorksheetName); $formatHeader = $Workbook->addFormat (); $formatHeader->setBold (); $formatHeader->setBottom (1); // Retrive the first handle foreach ($Handles as $Handle) if (is_object ($Handle)) break; // Generate the Excel-Header $Column = 0; $ColumnFormats = array (); foreach ($Fields as $ID=>$Field) { $Fields [$ID]['def'] = $Definition = parent::parseFieldDef ($ID, $Field, $Handle, 0, true); switch ($Definition ['Type']) { case twIf_Widget_Control::TYPE_DATE: case twIf_Widget_Control::TYPE_DATE_FUTURE: case twIf_Widget_Control::TYPE_DATE_YEARMONTH: case twIf_Widget_Control::TYPE_DATE_YEARMONTH_FUTURE: $ColumnFormats [$Column] = $formatDate = $Workbook->addFormat (); $formatDate->setNumFormat ('D-MMM-YY'); break; # case twIf_Widget_Control:: default: $ColumnFormats [$Column] = null; } $Worksheet->write (0, $Column++, utf8_decode (isset ($Definition ['Caption']) ? $Definition ['Caption'] : $ID), $formatHeader); } // Output the handles $Row = 1; foreach ($Handles as $Handle) { $Column = 0; // Output the values foreach ($Fields as $Field) { if ($ColumnFormats [$Column] == $formatDate) $Worksheet->write ($Row, $Column, number_format ((parent::formatNative ($Handle, $Field ['def']) + 2209165200) / 86400, 5, '.', ''), $ColumnFormats [$Column]); else $Worksheet->write ($Row, $Column, utf8_decode (parent::getFieldValue ($Handle, $Field ['def'])), $ColumnFormats [$Column]); $Column++; } $Row++; } // Finalize the Workbook if (!$Finalize) return true; $Workbook->close (); // Check wheter the file is there if (!is_file ($Workbook->_filename)) return false; // Try to open it if (!is_resource ($f = fopen ($Workbook->_filename, 'r'))) return false; $buf = fread ($f, filesize ($Workbook->_filename)); fclose ($f); return $buf; } // }}} } ?>