我有一个页面,在帖子上创建一个带有PHPExcel的xls文件,工作正常,文件下载弹出,一切都是黄金,但当我尝试打开文件时,我得到一个错误,说excel无法打开文件。我检查了所有进入的数据,一切都很好,所以我认为它必须与文件有关。
我在notepad ++中打开了下载的xls,果然,文件顶部有html,页面的html相同,内容被调用的地方和检查帖子的php运行。一旦我删除它开放得很好,仍然缺少一些字段,但我想在我担心之前让它实际可见。
这是我创建xls
的代码global $__CMS_CONN__;
error_reporting(E_ALL);
$listmem = $_SESSION['listmem'];
$evtdate1 = date('Y-m-d', strtotime('-1 year', strtotime(date('Y-m-d'))));
$evtdate2 = date('Y-m-d');
require_once('/web/content/wolf/plugins/Classes/PHPExcel.php');
$sql = "SELECT Count(*) AS number FROM (SELECT DISTINCT event_timestamp FROM membership_attendance_log WHERE event_timestamp >= '".$evtdate1."' AND event_timestamp <= '".$evtdate2."') as numevents";
$evtcount = $__CMS_CONN__->prepare($sql);
$evtcount->execute();
$count = $evtcount->fetchAll(PDO::FETCH_ASSOC);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Date Range: '.$evtdate1." to ".$evtdate2)
->setCellValue('D1', 'Total number of events: '.$count['number'])
->setCellValue('A2', 'Member')
->setCellValue('B2', 'Missed Meetings')
->setCellValue('C2', 'Attendance')
->setCellValue('D2', 'Business Name')
->setCellValue('E2', 'Title')
->setCellValue('F2', 'Phone')
->setCellValue('G2', 'Email');
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
$i = 3;
foreach($listmem as $row)
{
$qry = "SELECT Count(membership_individual_id) AS attendance FROM membership_attendance_log WHERE membership_individual_id=".$row['id']." AND event_timestamp >= '".$evtdate1."' AND event_timestamp <= '".$evtdate2."'";
$stmt = $__CMS_CONN__->prepare($qry);
$stmt->execute();
$attendance = $stmt->fetchAll(PDO::FETCH_ASSOC);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $row['first_name']." ".$row['last_name'])
->setCellValue('B'.$i, $attendance['attendance'])
->setCellValue('C'.$i, floor($attendance['attendance'] / $count['number']))
->setCellValue('D'.$i, $row['business_name'])
->setCellValue('E'.$i, $row['title'])
->setCellValue('F'.$i, $row['business_phone'])
->setCellValue('G'.$i, $row['email']);
$i++;
}
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->setTitle('Report');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Report-'.date("mdY").'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
这是在使用wolfCMS的网站上,只是fyi。我试着将它从文件中拉出来,只是把它发布到一个单独的php文件中,但我仍然有同样的事情。
答案 0 :(得分:0)
尝试在发送标头之前添加它
ob_end_clean();
它应该解决你的问题。我认为输出缓冲区的一些数据导致了这个问题。