This article illustrates the use of the Class library for exporting Excel data in PHP. Share to everyone for your reference, specific as follows:
Today, a project to do a PHP export data saved in Excel, found on the internet was originally intended to use Phpexcel, and later found it too difficult to change a but the song is the export of XML
Class is simple to write, but very practical. You can simply export strings and numbers in two formats.
If you are interested, you can use it to expand, basically enough.
Class Excel_xml {//set to private variable, top label private $header = "<?xml version=\" 1.0\ "encoding=\"%s\ "\>\n<workbook" 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>";
Set to row array private $lines = Array ();
Set encoding 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 encoding, the default thing in the constructor UTF-8 format public Function setencoding ($sEncoding) {$this->sencoding = $sEncoding;}//Set up Excel header Pub Lic function Setworksheettitle ($title) {$title = Preg_replace ("/[\\\|:| \/|\?|
\*|\[|\]]/"," ", $title); $title = substr ($title, 0, 31);
$this->sworksheettitle = $title; }//Add line function (key function) Private function addrow ($array) {$cells = ""; Set each cell to be an empty foreach ($array as $k => $v) {$type = ' string ';//The default type is string if ($this->bconverttypes = = True && ;
Is_numeric ($v))://Judge 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 XML PU Blic function generatexml ($filename = ' Excel-export ') {$filename = preg_replace ('/[^aa-zz0-9\_\-]/', ', $filename); hea Der ("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, is to put the data array, read out, and then use the XML tag, in the 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 ');
$xls->addarray ($data) of the instantiation function;
$xls->generatexml (' School '); Export and set name
}
Above yes write an export method. The keys and values in the array $this->team are already exported while the viewer is running.
More interested in PHP related content readers can view the site topics: "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP Array" operation Skills Encyclopedia, "PHP Sorting algorithm Summary", " PHP commonly used traversal algorithm and skills summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage Summary" A summary of common PHP database operation techniques
I hope this article will help you with the PHP program design.