简体   繁体   中英

PHPexcel issues

OK, now I read examples of phpexcel. As I understand there shouldn't be any other output on page, to make proper xls-file. 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. 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? 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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