我想为用户下载生成2个excel文件:一个成功,但另一个不成功

我有这个代码:

<?php require_once '../include/Classes/PHPExcel.php' ?> <?php require_once '../include/Classes/PHPExcel/IOFactory.php' ?> <?php require_once '../include/Classes/PHPExcel/Writer/Excel2007.php' ?> $PHPExcel = new PHPExcel(); $reader = PHPExcel_IOFactory::createReader('Excel2007'); $PHPExcel = $reader->load("path"); ............work on setting the excel $PHPExcelWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007'); $PHPExcelWriter->save($filename . '.xlsx'); $PHPExcel->disconnectWorksheets(); unset($PHPExcel); if (file_exists($filename . '.xlsx')) { header('Content-Description: File Transfer'); header('Content-Transfer-Encoding: binary'); header("Content-Disposition: attachment;filename=\"$filename.xlsx\""); header('Content-Type: application/vnd.ms-excel;charset=UTF-8;'); header('Content-Length: ' . filesize($filename . '.xlsx')); header('Pragma: no-cache'); header('Expires: 0'); ob_clean(); flush(); @readfile($filename . '.xlsx'); exit; } 

这个是成功的。而且这个文件可以被用户下载到那里,而不仅仅是把文件保存在本地主机上。

 if (isset($_POST['Export1']) && $_POST['Export1'] == 'Export to Excel') { $invoice_id = $_POST['invoice_id']; $invoice_date = $_POST['invoice_date']; $event_code = $_POST['event_code']; //$total_price = $_POST['total_price']; //$total_discount = $_POST['total_discount']; $total_amount = $_POST['total_amount']; $invoice_code = $_POST['invoice_code']; $bill_to = $_POST['bill_to']; $prog_sub = $_POST['prog_sub']; $temp_prog_sub = explode('-', $prog_sub); $program = $temp_prog_sub[0]; $subject = $temp_prog_sub[1]; $init_first_row = 15; $PHPExcel = new PHPExcel(); $reader = PHPExcel_IOFactory::createReader('Excel2007'); $PHPExcel = $reader->load("../include/Classes/Invoice_In-school.xlsx"); // sample is your file name $PHPExcel->getProperties()->setCreator("Pathways"); $PHPExcel->getProperties()->setTitle("In-School Invoice"); $PHPExcel->setActiveSheetIndex(0); $PHPExcel->getActiveSheet()->setCellValue('D7', $bill_to); $PHPExcel->getActiveSheet()->setCellValue('AD7', $invoice_date); $PHPExcel->getActiveSheet()->setCellValue('AD9', $invoice_code); //place a invoice_code here $PHPExcel->getActiveSheet()->setCellValue('D12', $program . ' ' . $subject . '(' . $event_code . ')'); //$PHPExcel->getActiveSheet()->setCellValue('K24', '$' .$total_price); //$PHPExcel->getActiveSheet()->setCellValue('Q24', '$' .$total_discount); $PHPExcel->getActiveSheet()->setCellValue('AD25', '$' . $total_amount); if (isset($_POST['activity_id'])) { for ($j = 0; $j < count($_POST['activity_id']); $j++) { $amount = $_POST['amount'][$j]; $date = $_POST['date'][$j]; $start_time = $_POST['start_time'][$j]; $end_time = $_POST['end_time'][$j]; (String) $date_col = 'A' . $init_first_row; (String) $time_col = 'N' . $init_first_row; (String) $amount_col = 'AD' . $init_first_row; $PHPExcel->getActiveSheet()->setCellValue((String) $date_col, $date); $PHPExcel->getActiveSheet()->setCellValue((String) $time_col, $start_time . '-' . $end_time); $PHPExcel->getActiveSheet()->setCellValue((String) $amount_col, '$' . $amount); $init_first_row++; } } if (isset($_POST['activity_id'])) { $no_of_act = count($_POST['activity_id']); } else { $no_of_act = 0; } $i = $_POST['i']; $i-=$no_of_act; for ($j = 0; $j < $i; $j++) { $j+=$no_of_act; $amount = $_POST['amount'][$j]; $date = $_POST['date'][$j]; $start_time = $_POST['start_time'][$j]; $end_time = $_POST['end_time'][$j]; (String) $date_col = 'A' . $init_first_row; (String) $time_col = 'N' . $init_first_row; (String) $amount_col = 'AD' . $init_first_row; $PHPExcel->getActiveSheet()->setCellValue((String) $date_col, $date); //date $PHPExcel->getActiveSheet()->setCellValue((String) $time_col, $start_time . '-' . $end_time); //time $PHPExcel->getActiveSheet()->setCellValue((String) $amount_col, '$' . $amount); $init_first_row++; $j-=$no_of_act; } //ob_start(); $filename="XXXXX"; $PHPExcelWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007'); $PHPExcelWriter->save($filename . '.xlsx'); $PHPExcel->disconnectWorksheets(); unset($PHPExcel); if (file_exists($filename . '.xlsx')) { header('Content-Description: File Transfer'); header('Content-Transfer-Encoding: binary'); header("Content-Disposition: attachment;filename=\"$invoice_code.xlsx\""); header('Content-type: application/octet-stream'); //header('Content-Type: application/vnd.ms-excel;charset=UTF-8;'); header('Content-Length: ' . filesize($filename . '.xlsx')); header('Pragma: no-cache'); header('Expires: 0'); ob_clean(); flush(); @readfile($filename . '.xlsx'); exit; } echo("<script>location.href ='XXXXXXX.php';</script>"); } 

而这一个失败。并输出这个:

 PK  D$ Hp [Content_Types].xml   N 0E |E -Jܲ@5  *Q> ؓƪ_򸯿g   $R v'J =   &[k 5D ޕlXXNz ݢdo   e  S x%    j4    KV  9GY X   T>Z  1.xr) o [. K R 6  ... 

而失败的一个更复杂,我试图使用ob_start(); 但没有工作。 请帮忙!!!!!!!!!