How to export xls using SimpleExcel _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use SimpleExcel to export xls. I wrote an article a few days ago and used the php-excel-reader class to import the excel content. By the way, I used simpleexcel to export some excel files, which is a simple export xls class, very useful! S. I wrote an article a few days ago and used the php-excel-reader class to import the excel content. By the way, I used the simple excel class to export the xls content, very useful!
Simple excel source code is as follows:

The code is as follows:


/**
* Simple excel generating from PHP5
*
* @ Package Utilities
* @ License http://www.opensource.org/licenses/mit-license.php
* @ Author Oliver Schwarz
* @ Version 1.0
*/
Class Excel_Xml
{
Private $ header =" \ N ";
Private $ footer =" ";
Private $ lines = array ();
Private $ sEncoding;
Private $ bConvertTypes;
Private $ sWorksheetTitle;
Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
{
$ This-> bConvertTypes = $ bConvertTypes;
$ This-> setEncoding ($ sEncoding );
$ This-> setWorksheetTitle ($ sWorksheetTitle );
}
Public function setEncoding ($ sEncoding)
{
$ This-> sEncoding = $ sEncoding;
}
Public function setWorksheetTitle ($ title)
{
$ Title = preg_replace ("/[\\|:\/ | \? | \ * | \ [| \]/"," ", $ Title );
$ Title = substr ($ title, 0, 31 );
$ This-> sWorksheetTitle = $ title;
}
Private function addRow ($ array)
{
$ Cells = "";
Foreach ($ array as $ k => $ v ):
$ Type = 'string ';
If ($ this-> bConvertTypes === true & is_numeric ($ v )):
$ Type = 'number ';
Endif;
$ V = htmlentities ($ v, ENT_COMPAT, $ this-> sEncoding );
$ Cells. =" ". $ V ." \ N ";
Endforeach;
$ This-> lines [] =" \ N ". $ cells ." \ N ";
}
Public function addArray ($ array)
{
Foreach ($ array as $ k => $ v)
$ This-> addRow ($ v );
}
Public function generateXML ($ filename = 'Excel-export ')
{
$ Filename = preg_replace ('/[^ aA-zZ0-9 \ _ \-]/', '', $ filename );
Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
Header ("Content-Disposition: inline; filename = \" ". $ filename.". xls \"");
Echo stripslashes (sprintf ($ this-> header, $ this-> sEncoding ));
Echo "\ n SWorksheetTitle. "\"> \ n




\ N ";Foreach ($ this-> lines as $ line)Echo $ line;Echo"
\ N\ N ";
Echo $ this-> footer;
}
}
?>


The use case of php is as follows:

The code is as follows:


/**
* @ Author mckee
* @ Blog www.phpddt.com
*/
Require_once 'Excel. class. php ';
$ Xls = new Excel_Xml ('utf-8', false, 'test ');
$ Data = array (
1 => array ('name', 'address '),
2 => array ('php point pass', 'www .phpddt.com '),
3 => array ('Baidu ', 'www .baidu.com ')
);
$ Xls-> addArray ($ data );
$ Xls-> generateXML ('name4test ');

?>


The export result is as follows:

Export excel, a simple export xls class, very easy to use! S...

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.