空白页面和否下载PHPExcel

时间:2016-03-21 17:56:42

标签: php mysql excel 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格式时,我得到一个空白页面而没有下载。

我没有收到任何错误。我不知道该怎么做大声笑

1 个答案:

答案 0 :(得分:0)

明显的问题(只是涉及这段代码)

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

仍将继续执行其余代码

您包括PHPExcel,创建一个新的PHPExcel对象,并将响应发送到您在while $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;

另请注意,在PHP中不推荐使用mysql扩展,您应该使用mysqli或PDO和预处理语句/绑定变量来保护脚本免受SQL注入