2014-08-27 91 views
2

有关在浏览器中显示excel内容而不要求保存附件的问题。 我可以通过附件打开excel文件下载。但无法在浏览器中显示它。使用PHPExcel和PHPExcel_Writer_Excel2007在浏览器中显示excel文件

$writer = new PHPExcel_Writer_Excel2007($spreadsheet); 
//$writer = PHPExcel_IOFactory::createWriter(); 
ob_end_clean(); 
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet'); 
ob_end_clean(); 
$writer->save('php://output'); 
exit; 

这是相同的,以 Displaying excel file in a browser. PHPExcel

@helicera:你需要给文件名的扩展名的xlsx。 - 亚历克斯12年1月18日在7:05

但我可以给xlsx延长后删除 标题('内容处置:附件;文件名=“report.xlsx”'); 行?

======================== from loading excel ==================

<?php 
error_reporting(E_STRICT); 
require_once 'PHPExcel.php'; 
require_once 'PHPExcel/Writer/Excel2007.php'; 
require_once 'PHPExcel/IOFactory.php'; 
$inputFileName = 'example.xls'; 
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML'); 
$objWriter->save('example.html'); 
?> 

回答

2

您无法获得本机格式的Excel文件以显示在浏览器中....网络浏览器将呈现HTML标记,并且可以以各种格式显示图像,但不能显示其他任意文件格式。

如果你想显示在Web浏览器的Excel文件,你需要使用PHPExcel的HTML作家,这将生成HTML标记,一个浏览器能够显示

+0

感谢你的。但似乎没有奏效。我正在努力缩小范围,以便人们可以轻松地提供帮助。我可以使用save('example.xls)将它保存到www目录中。现在我正试图从那里加载excel文件。它仍然没有显示任何东西。你知道为什么吗? – rinjan 2014-08-27 14:20:32

+1

“没有工作”是指什么?不要发送任何头文件,只保存到''php:// output'' ...如果你保存到'example.html',它将保存到webserver上的一个名为example.html的文件中,而不是发送任何东西到浏览器 – 2014-08-27 14:22:09

+0

非常感谢你!当我使用php://输出时它正在工作 – rinjan 2014-08-27 14:29:30

相关问题