PHP execel Export XML Program

Source: Internet
Author: User

PHP execel Export XML Program

<?php

Class Excel_xml
{


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

Private $footer = "</Workbook>";

Private $lines = Array ();


Private $worksheet _title = "Table1";

Private Function AddRow ($array)
{

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

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

$cells. = "<cell><data ss:type=" String ">". Utf8_encode ($v). "</data></cell>n";

Endforeach;

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

}

Public Function AddArray ($array)
{

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

}


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

}

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 stripslashes for the damn ">"
Echo stripslashes ($this->header);
echo "N<worksheet ss:name=" ". $this->worksheet_title. "" >n<table>n ";
echo "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>n";
echo Implode ("n", $this->lines);
echo "</table>n</worksheet>n";
Echo $this->footer;

}

}

?>

Instance

<?php

Include the Php-excel class
Require (DirName (__file__). "/class-excel-xml.inc.php");

Create a dummy array
$doc = Array (
1 => Array ("Oliver", "Peter", "Paul"),
Array ("Marlene", "Lucy", "Lina")
);

Generate Excel File
$xls = new Excel_xml;
$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.