How to export xls in Simple Excel

Source: Internet
Author: User

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 simple excel to export some excel files. This is a simple export xls class, very useful!
Simple excel source code is as follows: Copy codeThe Code is as follows: <? Php
/**
* Simple excel generating from PHP5
*
* @ Package Utilities
* @ License http://www.opensource.org/licenses/mit-license.php
* @ Author Oliver Schwarz <oliver.schwarz@gmail.com>
* @ Version 1.0
*/
Class Excel_Xml
{
Private $ header = "<? Xml version = "1.0 \" encoding = \ "% s \"? \> \ N <Workbook xmlns = \ "urn: schemas-microsoft-com: office: spreadsheet \" xmlns: x = \ "urn: schemas-microsoft-com: office: excel \ "xmlns: ss = \" urn: schemas-microsoft-com: office: spreadsheet \ "xmlns: html = \" http://www.w3.org/TR/REC-html40\ "> ";
Private $ footer = "</Workbook> ";
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. = "<Cell> <Data ss: Type = \" $ type \ ">". $ v. "</Data> </Cell> \ n ";
Endforeach;
$ This-> lines [] = "<Row> \ n". $ cells. "</Row> \ 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 <Worksheet ss: Name = \" ". $ this-> sWorksheetTitle." \ "> \ n <Table> \ n ";
Foreach ($ this-> lines as $ line)
Echo $ line;
Echo "</Table> \ n </Worksheet> \ n ";
Echo $ this-> footer;
}
}
?>

The Use Case of php is as follows:Copy codeThe Code is as follows: <? Php
/**
* @ 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:

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.