繁体   English   中英

PHPexcel问题

[英]PHPexcel issues

好的,现在我阅读了phpexcel的示例。 据我了解,在页面上不应有任何其他输出来制作适当的xls文件。 我这么认为,因为当我有

echo "some output and my form for query";
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setCreator("Administrator");
            $objPHPExcel->getProperties()->setLastModifiedBy("Administrator");
            $objPHPExcel->getProperties()->setTitle("test");
            $objPHPExcel->getProperties()->setSubject("test");
            $objPHPExcel->getProperties()->setDescription("test");

            $objPHPExcel->setActiveSheetIndex(0);
            $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
            $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
            $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
            $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

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

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            // Redirect output to a client’s web browser (Excel5)
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="01simple.xls"');
            header('Cache-Control: max-age=0');
            // If you're serving to IE 9, then the following may be needed
            header('Cache-Control: max-age=1');

            // If you're serving to IE over SSL, then the following may be needed
            header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
            header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header ('Pragma: public'); // HTTP/1.0
echo"some other code";

并得到xls,它警告说那里没有css链接,而且除了Hello world!之外,它还会放很多文本,例如excel文件中的链接。 看起来像这样: 在Excel中输出

我可以将所有与xls一起工作的文件放在其他php文件中,并用ajax进行获取,但是如何给该文件查询结果呢? 所有查询都应该在我的主文件中,因为我不想与数据库建立多个连接。 很清楚,我想要什么。 有什么建议吗?

要下载xls(x)文件,您必须设置正确的标题并清理缓冲区,以避免流损坏。 如文档所述,您可以尝试使用以下代码:

//------------- Enter your code before this line --------- //

// Cleaning buffer
ob_clean();

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

/* Or you can use 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
*/

// redirect output to client browser
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_filename.xls"');
/* Use this for Writer Excel2007     
header('Content-Disposition: attachment;filename="your_filename.xlsx"');
*/
header('Cache-Control: max-age=0');

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

您可以在这里参考PHPExcel的开发人员文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM