PHP generates Excel file source code

Source: Internet
Author: User
Tags foreach xmlns
The code is as follows Copy Code

Class excel{

/**
* Header Excel file (line with prefix)
*
* XML specifications copied from Excel.
*
* @ Access Private
* @ var series
*/
var $header = "<?xml version=" 1.0 "encoding=" Utf-8 "?>"
<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 Excel file (attached to line)
*
* XML specifications copied from Excel.
*
* @ Access Private
* @ var series
*/
var $footer = "</workbook>";

/**
* Document lines (rows in an array)
*
* @access Private
* @var Array
*/
var $lines = array ();

/**
Sheet name
*
* contains a single sheet name
*
* @ Access Private
* @ var series
*/
var $worksheet _title = "Table1";

/**
Add a single line of document string $
*
* @ Access Private
* @ Palamkumara Array one-dimensional array
@ To-do line creation should be done to reduce the-> AddArray
*/
function AddRow ($array) {

Initialize all cells to this row
$cells = "";

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

Add a string and a number to avoid the generated Excel appears with a warning that the number is stored as a string
if (Is_numeric ($v)) {
Prevent 0 loss After Excel is generated from the first letter 0 o'clock
if (substr ($v, 0, 1) = 0) {
$cells. = "<cell><data ss:type=" string ">". $v. "</data></cell>";
} else {
$cells. = "<cell><data ss:type=" number ">". $v. "</data></cell>";
}
} else {
$cells. = "<cell><data ss:type=" string ">". $v. "</data></cell>";
}

Endforeach;

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

}

/**
* Add an array to the document
*
* This should be the only way to generate an Excel
File
*
* @ Access Public
* @ Palamkumara Array of two dimensions
@ To-do 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 sheet name
*
* Check the string does not allow characters (:/?*),
* Cut it to a maximum of 31 characters and set the title. Damn it
* Why characters are not allowed to be found anywhere? Window
* Help doesn't help ...
*
* @ Access Public
* @ Palamkumara string $ title Design title
*/
function Setworksheettitle ($title) {

Strip out Special chars
$title = Preg_replace ("/[\|:| /|?| *| [|]] /"," ", $title);

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

Set Title
$this->worksheet_title = $title;

}

/**
* Generate Excel File
*
* The last generated Excel file and use the header () function
* provided to the browser.
*
* @ Access Public
* @ Palamkumara string $ file name Excel file 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 "<worksheet ss:name=" ". $this->worksheet_title. "> <table>";
echo "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>";
echo Implode ("", $this->lines);
echo "</table> </worksheet>";
Echo $this->footer;

}

}

/**
* Use method of cake
* Note * * Cake configuration file define (' Debug ', 0);
*
* Vendor (' Excel ');
* $doc = Array (
* 0 => Array (' China ', ' Chinese ', ' Chinese People ', ' 123456 ');
*  );
* $xls = new Excel;
* $xls->addarray ($doc);
* $xls->generatexml ("mytest");
*/

/**
* Non-frame use method
*
* require_once (' excel.php ');
* $doc = Array (
* 0 => Array (' China ', ' Chinese ', ' Chinese People ', ' 123456 ');
*  );
* $xls = new Excel;
* $xls->addarray ($doc);
* $xls->generatexml ("mytest");
*/

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.