sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / export / xls.php
blob0a3f1c65fac07ed79f83b2859611a4a72c5de209
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once('Spreadsheet/Excel/Writer.php');
7 /**
8 * Set of functions used to build MS Excel dumps of tables
9 */
11 /**
12 * Outputs comment
14 * @param string Text of comment
16 * @return bool Whether it suceeded
18 function PMA_exportComment($text) {
19 return TRUE;
22 /**
23 * Outputs export footer
25 * @return bool Whether it suceeded
27 * @access public
29 function PMA_exportFooter() {
30 global $workbook;
31 global $tmp_filename;
33 $res = $workbook->close();
34 if (PEAR::isError($res)) {
35 echo $res->getMessage();
36 return FALSE;
38 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) return FALSE;
39 unlink($tmp_filename);
41 return TRUE;
44 /**
45 * Outputs export header
47 * @return bool Whether it suceeded
49 * @access public
51 function PMA_exportHeader() {
52 global $workbook;
53 global $tmp_filename;
55 if (empty($GLOBALS['cfg']['TempDir'])) return FALSE;
56 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
57 $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
59 return TRUE;
62 /**
63 * Outputs database header
65 * @param string Database name
67 * @return bool Whether it suceeded
69 * @access public
71 function PMA_exportDBHeader($db) {
72 return TRUE;
75 /**
76 * Outputs database footer
78 * @param string Database name
80 * @return bool Whether it suceeded
82 * @access public
84 function PMA_exportDBFooter($db) {
85 return TRUE;
88 /**
89 * Outputs create database database
91 * @param string Database name
93 * @return bool Whether it suceeded
95 * @access public
97 function PMA_exportDBCreate($db) {
98 return TRUE;
102 * Outputs the content of a table in CSV format
104 * @param string the database name
105 * @param string the table name
106 * @param string the end of line sequence
107 * @param string the url to go back in case of error
108 * @param string SQL query for obtaining data
110 * @return bool Whether it suceeded
112 * @access public
114 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
115 global $what;
116 global $workbook;
118 $worksheet =& $workbook->addWorksheet($table);
119 $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
121 // Gets the data from the database
122 $result = PMA_DBI_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED);
123 $fields_cnt = PMA_DBI_num_fields($result);
124 $col = 0;
126 // If required, get fields name at the first line
127 if (isset($GLOBALS['xls_shownames']) && $GLOBALS['xls_shownames'] == 'yes') {
128 $schema_insert = '';
129 for ($i = 0; $i < $fields_cnt; $i++) {
130 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
131 } // end for
132 $col++;
133 } // end if
135 // Format the data
136 while ($row = PMA_DBI_fetch_row($result)) {
137 $schema_insert = '';
138 for ($j = 0; $j < $fields_cnt; $j++) {
139 if (!isset($row[$j]) || is_null($row[$j])) {
140 $worksheet->write($col, $j, $GLOBALS['xls_replace_null']);
141 } else if ($row[$j] == '0' || $row[$j] != '') {
142 // FIXME: we should somehow handle character set here!
143 $worksheet->write($col, $j, $row[$j]);
144 } else {
145 $worksheet->write($col, $j, '');
147 } // end for
148 $col++;
149 } // end while
150 PMA_DBI_free_result($result);
152 return TRUE;