php+excel存html,PHPExcel保存CSV也是在添加网页HTML

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

> .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();

解决方法:

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

一个例子:

>这是对的:

$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()->setTitle("Report");

// ...

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

?>

>这是错的:

Export to Excel

echo '

Export to Excel

';

$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()->setTitle("Report");

// ...

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

?>

标签:php,csv,file-extension,phpexcel,prado

来源: https://codeday.me/bug/20190726/1542231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值