$objWriter->save('php://output');

Nov 26, 2014 at 8:04 AM
Please help. I've written some PHP excel to export some data to an excel file. This works perfectly fine.

I then changed the code adding headers and output to export the data, but it is now outputting the webpage not the file. I'm completely new to this, and can't find any previous questions relevant to this. Here is my code:

if(isset($_POST['esd']))
{
$sqlcust = "SELECT * FROM compliance_customers WHERE Customer_ref = " . $_REQUEST['cust'];
$sqlcustresult = mysqli_query($conn, $sqlcust);
while($row_sqlcustresult = mysqli_fetch_array($sqlcustresult))
{
    $name = $row_sqlcustresult['Customer_name'];
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'_'.$date.'.xlsx"');
}

use php excel to create a spreadsheet of the customers current subscription

if( (isset($_REQUEST['esd'])) && (!isset($noEsd)) )
{
// Set properties for the excel file
$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator($_SESSION['username']);
$objPHPExcel->getProperties()->setLastModifiedBy($_SESSION['username']);
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Customer export_" . $customername);
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Customer export_" . $customername);
$objPHPExcel->getProperties()->setDescription($customername . "_" . $date);

$cnt_current_record = 2;

$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Content Name');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Content Type');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Content Description');

$sql_excelfile = "SELECT * FROM compliance_changes ORDER BY change_type";
$result_sql_excelfile = mysqli_query($conn, $sql_excelfile);
while($row_sql_excelfile = mysqli_fetch_array($result_sql_excelfile))
{
    if(in_array($row_sql_excelfile['change_name'], $excel_array))
    {
        #echo  "<tr class='subscriptiondetails'><td>" .$row_sql_excel['change_name']."</td><td>" .$row_sql_excel['change_type']."</td><td>". $row_sql_excel['change_description']."</td></tr>";
        #$objPHPExcel->setActiveSheetIndex($cnt_current_record);
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $cnt_current_record, $row_sql_excelfile['change_name']);
        $objPHPExcel->getActiveSheet()->SetCellValue('B' . $cnt_current_record, $row_sql_excelfile['change_type']);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $cnt_current_record, $row_sql_excelfile['change_description']);
        $cnt_current_record++;
    }
}
#rename sheet
$sheet_title = $customername;
$objPHPExcel->getActiveSheet()->setTitle($sheet_title);

#write the xls file
#$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filename = $customername . "_" . $date . ".xlsx";
#$objWriter->save(str_replace('customerdetail2.php', $filename, __FILE__));

$objWriter->save(str_replace('customerdetail2.php', $filename, 'php://output'));

#echo the file has been written
echo $filename . " has been created";

#$noEsd = 'Yes';
}