0

我已经用 phpexcel 创建了一个 excel 文件并成功地将它保存在服务器上。但我真正想做的是让浏览器即时下载这个文件。有什么解决办法吗?
这是我的代码:

$objPHPExcel = new PHPExcel();

        $objPHPExcel->setActiveSheetIndex(0);


        $col = 0;
        foreach ($json[0] as $key => $value) {
            $a = $key;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $key);
            $col++;
        }

        $row = 2;
        foreach ($json as $itemGroup) {
            $col = 0;
            foreach ($itemGroup as $key => $value) {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
                $col++;
            }
            $row++;
        }

        $objPHPExcel->getActiveSheet()->setTitle('Report');

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        try {
            $fileName = date("Y-m-d H:i:s");
            $objWriter->save('/uploaded_images/generatedReports/' . $fileName . '.xlsx');
            return true;
        } catch (Exception $exc) {
            return false;
        }
4

5 回答 5

1

查看示例 01simple-download-xlsx.php,它显示了要设置的标头,并且您应该保存到 php://output 而不是“命名”文件

于 2013-02-05T15:09:52.370 回答
0
header('Location : /uploaded_images/generatedReports/' . $fileName . '.xlsx');
于 2013-02-05T10:12:57.870 回答
0

尝试使用附件标题?

header("Content-Disposition: attachment; filename=your_file.xls");
于 2013-02-05T10:14:05.857 回答
0
Berofe returning true in try block you have to use header method for downloadable action

$filename ="excelreport.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo "your content \t you next tabcontent \n your next line content \t"
于 2013-02-05T10:16:47.870 回答
0

您可以设置适当的标题,并使用 'php://stdout' 作为输出文件名。

例如

$filename = date("Y-m-d H:i:s").'.xls';
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
$objWriter->save('php://stdout');
return true;
于 2013-02-05T10:26:30.383 回答