Php 如何直接向用户输出EXCEL文件而不在本地存储?

Php 如何直接向用户输出EXCEL文件而不在本地存储?,php,ajax,phpexcel,Php,Ajax,Phpexcel,我使用PHPExcel生成excel文件(疯狂吧?),其中包含我从数据库获得的信息,问题是每当我创建文件时,它都会保存在服务器中,然后我只向用户发送指向该文件的链接,以便用户可以访问它 我怀疑这是否是正确的方法,我想要的是只将文件发送到浏览器,而不将其保存在服务器上 如何将EXCEL文件直接输出给用户而不在本地存储? 代码: 一旦我在$中收到文件。ajax调用中收到该文件,只需将其添加到锚定标记中: $.ajax({ ... success : function(re

我使用PHPExcel生成excel文件(疯狂吧?),其中包含我从数据库获得的信息,问题是每当我创建文件时,它都会保存在服务器中,然后我只向用户发送指向该文件的链接,以便用户可以访问它

我怀疑这是否是正确的方法,我想要的是只将文件发送到浏览器,而不将其保存在服务器上

如何将EXCEL文件直接输出给用户而不在本地存储?

代码:

一旦我在
$中收到
文件
。ajax
调用中收到该文件,只需将其添加到锚定标记中:

$.ajax({
       ...
       success : function(response){
           $('#container').append('<a href="'+response.File+'">Here is your file</a>
       }
$.ajax({
...
成功:功能(响应){
$('#container')。追加('
}
您可以发送

标题(“内容处置:附件;文件名=\”file.xls\”;

强制下载,然后

标题(“内容类型:application/vnd.ms excel”);

要向浏览器表明您正在发送一个excel文件,并最终将该文件作为html
表格进行回显,请执行以下操作:

<?php
header("Content-Disposition: attachment; filename=\"file.xls\"");
header("Content-type:application/vnd.ms-excel");
echo "<html><table>...</table></html>";  // your table data here..
?>

Excel可以用
打开
.html
文件,因此不会出现任何问题。

您可以发送

标题(“内容处置:附件;文件名=\”file.xls\”;

强制下载,然后

标题(“内容类型:application/vnd.ms excel”);

要向浏览器表明您正在发送一个excel文件,并最终将该文件作为html
表格进行回显,请执行以下操作:

<?php
header("Content-Disposition: attachment; filename=\"file.xls\"");
header("Content-type:application/vnd.ms-excel");
echo "<html><table>...</table></html>";  // your table data here..
?>

Excel可以使用
s打开
.html
文件,因此不会出现任何问题。

请查看此项

//    reset all output buffering
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
    header('Content-type: application/ms-excel');
    header('Content-Disposition:  inline; attachment; filename='.$filename);

    // we can't send any more headers after this
    flush();

    $excel = new PhpExcel();
    $excel->setActiveSheetIndex(0);
    $sheet = $excel->getActiveSheet();
    // in this example, $data was an array in the format row => value
    //  data structure is not relevant to issue
    foreach ($data as $key => $value) {
         // add data to sheet here
         $sheet->SetCellValue('A' . $key, $value);
         // etc...
    }
    $writer = new PHPExcel_Writer($excel);
    // push to browser
    $writer->save('php://output');
看看这个

//    reset all output buffering
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
    header('Content-type: application/ms-excel');
    header('Content-Disposition:  inline; attachment; filename='.$filename);

    // we can't send any more headers after this
    flush();

    $excel = new PhpExcel();
    $excel->setActiveSheetIndex(0);
    $sheet = $excel->getActiveSheet();
    // in this example, $data was an array in the format row => value
    //  data structure is not relevant to issue
    foreach ($data as $key => $value) {
         // add data to sheet here
         $sheet->SetCellValue('A' . $key, $value);
         // etc...
    }
    $writer = new PHPExcel_Writer($excel);
    // push to browser
    $writer->save('php://output');

接收到的响应显示如何处理提供文件供下载的成功响应,或由js处理的失败响应(例如,显示在当前html页面中)在没有mime类型问题的情况下,

接受的响应显示了如何处理提供文件供下载的成功响应,或由js处理的失败响应(例如,显示在当前html页面中)如果没有mime类型的问题,你能说得更具体一点吗?@IsaacGonzalez这有意义吗?好的,我现在明白了,但问题是,由于我对文件中的内容做了一些其他操作,我不会在不重新执行大量代码的情况下将所有内容输出为
。+1对于你的帮助,你能说得更具体一点吗ease?@IsaacGonzalez这样做有意义吗?好吧,我现在明白了,但问题是,由于我对文件中的内容执行了其他一些操作,所以我不会在不重新执行大量代码的情况下将所有内容输出为
。+1谢谢你的帮助,这个答案是否可以跨多个浏览器兼容?我刚刚使用了Ajax和jQueryNo guarantees-我不是js或浏览器专家,但这是我能想到的最好的答案,看起来它应该能工作。谢谢,我一定会尝试,我会让你知道这个答案能兼容多个浏览器吗?我刚刚使用了Ajax和jQueryNo保证-我不是js或浏览器专家,但这是我能想到的最好的答案nd看起来应该有用谢谢我一定会试试的我会告诉你的