EXPERIMENTAL native MS Excel export support, using PEAR module Spreadsheet_Excel_Writer (RFE #968110).

This commit is contained in:
Michal Čihař
2004-06-07 14:11:54 +00:00
parent 30c6f6f58b
commit 08db25e5a6
108 changed files with 372 additions and 16 deletions

View File

@@ -871,6 +871,11 @@ if (!isset($cfg['docSQLDir'])) {
$cfg['docSQLDir'] = '';
}
if (!isset($cfg['TempDir'])) {
$cfg['TempDir'] = '';
}
if (!isset($cfg['SQLValidator']['use'])) {
$cfg['SQLValidator']['use'] = FALSE;
}
@@ -1192,9 +1197,13 @@ if (!isset($cfg['CtrlArrowsMoving'])) {
$cfg['CtrlArrowsMoving'] = TRUE;
}
if (!isset($cfg['Export'])) {
$cfg['Export'] = array();
}
if (!isset($cfg['Export']['format'])) {
$cfg['Export']['format'] = 'sql';
} // sql/latex/excel/csv/xml
} // sql/latex/excel/csv/xml/xls
if (!isset($cfg['Export']['compression'])) {
$cfg['Export']['compression'] = 'none';
} // none/zip/gzip/bzip2
@@ -1217,6 +1226,12 @@ if (!isset($cfg['Export']['csv_null'])) {
if (!isset($cfg['Export']['csv_columns'])) {
$cfg['Export']['csv_columns'] = FALSE;
}
if (!isset($cfg['Export']['xls_null'])) {
$cfg['Export']['xls_null'] = 'NULL';
}
if (!isset($cfg['Export']['xls_columns'])) {
$cfg['Export']['xls_columns'] = FALSE;
}
if (!isset($cfg['Export']['excel_null'])) {
$cfg['Export']['excel_null'] = 'NULL';
}

View File

@@ -6,6 +6,18 @@
require_once('./libraries/relation.lib.php');
$cfgRelation = PMA_getRelationsParam();
// Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
if (!empty($GLOBALS['cfg']['TempDir'])) {
include_once('Spreadsheet/Excel/Writer.php');
if (class_exists('Spreadsheet_Excel_Writer')) {
$xls = TRUE;
} else {
$xls = FALSE;
}
} else {
$xls = FALSE;
}
function PMA_exportCheckboxCheck($str) {
if (isset($GLOBALS['cfg']['Export'][$str]) && $GLOBALS['cfg']['Export'][$str]) {
echo ' checked="checked"';
@@ -48,6 +60,9 @@ if (isset($sql_query)) {
getElement("csv_options").style.display = 'none';
getElement("excel_options").style.display = 'none';
getElement("latex_options").style.display = 'none';
<?php if ($xls) { ?>
getElement("xls_options").style.display = 'none';
<?php } ?>
<?php if (!$hide_sql) { ?>
getElement("sql_options").style.display = 'none';
<?php } ?>
@@ -58,6 +73,10 @@ if (isset($sql_query)) {
hide_them_all();
if (getElement('radio_dump_latex').checked) {
getElement('latex_options').style.display = 'block';
<?php if ($xls) { ?>
} else if (getElement('radio_dump_xls').checked) {
getElement('xls_options').style.display = 'block';
<?php } ?>
<?php if (!$hide_sql) { ?>
} else if (getElement('radio_dump_sql').checked) {
getElement('sql_options').style.display = 'block';
@@ -111,6 +130,14 @@ if (isset($sql_query)) {
<label for="radio_dump_latex"><?php echo $strLaTeX; ?></label>
<br /><br />
<?php if ($xls) { ?>
<!-- Native Excel -->
<input type="radio" name="what" value="xls" id="radio_dump_xls" onclick="if (this.checked) { hide_them_all(); getElement('xls_options').style.display = 'block'; getElement('checkbox_dump_asfile').checked = true;}; return true" <?php PMA_exportIsActive('format', 'xls'); ?> />
<label for="radio_dump_xls"><?php echo $strStrucNativeExcel; ?></label>
<br /><br />
<?php } ?>
<!-- Excel CSV -->
<input type="radio" name="what" value="excel" id="radio_dump_excel" onclick="if (this.checked) { hide_them_all(); getElement('excel_options').style.display = 'block'; }; return true" <?php PMA_exportIsActive('format', 'excel'); ?> />
<label for="radio_dump_excel"><?php echo $strStrucExcelCSV; ?></label>
@@ -344,6 +371,31 @@ if ($cfgRelation['mimework']) {
</fieldset>
</fieldset>
<?php if ($xls) { ?>
<!-- Native Excel options -->
<fieldset id="xls_options">
<legend><?php echo $strExcelOptions; ?></legend>
<input type="hidden" name="xls_data" value="xls_data" />
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<?php echo $strReplaceNULLBy; ?>&nbsp;
</td>
<td>
<input type="text" name="xls_replace_null" size="20" value="<?php echo $cfg['Export']['xls_null']; ?>" class="textfield" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="xls_shownames" value="yes" id="checkbox_dump_xls_shownames" <?php PMA_exportCheckboxCheck('xls_columns'); ?> />
<label for="checkbox_dump_xls_shownames"><?php echo $strPutColNames; ?></label>
</td>
</tr>
</table>
</fieldset>
<?php } ?>
<!-- CSV options -->
<fieldset id="csv_options">
<legend><?php echo $strCSVOptions; ?></legend>

150
libraries/export/xls.php Normal file
View File

@@ -0,0 +1,150 @@
<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
require_once('Spreadsheet/Excel/Writer.php');
/**
* Set of functions used to build MS Excel dumps of tables
*/
/**
* Outputs comment
*
* @param string Text of comment
*
* @return bool Whether it suceeded
*/
function PMA_exportComment($text) {
return TRUE;
}
/**
* Outputs export footer
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportFooter() {
global $workbook;
global $tmp_filename;
$workbook->close();
if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) return FALSE;
unlink($tmp_filename);
return TRUE;
}
/**
* Outputs export header
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportHeader() {
global $workbook;
global $tmp_filename;
if (empty($GLOBALS['cfg']['TempDir'])) return FALSE;
$tmp_filename = tempnam($GLOBALS['cfg']['TempDir'], 'pma_xls_');
$workbook = new Spreadsheet_Excel_Writer($tmp_filename);
return TRUE;
}
/**
* Outputs database header
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBHeader($db) {
return TRUE;
}
/**
* Outputs database footer
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBFooter($db) {
return TRUE;
}
/**
* Outputs create database database
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBCreate($db) {
return TRUE;
}
/**
* Outputs the content of a table in CSV format
*
* @param string the database name
* @param string the table name
* @param string the end of line sequence
* @param string the url to go back in case of error
* @param string SQL query for obtaining data
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
global $what;
global $workbook;
$worksheet =& $workbook->addWorksheet($table);
// Gets the data from the database
$result = PMA_DBI_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED);
$fields_cnt = PMA_DBI_num_fields($result);
$col = 0;
// If required, get fields name at the first line
if (isset($GLOBALS['xls_shownames']) && $GLOBALS['xls_shownames'] == 'yes') {
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++) {
$worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
} // end for
$col++;
} // end if
// Format the data
while ($row = PMA_DBI_fetch_row($result)) {
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if (!isset($row[$j]) || is_null($row[$j])) {
$worksheet->write($col, $j, $GLOBALS['xls_replace_null']);
} else if ($row[$j] == '0' || $row[$j] != '') {
// FIXME: we should somehow handle character set here!
$worksheet->write($col, $j, $row[$j]);
} else {
$worksheet->write($col, $j, '');
}
} // end for
$col++;
} // end while
PMA_DBI_free_result($result);
return TRUE;
}
?>