从数据库导出时出现PHPexcel库错误

时间:2013-12-18 00:40:27

标签: php mysql phpexcel

我正在使用PHPexcel库进行报告。当我使用静态值时它运行良好,但是当我尝试将MySQL数据库中的数据导出到PHPexcel时,

它总是给我这个错误

Warning: Cannot modify header information - headers already sent 

我尝试了ob_start()功能但是当我打开下载的excel时,它是空的

这是我的PHPexcel代码:

<?php

include("conn.php");

$query ="Select * from info";
$result = mysql_query($query);



/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();




// Add some data
$row1=1;
$row2=1;
$row3=1;

echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);


while($row = mysql_fetch_array($result))
{
$let1 = "A".$row1."";
$let2 = "B".$row2."";
$let3 = "C".$row3."";

$objPHPExcel->getActiveSheet()->SetCellValue($let1, $row['id']);
$objPHPExcel->getActiveSheet()->SetCellValue($let2, $row['name']);
$objPHPExcel->getActiveSheet()->SetCellValue($let3, $row['age']);

$row1++;
$row2++;
$row3++;
}

$objPHPExcel->getActiveSheet()->getStyle("A1:C1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle("A1:C1")->getFont()->setSize(20);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:C1');

$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(
    array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FF0000')
        )
    )
);


header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");

exit();

?>

它可以很好地处理静态值,但是当我尝试动态添加值时,它会变得混乱。请帮助,谢谢你提前

1 个答案:

答案 0 :(得分:4)

查看代码时,您的header()调用表示应将页面内容视为电子表格,但不要从电子表格对象创建任何输出。删除所有输出(你的回音调用),因为它不可能有它,将标题调用移到文件的顶部并回显Excel类的原始输出。

以下代码。我移动了标题,删除了一些垃圾,并添加了PHPExcel手册建议写入STDOUT的方法。

<?php

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");

/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

include("conn.php");

$query ="Select * from info";
$result = mysql_query($query);

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

// Add some data
$row1=1;
$row2=1;
$row3=1;

$objPHPExcel->setActiveSheetIndex(0);

while($row = mysql_fetch_array($result))
{
    $let1 = "A".$row1."";
    $let2 = "B".$row2."";
    $let3 = "C".$row3."";

    $objPHPExcel->getActiveSheet()->SetCellValue($let1, $row['id']);
    $objPHPExcel->getActiveSheet()->SetCellValue($let2, $row['name']);
    $objPHPExcel->getActiveSheet()->SetCellValue($let3, $row['age']);

    $row1++;
    $row2++;
    $row3++;
}

$objPHPExcel->getActiveSheet()->getStyle("A1:C1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle("A1:C1")->getFont()->setSize(20);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:C1');

$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(
    array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FF0000')
        )
    )
);

// Untested... pulled from the manual as the way to write with PHPExcel
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');                                                                                                           
$objWriter->save('php://output');

?>