开发者

PHPExcel adding html to xls

I have a page that creates an xls file with PHPExcel on post, which works fine, the file download pops up and everything is golden, but when i try to open the file i get an error saying excel cannot open the file. I checked all the data going in and everything was fine, so i figured it had to be something with the file.

I opened the downloaded xls in notepad++, and sure enough, there was html at the top of the file, the same html for the page, up to where the content was called and the php checking the post ran. As soon as i deleted that it opened up just fine, still missing a few fields, but I'd like to make it actually viewable before i worry about that.

Here's my code for creating the 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;

This is on a site using wolfCMS, just fyi. I tried pulling it out of the file and just having it post to a seperate php file but i still got the same thing.


Try adding this just before sending the header ob_end_clean(); It should solve your problem. I think some data on output buffer is causing the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜