3.2.5
[phpmyadmin/dkf.git] / libraries / export / xls.php
blobb450d6f2acb64c513668870158863a99b686fc99
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
6 * @package phpMyAdmin-Export-XLS
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['xls'] = array(
18 'text' => 'strImportXLS',
19 'extension' => 'xls',
20 'mime_type' => 'application/vnd.ms-excel',
21 'force_file' => true,
22 'options' => array(
23 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
24 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
25 array('type' => 'hidden', 'name' => 'data'),
27 'options_text' => 'strOptions',
29 } else {
31 /* Append the PHPExcel directory to the include path variable */
32 set_include_path(get_include_path() . PATH_SEPARATOR . getcwd() . '/libraries/PHPExcel/');
34 require_once './libraries/PHPExcel/PHPExcel.php';
35 require_once './libraries/PHPExcel/PHPExcel/Writer/Excel5.php';
37 /**
38 * Outputs comment
40 * @param string Text of comment
42 * @return bool Whether it suceeded
44 function PMA_exportComment($text) {
45 return TRUE;
48 /**
49 * Outputs export footer
51 * @return bool Whether it suceeded
53 * @access public
55 function PMA_exportFooter() {
56 global $workbook;
57 global $tmp_filename;
59 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
61 $workbookWriter = new PHPExcel_Writer_Excel5($workbook);
62 $workbookWriter->save($tmp_filename);
64 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
65 return FALSE;
68 unlink($tmp_filename);
70 unset($GLOBALS['workbook']);
71 unset($GLOBALS['sheet_index']);
73 return TRUE;
76 /**
77 * Outputs export header
79 * @return bool Whether it suceeded
81 * @access public
83 function PMA_exportHeader() {
84 global $db;
86 /* Initialize the workbook */
87 $GLOBALS['workbook'] = new PHPExcel();
88 $GLOBALS['sheet_index'] = 0;
89 global $workbook;
91 $workbook->getProperties()->setCreator('phpMyAdmin ' . PMA_VERSION);
92 $workbook->getProperties()->setLastModifiedBy('phpMyAdmin ' . PMA_VERSION);
93 $workbook->getProperties()->setTitle($db);
94 $workbook->getProperties()->setSubject('phpMyAdmin ' . PMA_VERSION . ' XLS Dump');
96 return TRUE;
99 /**
100 * Outputs database header
102 * @param string Database name
104 * @return bool Whether it suceeded
106 * @access public
108 function PMA_exportDBHeader($db) {
111 return TRUE;
115 * Outputs database footer
117 * @param string Database name
119 * @return bool Whether it suceeded
121 * @access public
123 function PMA_exportDBFooter($db) {
124 return TRUE;
128 * Outputs create database database
130 * @param string Database name
132 * @return bool Whether it suceeded
134 * @access public
136 function PMA_exportDBCreate($db) {
137 return TRUE;
141 * Outputs the content of a table in XLS format
143 * @param string the database name
144 * @param string the table name
145 * @param string the end of line sequence
146 * @param string the url to go back in case of error
147 * @param string SQL query for obtaining data
149 * @return bool Whether it suceeded
151 * @access public
153 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
154 global $workbook;
155 global $sheet_index;
158 * Get the data from the database using the original query
160 $result = PMA_DBI_fetch_result($sql_query);
161 $row_cnt = count($result);
163 if ($row_cnt > 0) {
164 $col_names = array_keys($result[0]);
165 $fields_cnt = count($result[0]);
166 $row_offset = 1;
168 /* Only one sheet is created on workbook initialization */
169 if ($sheet_index > 0) {
170 $workbook->createSheet();
173 $workbook->setActiveSheetIndex($sheet_index);
174 $workbook->getActiveSheet()->setTitle(substr($table, 0, 31));
176 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
177 for ($i = 0; $i < $fields_cnt; ++$i) {
178 $workbook->getActiveSheet()->setCellValueByColumnAndRow($i, $row_offset, $col_names[$i]);
180 $row_offset++;
183 for ($r = 0; ($r < 65536) && ($r < $row_cnt); ++$r) {
184 for ($c = 0; $c < $fields_cnt; ++$c) {
185 if (!isset($result[$r][$col_names[$c]]) || is_null($result[$r][$col_names[$c]])) {
186 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $GLOBALS['xls_null']);
187 } elseif ($result[$r][$col_names[$c]] == '0' || $result[$r][$col_names[$c]] != '') {
189 * @todo we should somehow handle character set here!
191 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $result[$r][$col_names[$c]]);
192 } else {
193 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), '');
198 $sheet_index++;
201 return TRUE;