Example of using PHP to export excel Data class library, excel class library

Source: Internet
Author: User

Example of using PHP to export excel Data class library, excel class library

This example describes how to use PHP to export excel Data class libraries. We will share this with you for your reference. The details are as follows:

Today, a project is used to export data in PHP and save it in excel. I found a project on the Internet that originally wanted to use phpexcel. Later I found it too difficult. I changed it, but the exported song is XML.

Class writing is very simple, but it is very practical. Only strings and numbers can be exported.

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

Class Excel_XML {// 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\ "> "; // The bottom label private $ footer = "</Workbook>"; // specifies the row-specific array private $ lines = array (); // sets the encoding private $ sEncoding; // set the type private $ bConvertTypes; // set the sheet name private $ sWorksheetTitle; // The constructor public f Unction _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1') {$ this-> bConvertTypes = $ bConvertTypes; $ this-> setEncoding ($ sEncoding); $ this-> setWorksheetTitle ($ sWorksheetTitle);} // sets the encoding, the default thing in the constructor is the UTF-8 format public function setEncoding ($ sEncoding) {$ this-> sEncoding = $ sEncoding;} // set the excel header public function setWorksheetTitle ($ title) {$ title = preg_replace ("/[\\|:|\/ | \? | \ * | \ [| \]/"," ", $ Title); $ title = substr ($ title, 0, 31 ); $ this-> sWorksheetTitle = $ title;} // Add a row function (key function) private function addRow ($ array) {$ cells = ""; // set each unit to null foreach ($ array as $ k => $ v) {$ type = 'string '; // The default type is string if ($ this-> bConvertTypes === true & is_numeric ($ v): // The 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 an array} // Add an 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. It is to read the data array and use the XML tag to seal it. You can use the header () function provided by php to tell the browser.

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 '); // instantiate the function $ xls-> addArray ($ data ); $ xls-> generateXML ('school '); // export and set the name}

The above is the write export method. When you run the browser, the keys and values in the array $ this-> team have been exported.

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.