首页 文章

PHPExcel输出到浏览器无法正常工作

提问于
浏览
0

我不是真正的PHP专家,因为我过去的经验主要面向系统工程方面 . 我正面临PHPExcel的问题,当我想将XLSX文件输出到浏览器时,它给出了这个错误“无法修改 Headers 信息 - 第1行已发送的 Headers ” .

这是我的代码示例

$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('Cache-Control:max-age = 0');

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


}

任何帮助表示赞赏,我的大部分代码都经过复制和粘贴,然后根据我的知识编辑,我也能够通过电子邮件发送xlsx文件,但发送下载时遇到问题 . 我不介意从目录中读取文件以供下载 .

问候,

1 回答

  • 0

    没有空格或制表符,没有错误消息,没有换行符,没有BOM标记,没有任何其他输出 .

    请注意,您看到的错误消息告诉您哪个文件/行负责输出 headers already sent by xxx in line 1 ....所以这是在哪里查看 .

    在文件的第1行,最有可能是在打开 <?php 标记之前,或者使用BOM Headers 保存的文件

相关问题