2

我有一个页面可以在帖子上使用 PHPExcel 创建一个 xls 文件,它工作正常,文件下载弹出并且一切都是金色的,但是当我尝试打开文件时,我收到一个错误,说 excel 无法打开文件。我检查了所有输入的数据,一切都很好,所以我认为它必须与文件有关。

我在记事本++中打开了下载的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 的网站上,仅供参考。我尝试将其从文件中拉出,然后将其发布到单独的 php 文件中,但我仍然得到相同的结果。

4

1 回答 1

0

尝试在发送标头之前添加 ob_end_clean(); 它应该可以解决您的问题。我认为输出缓冲区上的一些数据导致了这个问题。

于 2012-05-24T18:50:52.307 回答