2016-03-21 98 views
0

任何人都可以幫助我使用PHPExcel嗎?空白頁和沒有下載PHPExcel

我有這樣的代碼......

<?php 
session_start(); 
include 'dbconnect.php'; 
date_default_timezone_set("Europe/Rome"); 

if (!isset($_SESSION['user'])) { 
    header("Location: index.php"); 
} 
$query = mysql_query("SELECT * FROM partner1 WHERE uid=" . $_GET['id']); 
while ($userRow1 = mysql_fetch_assoc($query)) { 

    /** Error reporting */ 
    error_reporting(E_ALL); 
    ini_set('display_errors', TRUE); 
    ini_set('display_startup_errors', TRUE); 
    date_default_timezone_set('Europe/Rome'); 

    if (PHP_SAPI == 'cli') 
     die('This example should only be run from a Web Browser'); 

    /** Include PHPExcel */ 
    require_once dirname(__FILE__) . '/Classes/PHPExcel.php'; 


// Create new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 

    $objPHPExcel->getActiveSheet()->getStyle("A1:A27")->getFont()->setBold(true); 
    $objPHPExcel->getActiveSheet()->getStyle("A1:A27")->getFont()->setSize(16); 
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('7')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('8')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('9')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('10')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('11')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('12')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('13')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('14')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('15')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('16')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('17')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('18')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('19')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('20')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('21')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('22')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('23')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('24')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('25')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('26')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getRowDimension('27')->setRowHeight(40); 
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); 
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); 


// Set document properties 
    $objPHPExcel->getProperties()->setCreator("Test 123") 
     ->setLastModifiedBy("-") 
     ->setTitle("" . $userRow1['nome'] . " " . $userRow1['cognome'] . "") 
     ->setSubject("-") 
     ->setDescription("-") 
     ->setKeywords("-") 
     ->setCategory("-"); 


// Add some data 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A1', 'Codice Op') 
     ->setCellValue('A2', 'DATA:') 
     ->setCellValue('A3', 'DATI CLIENTE') 
     ->setCellValue('A4', 'Nome') 
     ->setCellValue('A5', 'Cognome') 
     ->setCellValue('A6', 'Data e Luogo di nascita') 
     ->setCellValue('A7', 'Via') 
     ->setCellValue('A8', 'Cumune,Cap,PR') 
     ->setCellValue('A9', 'Telefono rete fissa') 
     ->setCellValue('A10', 'Recapito Alternativo') 
     ->setCellValue('A11', 'Tipo Documento') 
     ->setCellValue('A12', 'Ente di rilascio') 
     ->setCellValue('A13', 'Data Rilascio') 
     ->setCellValue('A14', 'Data Scadenza') 
     ->setCellValue('A15', 'Codice Fiscale') 
     ->setCellValue('A16', 'PROPOSTA') 
     ->setCellValue('A17', 'OFFERTA') 
     ->setCellValue('A18', 'TIM VISION') 
     ->setCellValue('A19', 'Modem (s/n)') 
     ->setCellValue('A20', 'Gestore telefonico') 
     ->setCellValue('A21', 'Codice di migrazione1') 
     ->setCellValue('A22', 'Codice di migrazione2') 
     ->setCellValue('A23', 'ICCID') 
     ->setCellValue('A24', 'Numero per Smart') 
     ->setCellValue('A25', 'Gestore Mobile') 
     ->setCellValue('A26', 'Inserimento Elenco Telefonico') 
     ->setCellValue('A27', 'NATIVO TELECOM') 
     ->setCellValue('B1', '-') 
     ->setCellValue('B2', '' . $userRow1['data'] . '') 
     ->setCellValue('B3', '-') 
     ->setCellValue('B4', '' . $userRow1['nome'] . '') 
     ->setCellValue('B5', '' . $userRow1['cognome'] . '') 
     ->setCellValue('B6', '' . $userRow1['natoil'] . '') 
     ->setCellValue('B7', '' . $userRow1['via'] . '') 
     ->setCellValue('B8', '' . $userRow1['cumune'] . ' , ' . $userRow1['cap'] . ' , ' . $userRow1['provincia'] . '') 
     ->setCellValue('B9', '' . $userRow1['numero'] . '') 
     ->setCellValue('B10', '' . $userRow1['ricapitodicell'] . '') 
     ->setCellValue('B11', '' . $userRow1['documento'] . '') 
     ->setCellValue('B12', '' . $userRow1['rilascio'] . '') 
     ->setCellValue('B13', '' . $userRow1['scadenza'] . '') 
     ->setCellValue('B14', '' . $userRow1['codicefiscale'] . '') 
     ->setCellValue('B15', '-') 
     ->setCellValue('B16', '' . $userRow1['ofertascelta'] . '') 
     ->setCellValue('B17', '-') 
     ->setCellValue('B18', '-') 
     ->setCellValue('B19', '' . $userRow1['operatoretelefonico'] . '') 
     ->setCellValue('B20', '' . $userRow1['codicemigrazione'] . '') 
     ->setCellValue('B21', '' . $userRow1['iccid'] . '') 
     ->setCellValue('B22', '' . $userRow1['celloftsm'] . '') 
     ->setCellValue('B23', ' ') 
     ->setCellValue('B24', '-') 
     ->setCellValue('B25', '-') 
     ->setCellValue('B26', '-') 
     ->setCellValue('B27', '-'); 

// Rename worksheet 
    $objPHPExcel->getActiveSheet()->setTitle('Simple'); 
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); 
    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); 


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet 
    $objPHPExcel->setActiveSheetIndex(0); 


// Rename worksheet 
    $objPHPExcel->getActiveSheet()->setTitle('Simple'); 
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); 
    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); 


// Set active sheet index to the first sheet, so Excel opens this as the first sheet 
    $objPHPExcel->setActiveSheetIndex(0); 


// Redirect output to a client’s web browser (Excel5) 
    header('Content-Type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment;filename="01simple.xls"'); 
    header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
    header('Cache-Control: max-age=1'); 

// If you're serving to IE over SSL, then the following may be needed 
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified 
    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
    header('Pragma: public'); // HTTP/1.0 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
    $objWriter->save('php://output'); 
} 

exit; 

當我嘗試閱讀和下載我的數據庫中的數據爲Excel格式得到了一個空白頁面,並沒有下載。

我沒有得到任何錯誤。我不知道該怎麼辦笑

+0

通常,當您最終出現空白頁面時,這是因爲您的代碼中存在錯誤並且錯誤報告已關閉。 – ThrowBackDewd

+1

錯誤報告是TRUE隊友,即時通訊正在監視每一次刷新我的error_log –

+0

我注意到的一件事是,你有你的while語句關閉在你的頁面的最後,但你的頭裏麪包括。這意味着你多次發送你的標題。我建議的另一件事是在這些行上使用for循環:$ objPHPExcel-> getActiveSheet() - > getRowDimension('1') - > setRowHeight(40);'。你所做的只是從1-27增加,所以你可以簡化代碼。 – ThrowBackDewd

回答

0

明顯的問題

if(!isset($_SESSION['user'])) 
{ 
    header("Location: index.php"); 
} 

仍將繼續執行你包括PHPExcel代碼

的其餘部分(只是這堵牆的代碼涉水)創建一個新的PHPExcel對象,併發送響應到瀏覽器中的每一行,同時$userRow1 = mysql_fetch_assoc($query))循環....我假設你真的想要將每一行寫入相同的文件

編輯

試圖保持答案簡單,而不是代碼的牆,你的結構應該是這樣的:

include 'dbconnect.php'; 

/** Include PHPExcel */ 
require_once dirname(__FILE__) . '/Classes/PHPExcel.php'; 

// Create new PHPExcel object 
$objPHPExcel = new PHPExcel(); 

$query=mysql_query("SELECT * FROM partner1 WHERE uid=".$_GET['id']); 

while($userRow1 = mysql_fetch_assoc($query)) { 
    // Add some data 
    $objPHPExcel->setActiveSheetIndex(0) 
      ->setCellValue('A1', 'Codice Op') 
      ..... 
    ; 
} 

// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="01simple.xls"'); 
header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
header('Cache-Control: max-age=1'); 

// If you're serving to IE over SSL, then the following may be needed 
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); // HTTP/1.0 

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output'); 
exit; 

還要注意的是mysql擴展在PHP過時,你確實應該使用mysqli或帶有預處理語句/綁定變量的PDO來保護您的腳本免受SQL注入的影響

+0

你能幫我通過對代碼進行必要的修改嗎? –

+0

現在已下載但空白xls文件:( –

+0

也許你的SQL查詢沒有返回任何記錄 –