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: