Php generate excel file source code _ PHP Tutorial

Source: Internet
Author: User
Php generates the source code of the excel file. Php generate excel file source code php Tutorial generate excel file source code classexcel {*** header excel file (prefix row) ** xml specifications copied from excel. ** @ Access private * @ reactive string php generate excel file source code

Php tutorials generate excel file source code

Class excel {

/**
* Header excel files (prefix rows)
*
* Xml specifications copied from excel.
*
* @ Access private
* @ Reactive string
*/
Var $ header ="

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"> ";

/**
* Excel file in the footer (appended to rows)
*
* Xml specifications copied from excel.
*
* @ Access private
* @ Reactive string
*/
Var $ footer ="";

/**
* Document lines (rows in an array)
*
* @ Access private
* @ Var array
*/
Var $ lines = array ();

/**
Worksheet name
*
* Contains a single worksheet name
*
* @ Access private
* @ Reactive string
*/
Var $ worksheet_title = "table1 ";

/**
Add a single-line document string $
*
* @ Access private
* @ Palam Kumara array one-dimensional array
* @ To-do list creation should be reduced-> addarray
*/
Function addrow ($ array ){

// Initialize all cells for this row
$ Cells = "";

// Foreach key-> write value into cells
Foreach ($ array as $ k => $ v ):

// Add a string and a number to judge to prevent the generated excel from warning that the number is stored as a string.
If (is_numeric ($ v )){
// Prevent the loss of 0 after the excel file is generated when the first letter is 0
If (substr ($ v, 0, 1) = 0 ){
$ Cells. =" ". $ V ." N ";
} Else {
$ Cells. =" ". $ V ." N ";
}
} Else {
$ Cells. =" ". $ V ." N ";
}

Endforeach;

// Transform $ cells content into one row
$ This-> lines [] =" N ". $ cells ." N ";

}

/**
* Add an array to the document
*
* This should be the only way to generate an excel
* File.
*
* @ Access public
* @ Palam Kumara array two-dimensional array
* @ To-do list can be transferred to _ construct () later
*/
Function addarray ($ array ){

// Run through the array and add them into rows
Foreach ($ array as $ k => $ v ):
$ This-> addrow ($ v );
Endforeach;

}

/**
Set the worksheet name
*
* The checked string does not allow characters (:/? *),
* Cut it to a maximum of 31 characters and set the title. Damn
* Why cannot I find any characters? Windows
* No help for help...
*
* @ Access public
* @ Palam Kumara string $ Title Design title
*/
Function setworksheettitle ($ title ){

// Strip out special chars first
$ Title = preg_replace ("/[\ |: |/|? | * | [|]/"," ", $ Title );

// Now cut it to the allowed length
$ Title = substr ($ title, 0, 31 );

// Set title
$ This-> worksheet_title = $ title;

}

/**
* Generate an excel file
*
* The Last generated excel file and the header () function
* Provided to the browser.
*
* @ Access public
* @ Palam Kumara string $ an excel file with the name of the file name to generate (... xls)
*/
Function generatexml ($ filename ){

// Deliver header (as recommended in php manual)
Header ("content-type: application/vnd. ms-excel; charset = utf-8 ");
Header ("content-disposition: inline; filename =" ". $ filename.". xls "");

// Print out document to the browser
// Need to use strips tutorial lashes for the damn ">"
Echo stripslashes ($ this-> header );
Echo "n Worksheet_title. ""> n




N ";Echo"N ";Echo implode ("n", $ this-> lines );Echo"
NN ";
Echo $ this-> footer;

}

}

/**
* How to use cakephp
* Note ** the cakephp configuration file define ('debug', 0 );
*
* Vendor ('Excel ');
* $ Doc = array (
* 0 => array ('China', 'China ');
*);
* $ Xls = new excel;
* $ Xls-> addarray ($ doc );
* $ Xls-> generatexml ("mytest ");
*/

/**
* Usage of non-framework
*
* Require_once ('Excel. php ');
* $ Doc = array (
* 0 => array ('China', 'China ');
*);
* $ Xls = new excel;
* $ Xls-> addarray ($ doc );
* $ Xls-> generatexml ("mytest ");
*/

Php Tutorial to generate excel file source code class excel {/*** header excel file (prefix row) ** Copy xml specifications from excel. ** @ Access private * @...

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.