home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / export / xls.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  5.2 KB  |  219 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Set of functions used to build XLS dumps of tables
  5.  *
  6.  * @version $Id: xls.php 11326 2008-06-17 21:32:48Z lem9 $
  7.  */
  8. if (! defined('PHPMYADMIN')) {
  9.     exit;
  10. }
  11.  
  12. /**
  13.  *
  14.  */
  15. // Check if we have native MS Excel export using PEAR class Spreadsheet_Excel_Writer
  16. if (!empty($GLOBALS['cfg']['TempDir'])) {
  17.     @include_once 'Spreadsheet/Excel/Writer.php';
  18.     if (class_exists('Spreadsheet_Excel_Writer')) {
  19.         $xls = TRUE;
  20.     } else {
  21.         $xls = FALSE;
  22.     }
  23. } else {
  24.     $xls = FALSE;
  25. }
  26.  
  27. if ($xls) {
  28.  
  29.     if (isset($plugin_list)) {
  30.         $plugin_list['xls'] = array(
  31.             'text' => 'strStrucNativeExcel',
  32.             'extension' => 'xls',
  33.             'mime_type' => 'application/vnd.ms-excel',
  34.             'force_file' => true,
  35.             'options' => array(
  36.                 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
  37.                 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
  38.                 array('type' => 'hidden', 'name' => 'data'),
  39.                 ),
  40.             'options_text' => 'strOptions',
  41.             );
  42.     } else {
  43.  
  44. /**
  45.  * Set of functions used to build MS Excel dumps of tables
  46.  */
  47.  
  48. /**
  49.  * Outputs comment
  50.  *
  51.  * @param   string      Text of comment
  52.  *
  53.  * @return  bool        Whether it suceeded
  54.  */
  55. function PMA_exportComment($text)
  56. {
  57.     return TRUE;
  58. }
  59.  
  60. /**
  61.  * Outputs export footer
  62.  *
  63.  * @return  bool        Whether it suceeded
  64.  *
  65.  * @access  public
  66.  */
  67. function PMA_exportFooter()
  68. {
  69.     global $workbook;
  70.     global $tmp_filename;
  71.  
  72.     $res = $workbook->close();
  73.     if (PEAR::isError($res)) {
  74.         echo $res->getMessage();
  75.         return FALSE;
  76.     }
  77.     if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) {
  78.         return FALSE;
  79.     }
  80.     unlink($tmp_filename);
  81.  
  82.     return TRUE;
  83. }
  84.  
  85. /**
  86.  * Outputs export header
  87.  *
  88.  * @return  bool        Whether it suceeded
  89.  *
  90.  * @access  public
  91.  */
  92. function PMA_exportHeader()
  93. {
  94.     global $workbook;
  95.     global $tmp_filename;
  96.  
  97.     if (empty($GLOBALS['cfg']['TempDir'])) {
  98.         return FALSE;
  99.     }
  100.     $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xls_');
  101.     $workbook = new Spreadsheet_Excel_Writer($tmp_filename);
  102.  
  103.     return TRUE;
  104. }
  105.  
  106. /**
  107.  * Outputs database header
  108.  *
  109.  * @param   string      Database name
  110.  *
  111.  * @return  bool        Whether it suceeded
  112.  *
  113.  * @access  public
  114.  */
  115. function PMA_exportDBHeader($db)
  116. {
  117.     return TRUE;
  118. }
  119.  
  120. /**
  121.  * Outputs database footer
  122.  *
  123.  * @param   string      Database name
  124.  *
  125.  * @return  bool        Whether it suceeded
  126.  *
  127.  * @access  public
  128.  */
  129. function PMA_exportDBFooter($db)
  130. {
  131.     return TRUE;
  132. }
  133.  
  134. /**
  135.  * Outputs create database database
  136.  *
  137.  * @param   string      Database name
  138.  *
  139.  * @return  bool        Whether it suceeded
  140.  *
  141.  * @access  public
  142.  */
  143. function PMA_exportDBCreate($db)
  144. {
  145.     return TRUE;
  146. }
  147.  
  148. /**
  149.  * Outputs the content of a table in CSV format
  150.  *
  151.  * @param   string      the database name
  152.  * @param   string      the table name
  153.  * @param   string      the end of line sequence
  154.  * @param   string      the url to go back in case of error
  155.  * @param   string      SQL query for obtaining data
  156.  *
  157.  * @return  bool        Whether it suceeded
  158.  *
  159.  * @access  public
  160.  */
  161. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  162. {
  163.     global $what;
  164.     global $workbook;
  165.  
  166.     $workbook->setTempDir(realpath($GLOBALS['cfg']['TempDir']));
  167.  
  168.     // Gets the data from the database
  169.     $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  170.     $fields_cnt  = PMA_DBI_num_fields($result);
  171.  
  172.     $row = PMA_DBI_fetch_row($result);
  173.     for ($sheetIndex = 0; ; $sheetIndex++) {
  174.         // Maximum sheet name length is 31 chars - leave 2 for numeric index
  175.         $sheetName = substr($table, 0, 29) . ($sheetIndex > 0 ? $sheetIndex : '');
  176.         $worksheet =& $workbook->addWorksheet($sheetName);
  177.         $rowIndex = 0;
  178.  
  179.         // If required, get fields name at the first line
  180.         if (isset($GLOBALS['xls_columns']) && $GLOBALS['xls_columns']) {
  181.             for ($i = 0; $i < $fields_cnt; $i++) {
  182.                 $worksheet->write(0, $i, stripslashes(PMA_DBI_field_name($result, $i)));
  183.             } // end for
  184.             $worksheet->repeatRows($rowIndex);
  185.             $worksheet->freezePanes(array($rowIndex + 1, 0, $rowIndex + 1, 0));
  186.             $rowIndex++;
  187.         } // end if
  188.  
  189.         // Format the data (max 65536 rows per worksheet)
  190.         while ($rowIndex < 65536 && $row) {
  191.             set_time_limit(0);
  192.             for ($j = 0; $j < $fields_cnt; $j++) {
  193.                 if (!isset($row[$j]) || is_null($row[$j])) {
  194.                     $worksheet->write($rowIndex, $j, $GLOBALS['xls_null']);
  195.                 } elseif ($row[$j] == '0' || $row[$j] != '') {
  196.                     /**
  197.                      * @todo we should somehow handle character set here!
  198.                      */
  199.                     $worksheet->write($rowIndex, $j, $row[$j]);
  200.                 } else {
  201.                     $worksheet->write($rowIndex, $j, '');
  202.                 }
  203.             } // end for
  204.             $rowIndex++;
  205.             $row = PMA_DBI_fetch_row($result);
  206.         } // end while
  207.         if (!$row) {
  208.             break;
  209.         }
  210.     } // end for
  211.     PMA_DBI_free_result($result);
  212.  
  213.     return TRUE;
  214. }
  215.  
  216.     }
  217. }
  218. ?>
  219.