Using simple Excel to export XLS implementation method _php example

Source: Internet
Author: User
Tags xmlns
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:

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.