2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build XLS dumps of tables
12 // Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
13 if (!empty($GLOBALS['cfg']['TempDir'])) {
14 @include_once
'Spreadsheet/Excel/Writer.php';
15 if (class_exists('Spreadsheet_Excel_Writer')) {
26 if (isset($plugin_list)) {
27 $plugin_list['xls'] = array(
28 'text' => 'strStrucNativeExcel',
30 'mime_type' => 'application/vnd.ms-excel',
33 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
34 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
35 array('type' => 'hidden', 'name' => 'data'),
37 'options_text' => 'strOptions',
42 * Set of functions used to build MS Excel dumps of tables
48 * @param string Text of comment
50 * @return bool Whether it suceeded
52 function PMA_exportComment($text)
58 * Outputs export footer
60 * @return bool Whether it suceeded
64 function PMA_exportFooter()
69 $res = $workbook->close();
70 if (PEAR
::isError($res)) {
71 echo $res->getMessage();
74 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
77 unlink($tmp_filename);
83 * Outputs export header
85 * @return bool Whether it suceeded
89 function PMA_exportHeader()
94 if (empty($GLOBALS['cfg']['TempDir'])) {
97 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
98 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
104 * Outputs database header
106 * @param string Database name
108 * @return bool Whether it suceeded
112 function PMA_exportDBHeader($db)
118 * Outputs database footer
120 * @param string Database name
122 * @return bool Whether it suceeded
126 function PMA_exportDBFooter($db)
132 * Outputs create database database
134 * @param string Database name
136 * @return bool Whether it suceeded
140 function PMA_exportDBCreate($db)
146 * Outputs the content of a table in CSV format
148 * @param string the database name
149 * @param string the table name
150 * @param string the end of line sequence
151 * @param string the url to go back in case of error
152 * @param string SQL query for obtaining data
154 * @return bool Whether it suceeded
158 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
163 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
165 // Gets the data from the database
166 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
167 $fields_cnt = PMA_DBI_num_fields($result);
169 $row = PMA_DBI_fetch_row($result);
170 for ($sheetIndex = 0; ; $sheetIndex++
) {
171 // Maximum sheet name length is 31 chars - leave 2 for numeric index
172 $sheetName = substr($table, 0, 29) . ($sheetIndex > 0 ?
$sheetIndex : '');
173 $worksheet =& $workbook->addWorksheet($sheetName);
176 // If required, get fields name at the first line
177 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
178 for ($i = 0; $i < $fields_cnt; $i++
) {
179 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
181 $worksheet->repeatRows($rowIndex);
182 $worksheet->freezePanes(array($rowIndex +
1, 0, $rowIndex +
1, 0));
186 // Format the data (max 65536 rows per worksheet)
187 while ($rowIndex < 65536 && $row) {
189 for ($j = 0; $j < $fields_cnt; $j++
) {
190 if (!isset($row[$j]) ||
is_null($row[$j])) {
191 $worksheet->write($rowIndex, $j, $GLOBALS['xls_null']);
192 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
194 * @todo we should somehow handle character set here!
196 $worksheet->write($rowIndex, $j, $row[$j]);
198 $worksheet->write($rowIndex, $j, '');
202 $row = PMA_DBI_fetch_row($result);
208 PMA_DBI_free_result($result);