简体   繁体   English

PHPexcel问题

[英]PHPexcel issues

OK, now I read examples of phpexcel. 好的,现在我阅读了phpexcel的示例。 As I understand there shouldn't be any other output on page, to make proper xls-file. 据我了解,在页面上不应有任何其他输出来制作适当的xls文件。 I think so, because when I have 我这么认为,因为当我有

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";

And get xls, it has warnings that it doen't have css links there and moreover it puts a lot of text except Hello world!, such as links in excel file. 并得到xls,它警告说那里没有css链接,而且除了Hello world!之外,它还会放很多文本,例如excel文件中的链接。 It looks like this: 看起来像这样: 在Excel中输出

I can put all work with xls in other php file and get it with ajax, but how can I give results of query to this file? 我可以将所有与xls一起工作的文件放在其他php文件中,并用ajax进行获取,但是如何给该文件查询结果呢? All queries should be in my main file, because I don't want to have multiple connections to database. 所有查询都应该在我的主文件中,因为我不想与数据库建立多个连接。 How it is clear, what I want. 很清楚,我想要什么。 Any advices? 有什么建议吗?

To download xls(x) file, you have to set right headers and clean the buffer to avoid bad stream. 要下载xls(x)文件,您必须设置正确的标题并清理缓冲区,以避免流损坏。 As documentation explains you can try with this code: 如文档所述,您可以尝试使用以下代码:

//------------- 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');

You can refer here for the Developers Documentation of PHPExcel 您可以在这里参考PHPExcel的开发人员文档。

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

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