【问题标题】:Displaying excel file in a browser. PHPExcel在浏览器中显示 excel 文件。 PHPExcel
【发布时间】:2012-02-12 22:33:58
【问题描述】:

例如我正在使用这样的代码:

<?php

    require_once("D:\server/www/cls/PHPExcel.php");
    require_once("D:\server/www/cls/PHPExcel/IOFactory.php");

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', 'HeaderB');    
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2', 'HeaderC');
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', 'HeaderD');    
    ob_end_clean();

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="report.xlsx"');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();

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

它下载report.xlsx 文件并且不在浏览器中显示它。我该怎么做?

谢谢!

【问题讨论】:

    标签: php browser phpexcel


    【解决方案1】:

    删除此行...

    header('Content-Disposition: attachment;filename="report.xlsx"');
    

    用户的浏览器中也必须有能够查看 Excel 文件的东西。

    【讨论】:

    • 这很有趣.. 现在我正在下载*.php 文件本身:) 怎么了?我已删除内容处置标头。
    • @helicera:您需要为文件名提供xlsx 扩展名。
    • 确保您的代码中没有任何标题。从头开始检查或搜索标题。我花了 2 个小时才意识到我的包含中有 1 个 header()。
    猜你喜欢
    • 2014-10-21
    • 2017-03-27
    • 2011-03-31
    • 1970-01-01
    • 2010-11-28
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多