- 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):
-
-
- if(is_numeric($v)) {
-
- 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;
-
- $this->lines[] = "<row> " . $cells . "</row> ";
- }
-
-
-
-
-
-
-
-
-
-
- function addarray ($array) {
-
- foreach ($array as $k => $v):
- $this->addrow ($v);
- endforeach;
- }
-
-
-
-
-
-
-
-
-
-
-
- function setworksheettitle ($title) {
-
- $title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);
-
- $title = substr ($title, 0, 31);
-
- $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 stripslashes ($this->header);
- echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";
- echo "<column ss:index="1" ss:autofitwidth="0" ss:width="110"/> ";
- echo implode (" ", $this->lines);
- echo "</table> </worksheet> ";
- echo $this->footer;
- }
- }
cakephp中使用方法
注意:cakephp 配置文件 define('debug', 0);
- vendor ('excel');
- $doc = array (
- 0 => array ('中国', '中国人', '中国人民', '123456');
- );
- $xls = new excel;
- $xls->addarray ( $doc );
- $xls->generatexml ("mytest");
非框架使用方法,实例代码如下:
- require_once('excel.php');
- $doc = array (
- 0 => array ('中国', '中国人', '中国人民', '123456');
- );
- $xls = new excel;
- $xls->addarray ( $doc );
- $xls->generatexml ("mytest");
|