Merge remote branch 'origin/master' into mlewandow-branch01
[phpmyadmin/mlewandow.git] / libraries / export / xls.php
blob599025d63b4284358b0b3b7074f5d1f9eae55890
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
6 * @package phpMyAdmin-Export-XLS
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['xls'] = array(
17 'text' => __('Excel 97-2003 XLS Workbook'),
18 'extension' => 'xls',
19 'mime_type' => 'application/vnd.ms-excel',
20 'force_file' => true,
21 'options' => array(
22 array('type' => 'begin_group', 'name' => 'general_opts'),
23 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
24 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
25 array('type' => 'hidden', 'name' => 'structure_or_data'),
26 array('type' => 'end_group')
28 'options_text' => __('Options'),
30 } else {
32 /* Append the PHPExcel directory to the include path variable */
33 set_include_path(get_include_path() . PATH_SEPARATOR . getcwd() . '/libraries/PHPExcel/');
35 require_once './libraries/PHPExcel/PHPExcel.php';
36 require_once './libraries/PHPExcel/PHPExcel/Writer/Excel5.php';
38 /**
39 * Outputs comment
41 * @param string Text of comment
43 * @return bool Whether it suceeded
45 function PMA_exportComment($text) {
46 return TRUE;
49 /**
50 * Outputs export footer
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportFooter() {
57 global $workbook;
58 global $tmp_filename;
60 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
62 $workbookWriter = new PHPExcel_Writer_Excel5($workbook);
63 $workbookWriter->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
64 $workbookWriter->save($tmp_filename);
66 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
67 return FALSE;
70 unlink($tmp_filename);
72 unset($GLOBALS['workbook']);
73 unset($GLOBALS['sheet_index']);
75 return TRUE;
78 /**
79 * Outputs export header
81 * @return bool Whether it suceeded
83 * @access public
85 function PMA_exportHeader() {
86 global $db;
88 /* Initialize the workbook */
89 $GLOBALS['workbook'] = new PHPExcel();
90 $GLOBALS['sheet_index'] = 0;
91 global $workbook;
93 $workbook->getProperties()->setCreator('phpMyAdmin ' . PMA_VERSION);
94 $workbook->getProperties()->setLastModifiedBy('phpMyAdmin ' . PMA_VERSION);
95 $workbook->getProperties()->setTitle($db);
96 $workbook->getProperties()->setSubject('phpMyAdmin ' . PMA_VERSION . ' XLS Dump');
98 return TRUE;
102 * Outputs database header
104 * @param string Database name
106 * @return bool Whether it suceeded
108 * @access public
110 function PMA_exportDBHeader($db) {
113 return TRUE;
117 * Outputs database footer
119 * @param string Database name
121 * @return bool Whether it suceeded
123 * @access public
125 function PMA_exportDBFooter($db) {
126 return TRUE;
130 * Outputs create database database
132 * @param string Database name
134 * @return bool Whether it suceeded
136 * @access public
138 function PMA_exportDBCreate($db) {
139 return TRUE;
143 * Outputs the content of a table in XLS format
145 * @param string the database name
146 * @param string the table name
147 * @param string the end of line sequence
148 * @param string the url to go back in case of error
149 * @param string SQL query for obtaining data
151 * @return bool Whether it suceeded
153 * @access public
155 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
156 global $workbook;
157 global $sheet_index;
160 * Get the data from the database using the original query
162 $result = PMA_DBI_fetch_result($sql_query);
163 $row_cnt = count($result);
165 if ($row_cnt > 0) {
166 $col_names = array_keys($result[0]);
167 $fields_cnt = count($result[0]);
168 $row_offset = 1;
170 /* Only one sheet is created on workbook initialization */
171 if ($sheet_index > 0) {
172 $workbook->createSheet();
175 $workbook->setActiveSheetIndex($sheet_index);
176 $workbook->getActiveSheet()->setTitle(substr($table, 0, 31));
178 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
179 for ($i = 0; $i < $fields_cnt; ++$i) {
180 $workbook->getActiveSheet()->setCellValueByColumnAndRow($i, $row_offset, $col_names[$i]);
182 $row_offset++;
185 for ($r = 0; ($r < 65536) && ($r < $row_cnt); ++$r) {
186 for ($c = 0; $c < $fields_cnt; ++$c) {
187 if (!isset($result[$r][$col_names[$c]]) || is_null($result[$r][$col_names[$c]])) {
188 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $GLOBALS['xls_null']);
189 } elseif ($result[$r][$col_names[$c]] == '0' || $result[$r][$col_names[$c]] != '') {
191 * @todo we should somehow handle character set here!
193 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $result[$r][$col_names[$c]]);
194 } else {
195 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), '');
200 $sheet_index++;
203 return TRUE;