- class excel{
- /**
- *頭的excel文件(前綴的行)
- *
- *從excel復制的xml規格。
- *
- * @訪問私有
- * @無功串
- */
- 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">";
- /**
- *頁腳的excel文件(附加到行)
- *
- *從excel復制的xml規格。
- *
- * @訪問私有
- * @無功串
- */
- var $footer = "</workbook>";
- /**
- * document lines (rows in an array)
- *
- * @access private
- * @var array
- */
- var $lines = array ();
- /**
- 工作表名稱
- *
- *包含一個單一的工作表名稱
- *
- * @訪問私有
- * @無功串
- */
- var $worksheet_title = "table1";
- /**
- 添加一個單行的文檔字符串$
- *
- * @訪問私有
- * @帕拉姆庫馬拉陣列一維陣列
- * @待辦事項行創造應做減本-> addarray
- */
- function addrow ($array) {
- // initialize all cells for this row
- $cells = "";
- // foreach key -> write value into cells
- foreach ($array as $k => $v):
- // 加個字符串與數字的判斷 避免生成的 excel 出現數字以字符串存儲的警告
- if(is_numeric($v)) {
- // 防止首字母為 0 時生成 excel 后 0 丟失
- 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> ";
- }
- /**
- *添加一個數組到文檔
- *
- *這應該是唯一的方法需要生成一個excel
- *文件。
- *
- * @訪問公開
- * @帕拉姆庫馬拉數組二維數組
- * @待辦事項可以轉移到__construct()稍后
- */
- function addarray ($array) {
- // run through the array and add them into rows
- foreach ($array as $k => $v):
- $this->addrow ($v);
- endforeach;
- }
- /**
- 設置工作表名稱
- *
- *檢查的字符串不允許字符(: /?*),
- *削減它的最大31個字符,并設置標題。該死
- *為何未允許字符無處可尋?視窗
- *幫助沒有幫助...
- *
- * @訪問公開
- * @帕拉姆庫馬拉字符串$標題設計標題
- */
- function setworksheettitle ($title) {
- // strip out special chars first
- $title = preg_replace ("/[/|:|/|?|*|[|]]/", "", $title);
- // now cut it to the allowed length
- $title = substr ($title, 0, 31);
- // set title
- $this->worksheet_title = $title;
- }
- /**
- *生成excel文件
- *
- *最后生成的excel文件,并使用header()函數
- *提供給瀏覽器。
- *
- * @訪問公開
- * @帕拉姆庫馬拉字符串$文件名名稱的excel文件來生成(... 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教程lashes for the damn ">"
- echo stripslashes ($this->header);
- echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";//開源代碼Vevb.com
- 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");
新聞熱點
疑難解答