PHP generates Excel files

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

Class excel{


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

var $footer = "</workbook>";
var $lines = array ();
var $worksheet _title = "Table1";

function AddRow ($array) {


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

}

function AddArray ($array) {

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

}

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) {


Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header ("Content-disposition:inline; Filename= "". $filename. ". xls" ");

echo Strips Tutorial Lashes ($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;

}

}


/**
* 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");
*/

?>

Method Two
In fact, when the real application, you can take the data from the database tutorial, and then the end of each column of data plus t, after each row of data plus n after the method echo out, in the beginning of PHP with header ("content-type:application/ Vnd.ms-excel "); indicates that the output is an Excel file, with the header (" Content-disposition:filename=test.xls "); The file name for the output is Text.xls. That's OK.

The code is as follows Copy Code

?

Header ("Content-type:application/vnd.ms-excel");

Header ("Content-disposition:filename=test.xls");

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

echo "Test1";

echo "Test2";

?>

Method Three

?
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_excel_gshjsl.xls");

$tx = ' table Head ';
echo $tx. " ";
echo "Number". " ";
echo "Name". " ";
echo "";

echo "=" 411481198507150666 "". " ";
echo "=" 0123456 "". " ";
echo "";
?>

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.