2 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 * @package phpMyAdmin-Export-XLS
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['xls'] = array(
17 'text' => __('Excel 97-2003 XLS Workbook'),
19 'mime_type' => 'application/vnd.ms-excel',
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'),
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';
41 * @param string Text of comment
43 * @return bool Whether it suceeded
45 function PMA_exportComment($text) {
50 * Outputs export footer
52 * @return bool Whether it suceeded
56 function PMA_exportFooter() {
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))) {
70 unlink($tmp_filename);
72 unset($GLOBALS['workbook']);
73 unset($GLOBALS['sheet_index']);
79 * Outputs export header
81 * @return bool Whether it suceeded
85 function PMA_exportHeader() {
88 /* Initialize the workbook */
89 $GLOBALS['workbook'] = new PHPExcel();
90 $GLOBALS['sheet_index'] = 0;
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');
102 * Outputs database header
104 * @param string Database name
106 * @return bool Whether it suceeded
110 function PMA_exportDBHeader($db) {
117 * Outputs database footer
119 * @param string Database name
121 * @return bool Whether it suceeded
125 function PMA_exportDBFooter($db) {
130 * Outputs create database database
132 * @param string Database name
134 * @return bool Whether it suceeded
138 function PMA_exportDBCreate($db) {
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
155 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
160 * Get the data from the database using the original query
162 $result = PMA_DBI_fetch_result($sql_query);
163 $row_cnt = count($result);
166 $col_names = array_keys($result[0]);
167 $fields_cnt = count($result[0]);
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]);
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]]);
195 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r +
$row_offset), '');