Because a few days ago wrote an article, with the Php-excel-reader class to import Excel content, by the way some Excel export problem, I use simple Excel, a very easy to export XLS class, especially good!
Simple Excel source code is as follows:
Copy Code code 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:o Ffice:spreadsheet\ "xmlns:x=\" urn:schemas-microsoft-com:office:excel\ "xmlns:ss=\" Urn:schemas-microsoft-com:o Ffice: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;
}
}
?>
Use the PHP case as follows:
Copy Code code 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 dot-dot pass ', ' www.phpddt.com '),
3 => Array (' Baidu ', ' www.baidu.com ')
);
$xls->addarray ($data);
$xls->generatexml (' name4test ');
?>
The results of the export are shown below: