简体   繁体   English

PHPExcel输出到浏览器不起作用

[英]PHPExcel output to browser not working

I am not really a PHP expert as my past experience is mostly geared towards the system engineering side. 我并不是真正的PHP专家,因为我过去的经验主要是针对系统工程方面的。 I am facing a problem with PHPExcel which gives me this error "Cannot modify header information - headers already sent by in line 1" when I want to output my XLSX file to the browser. 当我想将XLSX文件输出到浏览器时,我遇到了PHPExcel的问题,该问题使我收到此错误“无法修改标头信息-第1行已经发送的标头”。

Here is the sample of my code 这是我的代码示例

$host="localhost";
$uname="lol";
$pass="lol123";
$database = "lol12345"; 
$table="MemReport";
$table1="CPUReport";
$table2="TrafficReport";
$table3="HDDReport";


// create a file pointer connected to the output stream
$output = fopen('Memory.csv', 'w+');
$output1 = fopen('CPU.csv', 'w+');
$output2 = fopen('Traffic.csv', 'w+');
$output3 = fopen('HDD.csv', 'w+');

// output the column headings
fputcsv($output, array('HostName','Date','Time','Percent','TholdDescription'));
fputcsv($output1, array('HostName','Date','Time','Percent','TholdDescription'));
fputcsv($output2, array('HostName','Date','Time','Percent','TholdDescription'));
fputcsv($output3, array('HostName','Date','Time','Percent','TholdDescription'));

// fetch the data
mysql_connect($host, $uname, $pass);
mysql_select_db($database);
echo mysql_error(); 

$rows = mysql_query("SELECT * FROM $table order by date ASC, Time ASC");
echo mysql_error(); 
$rows1 = mysql_query("SELECT * FROM $table1 order by date ASC, Time ASC");
echo mysql_error(); 
$rows2 = mysql_query("SELECT * FROM $table2 order by date ASC, Time ASC");
echo mysql_error();
$rows3 = mysql_query("SELECT * FROM $table3 order by date ASC, Time ASC");
echo mysql_error(); 
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) 
fputcsv($output, $row);
while ($row1 = mysql_fetch_assoc($rows1))   
fputcsv($output1, $row1);
while ($row2 = mysql_fetch_assoc($rows2))
fputcsv($output2, $row2);
while ($row3 = mysql_fetch_assoc($rows3))
fputcsv($output3, $row3);


include '/tmp/Classes/PHPExcel/IOFactory.php';


$inputFileType = 'CSV';
$inputFileNames = array('HDD.csv','Traffic.csv','CPU.csv','Memory.csv');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames);
$objPHPExcel = $objReader->load($inputFileName);

$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
foreach($inputFileNames as $sheet => $inputFileName) {
    $objReader->setSheetIndex($sheet+1);
    $objReader->loadIntoExisting($inputFileName,$objPHPExcel);
    $objReader->loadIntoExisting($inputFileName,$objPHPExcel);
    $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));

} }

$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
//var_dump($sheetData);
//var_dump($sheetData);

$myfile = "Report.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header("Content-Disposition: attachment;filename=$myfile"); header(“ Content-Disposition:附件; filename = $ myfile”); header('Cache-Control: max-age=0'); header('Cache-Control:max-age = 0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');


}

any help is appreciated, most of my code has been copy and pasted and then edited to my knowledge, also i was able to get to email the xlsx file but just have problem sending it for download. 感谢任何帮助,我的大部分代码已被复制并粘贴,然后据我所知进行了编辑,我也能够通过电子邮件将xlsx文件发送出去,但是在发送下载时遇到了问题。 I don't mind reading the file from the directory for download. 我不介意从目录中读取文件进行下载。

regards, 问候,

Lin

No spaces or tabs, no error messages, no newline characters, no BOM markers, no other output whatsoever. 没有空格或制表符,没有错误消息,没有换行符,没有BOM标记,没有其他输出。

Note that the error message you're seeing tells you which file/line was responsible for the output headers already sent by xxx in line 1 .... so that's where to look. 请注意,您看到的错误消息告诉您哪个文件/行负责headers already sent by xxx in line 1的输出headers already sent by xxx in line 1 ....因此,可以在此处查找。

At line 1 of a file, it's most likely to be something before your opening <?php tag, or a file saved with a BOM header 在文件的第1行,最有可能是在打开<?php标记之前发生的事情,或者是带有BOM表头保存的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM