PHP export Excel file, the first step is to implement the PHP template export without data

Source: Internet
Author: User
Tags echo date php template

Today continue to study PHP export Excel file, the complex things to simplify, step by step implementation of the function, the first realization of the template file export, then the implementation of the data after the export, the final realization of the function, this is the basic idea. You can add a step in the middle, first write your own data export test, and then the database import. I first put the program submitted to the self-built eubexcel.php file, choose Post submission, export Excel file program on this page to write, refer to the PHPExcel-1.8 component of yesterday downloaded reference document, first deploy export Excel, the specific code is as follows

<?php

Error_reporting (E_all);
Ini_set (' display_errors ', TRUE);
Ini_set (' display_startup_errors ', TRUE);
Date_default_timezone_set (' Europe/london ');

if (Php_sapi = = ' CLI ')
Die (' This example should only is run from a Web Browser ');

/** Include Phpexcel */
Require_once dirname (__file__). ‘/.. /plugins/phpexcel-1.8/classes/phpexcel.php ';


Create New Phpexcel Object
$objPHPExcel = new Phpexcel ();

Read from Excel2007 (. xlsx) template
echo Date (' H:i:s '), "Load Excel5 template file", EOL;
$objReader = Phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = $objReader->load (".. /template/eub.xls ");
Print_r ($objPHPExcel);
Die ();

$filename = "eub-". Date ("Ymdhis"). ". XLS ";

Redirect output to a client ' s Web browser (Excel2007)
Header (' content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset= ' GB2312 "');
Header (' Content-disposition:attachment;filename= '. $filename. ' ');
Header (' cache-control:max-age=0 ');
If you ' re serving to IE 9 and then the following is needed
Header (' cache-control:max-age=1 ');

If you ' re serving to IE over SSL and then the following is needed
Header (' Expires:mon, 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

$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
$objWriter->save (' php://output ');
Exit
?>

In the meantime encountered a problem code in the Createreader (' Excel5 '), the code is about Phpexcel import different versions of Excel settings, I used the template file is (97-2003 worksheet) is a 2003 ago version, 2003 before the Excel read instantiation Excel5 class, 2003 need to instantiate excel2007 class, so, to change the $objreader instantiation of the place: where createreader(' Excel2007 ' ) instead of createreader(' Excel5 ')

PHP export Excel file, the first step is to implement the PHP template export without data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.