0

我有一个输出 Excel 文件的小 PHPExcel 脚本,问题是,当我从服务器打开它时,我可以毫无问题地打开它。但是当我通过浏览器中的标题下载它时,它不起作用。

我在互联网上搜索了这个问题,但我没有找到任何对我有帮助的东西,我尝试使用 ob_clean();,我检查了 BOM 是否是问题,没有任何效果。

所以我希望你们中的一些人有一个想法可以让我的脚本工作。

$this->objPHPExcel = $this->objReader->load($this->newFilePath);
    $objWorksheet = $this->objPHPExcel->getActiveSheet();
    $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel2007');
    //Adding IO General Overhead
    foreach($this->sums as $sumkey => $sumval){
        foreach($sumval as $keys => $vals){
            foreach($vals as $key => $val){
                $objWorksheet->setCellValueByColumnAndRow($key, $offset[$sumkey][$keys], $val);
            }
        }
        $objWriter->save($this->newFilePath);
    }

    ob_clean();

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');


    // It will be called file.xls
    header('Content-Disposition: attachment; filename="'.$this->newFilePath.'"');

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
4

1 回答 1

0

查看 /Tests 或 /Examples 中的 01simple-download-xlsx.php 以获取符合您要求的代码示例:

$this->objPHPExcel = $this->objReader->load($this->newFilePath);
    $objWorksheet = $this->objPHPExcel->getActiveSheet();
    $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel2007');
    //Adding IO General Overhead
    foreach($this->sums as $sumkey => $sumval){
        foreach($sumval as $keys => $vals){
            foreach($vals as $key => $val){
                $objWorksheet->setCellValueByColumnAndRow($key, $offset[$sumkey][$keys], $val);
            }
        }
    }


    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');


    // It will be called file.xls
    header('Content-Disposition: attachment; filename="'.$this->newFilePath.'"');

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

        $objWriter->save('php://output');
于 2013-02-25T16:00:02.303 回答