2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build XLS dumps of tables
8 if (! defined('PHPMYADMIN')) {
15 // Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
16 if (!empty($GLOBALS['cfg']['TempDir'])) {
17 @include_once
'Spreadsheet/Excel/Writer.php';
18 if (class_exists('Spreadsheet_Excel_Writer')) {
29 if (isset($plugin_list)) {
30 $plugin_list['xls'] = array(
31 'text' => 'strStrucNativeExcel',
33 'mime_type' => 'application/vnd.ms-excel',
36 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
37 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
38 array('type' => 'hidden', 'name' => 'data'),
40 'options_text' => 'strOptions',
45 * Set of functions used to build MS Excel dumps of tables
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text)
61 * Outputs export footer
63 * @return bool Whether it suceeded
67 function PMA_exportFooter()
72 $res = $workbook->close();
73 if (PEAR
::isError($res)) {
74 echo $res->getMessage();
77 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
80 unlink($tmp_filename);
86 * Outputs export header
88 * @return bool Whether it suceeded
92 function PMA_exportHeader()
97 if (empty($GLOBALS['cfg']['TempDir'])) {
100 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
101 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
107 * Outputs database header
109 * @param string Database name
111 * @return bool Whether it suceeded
115 function PMA_exportDBHeader($db)
121 * Outputs database footer
123 * @param string Database name
125 * @return bool Whether it suceeded
129 function PMA_exportDBFooter($db)
135 * Outputs create database database
137 * @param string Database name
139 * @return bool Whether it suceeded
143 function PMA_exportDBCreate($db)
149 * Outputs the content of a table in CSV format
151 * @param string the database name
152 * @param string the table name
153 * @param string the end of line sequence
154 * @param string the url to go back in case of error
155 * @param string SQL query for obtaining data
157 * @return bool Whether it suceeded
161 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
166 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
168 // Gets the data from the database
169 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
170 $fields_cnt = PMA_DBI_num_fields($result);
172 $row = PMA_DBI_fetch_row($result);
173 for ($sheetIndex = 0; ; $sheetIndex++
) {
174 // Maximum sheet name length is 31 chars - leave 2 for numeric index
175 $sheetName = substr($table, 0, 29) . ($sheetIndex > 0 ?
$sheetIndex : '');
176 $worksheet =& $workbook->addWorksheet($sheetName);
179 // If required, get fields name at the first line
180 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
181 for ($i = 0; $i < $fields_cnt; $i++
) {
182 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
184 $worksheet->repeatRows($rowIndex);
185 $worksheet->freezePanes(array($rowIndex +
1, 0, $rowIndex +
1, 0));
189 // Format the data (max 65536 rows per worksheet)
190 while ($rowIndex < 65536 && $row) {
192 for ($j = 0; $j < $fields_cnt; $j++
) {
193 if (!isset($row[$j]) ||
is_null($row[$j])) {
194 $worksheet->write($rowIndex, $j, $GLOBALS['xls_null']);
195 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
197 * @todo we should somehow handle character set here!
199 $worksheet->write($rowIndex, $j, $row[$j]);
201 $worksheet->write($rowIndex, $j, '');
205 $row = PMA_DBI_fetch_row($result);
211 PMA_DBI_free_result($result);