PHP full example program for exporting excel files

Source: Internet
Author: User

Php exeel. class. php file

The code is as follows: Copy code

<? 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
*/

/**
* Generating excel documents on-the-fly from PHP5
 *
* Uses the excel XML-specification to generate a native
* XML document, readable/processable by excel.
 *
* @ Package Utilities
* @ Subpackage Excel
* @ Author Oliver Schwarz <oliver.schwarz@vaicon.de>
* @ Version 1.1
 *
* @ Todo Issue #4: Internet Explorer 7 does not work well with the given header
* @ Todo Add option to give out first line as header (bold text)
* @ Todo Add option to give out last line as footer (bold text)
* @ Todo Add option to write to file
*/
Class Excel_XML
{

/**
* Header (of document)
* @ Var string
*/
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"> ";

/**
* Footer (of document)
* @ Var string
*/
Private $ footer = "</Workbook> ";

/**
* Lines to output in the excel document
* @ Var array
*/
Private $ lines = array ();

/**
* Used encoding
* @ Var string
*/
Private $ sEncoding;
       
/**
* Convert variable types
* @ Var boolean
*/
Private $ bConvertTypes;
       
/**
* Worksheet title
* @ Var string
*/
Private $ sWorksheetTitle;

/**
* Constructor
         *
* The constructor allows the setting of some additional
* Parameters so that the library may be configured
* One's needs.
         *
* On converting types:
* When set to true, the library tries to identify the type
* The variable value and set the field specification for Excel
* Accordingly. Be careful with article numbers or postcodes
* Starting with a '0' (zero )!
         *
* @ Param string $ sEncoding Encoding to be used (defaults to UTF-8)
* @ Param boolean $ bConvertTypes Convert variables to field specification
* @ Param string $ sWorksheetTitle Title for the worksheet
*/
Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
        {
$ This-> bConvertTypes = $ bConvertTypes;
$ This-> setEncoding ($ sEncoding );
$ This-> setWorksheetTitle ($ sWorksheetTitle );
        }
       
/**
* Set encoding
* @ Param string Encoding type to set
*/
Public function setEncoding ($ sEncoding)
        {
$ This-> sEncoding = $ sEncoding;
        }

/**
* Set worksheet title
         *
* Strips out not allowed characters and trims
* Title to a maximum length of 31.
         *
* @ Param string $ title Title for worksheet
*/
Public function setWorksheetTitle ($ title)
        {
$ Title = preg_replace ("/[\ |: |/|? | * | [|]/"," ", $ Title );
$ Title = substr ($ title, 0, 31 );
$ This-> sWorksheetTitle = $ title;
        }

/**
* Add row
         *
* Adds a single row to the document. If set to true, self: bConvertTypes
* Checks the type of variable and returns the specific field settings
* For the cell.
         *
* @ Param array $ array One-dimen1_array with row content
*/
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 ";
        }

/**
* Add an array to the document
* @ Param array 2-dimen1_array
*/
Public function addArray ($ array)
        {
Foreach ($ array as $ k => $ v)
$ This-> addRow ($ v );
        }


/**
* Generate the excel file
* @ Param string $ filename Name of excel file to generate (...xls)
*/
Public function generateXML ($ filename = 'Excel-export ')
        {
// Correct/validate filename
$ Filename = preg_replace ('/[^ aA-zZ0-9 _-]/', '', $ filename );
     
// Deliver header (as recommended in php manual)
Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
Header ("Content-Disposition: inline; filename =" ". $ filename.". xls "");
// Print out document to the browser
// Need to use stripslashes for the damn ">"
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;
        }

}
?>

Excel. class. Php file

The code is as follows: Copy code

<? Php
/* Function: export the excel class
* Date: January 1, August 16, 2012
* By: 565990136@qq.com
*/
Class myexcel {
Private $ filelds_arr1 = array ();
Private $ filelds_arr2 = array ();
Private $ filename;
// Load library
Function _ construct ($ fields_arr1, $ fields_arr2, $ filename = 'test '){
$ This-> filelds_arr1 = $ fields_arr1;
$ This-> filelds_arr2 = $ fields_arr2;
$ This-> filename = $ filename;
 }
Function putout (){
Require 'php-excel. class. Php ';

$ Datavalue = $ this-> filelds_arr2;
$ Newdata [0] = $ this-> filelds_arr1;
$ Arrcount = count ($ datavalue );
For ($ I = 1; $ I <= $ arrcount; $ I ++ ){
$ Newdata [$ I] = array_values ($ datavalue [$ i-1]);
}


$ Filename = $ this-> filename;
$ Xls = new Excel_XML ('utf-8', false, 'My Test Sheet ');
$ Xls-> addArray ($ newdata );
$ Xls-> generateXML ($ filename );
}
}

/*
** Usage example (note that the exported file name can only be in English)

$ Asdasd = new myexcel (array ('name', 'phone'), array ('Jia Xinming ', '123'), 'ABC ');
$ Asdasd-> putout ();
*/
?>

Note: We save the above code as two files and then perform the operation.

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.