Example of a class library usage for exporting Excel data from PHP

Source: Internet
Author: User
Tags php programming
The example in this article describes the class library usage of PHP for exporting Excel data. Share to everyone for your reference, as follows:

Today a project to do a PHP export data with Excel to save, found on the internet is originally intended to use Phpexcel, and later found too difficult, and changed a but the export of the song is XML

The class is very simple to write, but very practical. You can simply export strings and numbers in two formats.

If you are interested, you can expand it, basically enough.

Class excel_xml{//is set to private variable, top label 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\ ">";//Bottom Label private $ Footer = "</Workbook>";//fixed to row array private $lines = Array ();//Set code private $sEncoding;//set type private $bConvertTypes;// Set sheet name private $sWorksheetTitle;//constructor Public function __construct ($sEncoding = ' UTF-8 ', $bConvertTypes = false,$ Sworksheettitle = ' Table1 ') {$this->bconverttypes = $bConvertTypes; $this->setencoding ($sEncoding); $this Setworksheettitle ($sWorksheetTitle);} Set the encoding, the default thing inside the constructor UTF-8 format public Function setencoding ($sEncoding) {$this->sencoding = $sEncoding;} Set the header public function for Excel Setworksheettitle ($title) {$title = Preg_replace ("/[\\\|:| \/|\?| \*|\[|\]]/"," ", $title); $title = substr ($title, 0, +); $this->sworksheettitle = $title;} Add row function (key function) Private function AddRow ($array) {$cells = ""; Set each cell to an empty foreach ($array as $k + = $v) {$type = ' string ';//The default type is the string if ($this->bconverttypes = = = True && is _numeric ($v))://judgment type {$type = ' number ';} $v = Htmlentities ($v, Ent_compat, $this->sencoding); $cells. = "<cell><data ss:type=\" $type \ ">". $v. "</data></cell>\n";} $this->lines[] = "<row>\n". $cells. "</row>\n"; Write array}//add array public function AddArray ($array) {foreach ($array as $k = = $v) {$this->addrow ($v);}} Export Xmlpublic 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 principle is very simple, that is, the data array, read out, and then with the XML tag sealed, in PHP with the header () function to tell the browser, it can be.

Call:

Public Function Import () {$data = array (1 = = Array (' School name ', "team Name")), foreach ($this->team as $key + = $value) {
  
   array_push ($data, Array ($key, $value)); } $xls = new Excel_xml (' UTF-8 ', false, ' My Test Sheet '); The instantiation function $xls->addarray ($data); $xls->generatexml (' School '); Export and set name}
  

Above yes write an export method. The keys and values inside the array $this->team have been exported while the viewer is running.

I hope this article is helpful to you in PHP programming.

Related Article

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.