gpt4 book ai didi

PHPExcel 保存 CSV 也在添加网页 HTML

转载 作者:可可西里 更新时间:2023-11-01 00:16:39 25 4
gpt4 key购买 nike

我有一个网页,其中有一个单选组作为您希望保存的文件格式的选项。选项是:

  • .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 文件有什么想法吗?

顺便说一句,如果重要的话,这是一个 Prado 项目

更新:

还有一点……

我有一个网页可以生成制表格式的报告(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();

最佳答案

写信给 php://output与普通的 echo 完全一样陈述。 $objWriter->save() 的输出将添加到 回显 或位于 PHP block 之外的所有其他内容 ( <?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>

关于PHPExcel 保存 CSV 也在添加网页 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com