PHPExcel保存CSV也是在添加网页HTML

时间:2011-07-27 18:05:45

标签: php csv phpexcel file-extension prado

我有一个网页,其中有一个广播组作为您要保存的文件格式的选项。选项包括:

  • 的.xls
  • 的.xlsx
  • 的.csv

所有工作除了.csv之外,因为它还将页面HTML添加到文件的底部。

以下是我正在尝试的内容(代码段显示功能):

// Creating the format
$data = $this->getQueryResults();

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle("Report"); 

$objPHPExcel->getProperties()->setCreator("me");     
$objPHPExcel->getProperties()->setLastModifiedBy("me");                 
$objPHPExcel->getProperties()->setSubject("Report Stuff");     
$objPHPExcel->getProperties()->setDescription("Report Stuff");

// Next I iterate through the data array
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);

// check the radio option selected for the file format
if($this->radioXLS->Checked) {
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
    header('Cache-Control: max-age=0');
}

if($this->radioXLSX->Checked) {
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

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

if($this->radioCSV->Checked) {
    ob_end_clean(); // add/removing this does nothing
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $objWriter->setDelimiter(',');
    $objWriter->setEnclosure('');
    $objWriter->setLineEnding("\r\n");
    $objWriter->setSheetIndex(0);
    ob_end_clean(); // add/removing this does nothing

    header('Content-Type: text/csv');
    header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
    header('Cache-Control: max-age=0');
}

$objWriter->save('php://output');

有关为什么将页面HTML附加到.csv文件的任何想法?

另一方面,这是一个普拉多项目,如果重要的话

更新:

多一点......

我有一个网页,以表格格式(Think table / grid)生成报告。在同一页面上,我可以选择将表格格式的日期保存到Excel .xls(不知何故.xlsx现在不能正常工作,呃...)。用户可以选择将文件保存在.xls .xlsx .csv中,当单击该文件从该页面下载时。

这是否会导致已经呈现的网页通过以下内容添加到输出中:php://output

更新 - 解决方案:

在查看excel文件后,它还添加了网页HTML。我也查看了输出缓冲区的PHP函数,但仍然无法正常工作

    while(ob_get_level() > 0) {
        ob_end_clean();
    }

    if($this->radioCSV->Checked) {                        
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
        $objWriter->setDelimiter(',');
        $objWriter->setEnclosure('');
        $objWriter->setLineEnding("\r\n");
        $objWriter->setSheetIndex(0);
    } elseif($this->radioXLSX->Checked) {
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    } else {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    } 

    $objWriter->save('php://output');
    exit();

2 个答案:

答案 0 :(得分:5)

写入php://output与执行普通echo语句完全相同。 $objWriter->save()的输出将添加到回显或位于PHP块之外的所有其他内容(<?php ... ?>)。


一个例子:

  • 这是正确

    <?php
    
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle("Report"); 
    // ...
    $objWriter->save('php://output');
    
    ?>
    
  • 错误

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="es">
    <head><title>Export to Excel</title>
    </head>
    <body>
    <?php
    
    echo '<h1>Export to Excel</h1>';
    
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle("Report"); 
    // ...
    $objWriter->save('php://output');
    
    ?>
    </body>
    </html>
    

答案 1 :(得分:3)

您是否确认Excel文件不包含HTML? Excel可能会忽略它们。在最后一行之后添加exit();会改变什么吗?