Let's again obey TextareaRows (bug #1465906).
[phpmyadmin/crack.git] / libraries / export / xls.php
blob07f47306bbae096578e7fd68f5844930fcd0c89c
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 // Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
6 if (!empty($GLOBALS['cfg']['TempDir'])) {
7 @include_once('Spreadsheet/Excel/Writer.php');
8 if (class_exists('Spreadsheet_Excel_Writer')) {
9 $xls = TRUE;
10 } else {
11 $xls = FALSE;
13 } else {
14 $xls = FALSE;
17 if ($xls) {
19 if (isset($plugin_list)) {
20 $plugin_list['xls'] = array(
21 'text' => 'strStrucNativeExcel',
22 'extension' => 'xls',
23 'mime_type' => 'application/vnd.ms-excel',
24 'force_file' => true,
25 'options' => array(
26 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
27 array('type' => 'text', 'name' => 'columns', 'text' => 'strPutColNames'),
28 array('type' => 'hidden', 'name' => 'data'),
30 'options_text' => 'strStrucNativeExcelOptions',
32 } else {
34 /**
35 * Set of functions used to build MS Excel dumps of tables
38 /**
39 * Outputs comment
41 * @param string Text of comment
43 * @return bool Whether it suceeded
45 function PMA_exportComment($text)
47 return TRUE;
50 /**
51 * Outputs export footer
53 * @return bool Whether it suceeded
55 * @access public
57 function PMA_exportFooter()
59 global $workbook;
60 global $tmp_filename;
62 $res = $workbook->close();
63 if (PEAR::isError($res)) {
64 echo $res->getMessage();
65 return FALSE;
67 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
68 return FALSE;
70 unlink($tmp_filename);
72 return TRUE;
75 /**
76 * Outputs export header
78 * @return bool Whether it suceeded
80 * @access public
82 function PMA_exportHeader()
84 global $workbook;
85 global $tmp_filename;
87 if (empty($GLOBALS['cfg']['TempDir'])) {
88 return FALSE;
90 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
91 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
93 return TRUE;
96 /**
97 * Outputs database header
99 * @param string Database name
101 * @return bool Whether it suceeded
103 * @access public
105 function PMA_exportDBHeader($db)
107 return TRUE;
111 * Outputs database footer
113 * @param string Database name
115 * @return bool Whether it suceeded
117 * @access public
119 function PMA_exportDBFooter($db)
121 return TRUE;
125 * Outputs create database database
127 * @param string Database name
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportDBCreate($db)
135 return TRUE;
139 * Outputs the content of a table in CSV format
141 * @param string the database name
142 * @param string the table name
143 * @param string the end of line sequence
144 * @param string the url to go back in case of error
145 * @param string SQL query for obtaining data
147 * @return bool Whether it suceeded
149 * @access public
151 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
153 global $what;
154 global $workbook;
156 $worksheet =& $workbook->addWorksheet($table);
157 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
159 // Gets the data from the database
160 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
161 $fields_cnt = PMA_DBI_num_fields($result);
162 $col = 0;
164 // If required, get fields name at the first line
165 if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns'] == 'yes') {
166 $schema_insert = '';
167 for ($i = 0; $i < $fields_cnt; $i++) {
168 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
169 } // end for
170 $col++;
171 } // end if
173 // Format the data
174 while ($row = PMA_DBI_fetch_row($result)) {
175 $schema_insert = '';
176 for ($j = 0; $j < $fields_cnt; $j++) {
177 if (!isset($row[$j]) || is_null($row[$j])) {
178 $worksheet->write($col, $j, $GLOBALS['xls_null']);
179 } elseif ($row[$j] == '0' || $row[$j] != '') {
181 * @todo we should somehow handle character set here!
183 $worksheet->write($col, $j, $row[$j]);
184 } else {
185 $worksheet->write($col, $j, '');
187 } // end for
188 $col++;
189 } // end while
190 PMA_DBI_free_result($result);
192 return TRUE;