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