За последние 24 часа нас посетили 52790 программистов и 1768 роботов. Сейчас ищут 997 программистов ...

Экспорт файлов в Excell

Тема в разделе "Прочие вопросы по PHP", создана пользователем nudist, 22 июл 2015.

  1. nudist

    nudist Новичок

    С нами с:
    22 июл 2015
    Сообщения:
    3
    Симпатии:
    0
    Подскажите как решить проблему, при помощи кода указанного ниже пытаюсь сделать экспорт данных в Excell, но вместо того чтобы просто экспортировать данные, он экспортирует всю страницу с кнопками, формами и прочими элементами расположенными на странице, заранее благодарен.
    Код (Text):
    1.  
    2. <?php
    3.     if( !defined( "ExcelExport" ) ) {
    4.      define( "ExcelExport", 1 );
    5.        class ExportToExcel {
    6.             var $xlsData = "";
    7.             var $fileName = "";
    8.             var $countRow = 0;
    9.             var $countCol = 0;
    10.             var $totalCol = 3;//общее число  колонок в Excel
    11.                     //конструктор класса
    12.             function __construct (){
    13.                     $this->xlsData = pack( "ssssss", 0x809, 0x08, 0x00,0x10, 0x0, 0x0 );
    14.             }
    15.                     // Если число
    16.             function RecNumber( $row, $col, $value ){
    17.                     $this->xlsData .= pack( "sssss", 0x0203, 14, $row, $col, 0x00 );
    18.                     $this->xlsData .= pack( "d", $value );
    19.                     return;
    20.             }
    21.                     //Если текст
    22.             function RecText( $row, $col, $value ){
    23.                     $len = strlen( $value );
    24.                     $this->xlsData .= pack( "s*", 0x0204, 8 + $len, $row, $col, 0x00, $len);
    25.                     $this->xlsData .= $value;
    26.                     return;
    27.             }
    28.                     // Вставляем число
    29.             function InsertNumber( $value ){
    30.                     if ( $this->countCol == $this->totalCol ) {
    31.                             $this->countCol = 0;
    32.                             $this->countRow++;
    33.                     }
    34.                     $this->RecNumber( $this->countRow, $this->countCol, $value );
    35.                     $this->countCol++;
    36.                     return;
    37.             }
    38.                     // Вставляем текст
    39.             function InsertText( $value ){
    40.                     if ( $this->countCol == $this->totalCol ) {
    41.                             $this->countCol = 0;
    42.                             $this->countRow++;
    43.             }
    44.                     $this->RecText( $this->countRow, $this->countCol, $value );
    45.                     $this->countCol++;
    46.                     return;
    47.             }
    48.                     // Переход на новую строку
    49.             function GoNewLine(){
    50.                     $this->countCol = 0;
    51.                     $this->countRow++;
    52.                     return;
    53.                     }
    54.                     //Конец данных
    55.             function EndData(){
    56.                     $this->xlsData .= pack( "ss", 0x0A, 0x00 );
    57.                     return;
    58.             }
    59.                     // Сохраняем файл
    60.             function SaveFile( $fileName ){
    61.                     $this->fileName = $fileName;
    62.                     $this->SendFile();
    63.             }
    64.                     // Отправляем файл
    65.             function SendFile(){
    66.                     $this->EndData();
    67.                     header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
    68.                     header ( "Cache-Control: no-store, no-cache, must-revalidate" );
    69.                     header ( "Pragma: no-cache" );
    70.                     header ( "Content-type: application/x-msexcel" );
    71.                     header ( "Content-Disposition: attachment; fileName=$this->fileName.xls" );
    72.                     print $this->xlsData;
    73.              }
    74.             }
    75.     }
    76.    
    77.     $filename = 'export';
    78.     $excel = new ExportToExcel();
    79.     $excel->InsertText('Идентификатор');
    80.     $excel->InsertText('Фамилия');
    81.     $excel->InsertText('Имя');
    82.     $excel->GoNewLine();
    83.    
    84.     $r=0;
    85.     While($r<10){
    86.         $r++;
    87.         $excel->InsertNumber($r);
    88.         $excel->InsertText('Фамилия'.$r);
    89.         $excel->InsertText('Имя'.$r);
    90.         $excel->GoNewLine();
    91.     }
    92.    
    93.     $excel->SaveFile($filename);
    94. ?>