我创建了一个动态创建文件的程序。它工作正常。当我添加代码以提示用户保存文件的位置时,它会保存,但是当我尝试打开文件时,我收到错误“无法打开文件,因为文件格式或文件扩展名无效”。当我保存它没有提示它打开正常。
代码的相关部分是:
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('php://output');
#echo the file has been written
echo $filename . " has been created";
我对PHPexcel不太熟悉,不知道为什么如果我只保存它,文件打开很好,但是当我使用 $ objWriter-&GT;保存( 'PHP://输出'); 输出代码,它失败了。有人可以帮忙吗?
答案 0 :(得分:0)
感谢@markbaker的解决方案。
我有PHP代码在与php:// output相同的页面上回显代码。这会导致使用我从未意识到的页面中的HTML / PHP覆盖流。如果遇到同样的问题,一个很好的方法是使用文本编辑器打开文件,如果你的流包含html而不是输出信息,这也可能是你的问题。
我改变了这个,所以点击时的表单发布到另一个页面,只包含我想要输出的流的代码,这非常好用。
我希望这有帮助, 斯蒂芬。