Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / export / xlsx.php
blob89c52885704266f57a64a6ecbabdd788f975af17
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
6 * @package phpMyAdmin-Export-XLSX
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['xlsx'] = array(
17 'text' => __('Excel 2007 XLSX Workbook'),
18 'extension' => 'xlsx',
19 'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
20 'force_file' => true,
21 'options' => array(
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'),
30 } else {
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/Excel2007.php';
38 /**
39 * Outputs comment
41 * @param string Text of comment
43 * @return bool Whether it suceeded
45 function PMA_exportComment($text) {
46 return TRUE;
49 /**
50 * Outputs export footer
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportFooter() {
57 global $workbook;
58 global $tmp_filename;
60 $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xlsx_');
62 $workbookWriter = new PHPExcel_Writer_Excel2007($workbook);
63 $workbookWriter->save($tmp_filename);
65 if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
66 return FALSE;
69 unlink($tmp_filename);
71 unset($GLOBALS['workbook']);
72 unset($GLOBALS['sheet_index']);
74 return TRUE;
77 /**
78 * Outputs export header
80 * @return bool Whether it suceeded
82 * @access public
84 function PMA_exportHeader() {
85 global $db;
87 /* Initialize the workbook */
88 $GLOBALS['workbook'] = new PHPExcel();
89 $GLOBALS['sheet_index'] = 0;
90 global $workbook;
92 $workbook->getProperties()->setCreator('phpMyAdmin ' . PMA_VERSION);
93 $workbook->getProperties()->setLastModifiedBy('phpMyAdmin ' . PMA_VERSION);
94 $workbook->getProperties()->setTitle($db);
95 $workbook->getProperties()->setSubject('phpMyAdmin ' . PMA_VERSION . ' XLSX Dump');
97 return TRUE;
101 * Outputs database header
103 * @param string Database name
105 * @return bool Whether it suceeded
107 * @access public
109 function PMA_exportDBHeader($db) {
112 return TRUE;
116 * Outputs database footer
118 * @param string Database name
120 * @return bool Whether it suceeded
122 * @access public
124 function PMA_exportDBFooter($db) {
125 return TRUE;
129 * Outputs create database database
131 * @param string Database name
133 * @return bool Whether it suceeded
135 * @access public
137 function PMA_exportDBCreate($db) {
138 return TRUE;
142 * Outputs the content of a table in XLSX format
144 * @param string the database name
145 * @param string the table name
146 * @param string the end of line sequence
147 * @param string the url to go back in case of error
148 * @param string SQL query for obtaining data
150 * @return bool Whether it suceeded
152 * @access public
154 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
155 global $workbook;
156 global $sheet_index;
159 * Get the data from the database using the original query
161 $result = PMA_DBI_fetch_result($sql_query);
162 $row_cnt = count($result);
164 if ($row_cnt > 0) {
165 $col_names = array_keys($result[0]);
166 $fields_cnt = count($result[0]);
167 $row_offset = 1;
169 /* Only one sheet is created on workbook initialization */
170 if ($sheet_index > 0) {
171 $workbook->createSheet();
174 $workbook->setActiveSheetIndex($sheet_index);
175 $workbook->getActiveSheet()->setTitle(substr($table, 0, 31));
177 if (isset($GLOBALS['xlsx_columns']) && $GLOBALS['xlsx_columns']) {
178 for ($i = 0; $i < $fields_cnt; ++$i) {
179 $workbook->getActiveSheet()->setCellValueByColumnAndRow($i, $row_offset, $col_names[$i]);
181 $row_offset++;
184 for ($r = 0; ($r < 65536) && ($r < $row_cnt); ++$r) {
185 for ($c = 0; $c < $fields_cnt; ++$c) {
186 if (!isset($result[$r][$col_names[$c]]) || is_null($result[$r][$col_names[$c]])) {
187 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $GLOBALS['xlsx_null']);
188 } elseif ($result[$r][$col_names[$c]] == '0' || $result[$r][$col_names[$c]] != '') {
190 * @todo we should somehow handle character set here!
192 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), $result[$r][$col_names[$c]]);
193 } else {
194 $workbook->getActiveSheet()->setCellValueByColumnAndRow($c, ($r + $row_offset), '');
199 $sheet_index++;
202 return TRUE;