国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php生成excel文件源代碼

2024-09-04 11:44:18
字體:
來源:轉載
供稿:網友
  1. class excel{ 
  2.     /** 
  3.      *頭的excel文件(前綴的行) 
  4.      * 
  5.      *從excel復制的xml規格。 
  6.      * 
  7.      * @訪問私有 
  8.      * @無功串 
  9.      */ 
  10.     var $header = "<?xml version="1.0" encoding="utf-8"?> 
  11. <workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
  12.  xmlns:x="urn:schemas-microsoft-com:office:excel" 
  13.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
  14.  xmlns:html="http://www.w3.org/tr/rec-html40">"; 
  15.     /** 
  16.      *頁腳的excel文件(附加到行) 
  17.      * 
  18.      *從excel復制的xml規格。 
  19.      * 
  20.      * @訪問私有 
  21.      * @無功串 
  22.      */ 
  23.     var $footer = "</workbook>"
  24.     /** 
  25.      * document lines (rows in an array) 
  26.      *  
  27.      * @access private 
  28.      * @var array 
  29.      */ 
  30.     var $lines = array (); 
  31.     /** 
  32.      工作表名稱 
  33.      * 
  34.      *包含一個單一的工作表名稱 
  35.      * 
  36.      * @訪問私有 
  37.      * @無功串 
  38.      */ 
  39.     var $worksheet_title = "table1"
  40.     /** 
  41.   添加一個單行的文檔字符串$ 
  42.      * 
  43.      * @訪問私有 
  44.      * @帕拉姆庫馬拉陣列一維陣列 
  45.      * @待辦事項行創造應做減本-> addarray 
  46.      */ 
  47.     function addrow ($array) { 
  48.         // initialize all cells for this row 
  49.         $cells = ""
  50.          
  51.         // foreach key -> write value into cells 
  52.         foreach ($array as $k => $v): 
  53.     
  54.          // 加個字符串與數字的判斷 避免生成的 excel 出現數字以字符串存儲的警告 
  55.          if(is_numeric($v)) { 
  56.           // 防止首字母為 0 時生成 excel 后 0 丟失 
  57.           if(substr($v, 0, 1) == 0) { 
  58.            $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> "
  59.           } else { 
  60.            $cells .= "<cell><data ss:type="number">" . $v . "</data></cell> "
  61.           } 
  62.          } else { 
  63.              $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> "
  64.          } 
  65.         endforeach
  66.         // transform $cells content into one row 
  67.         $this->lines[] = "<row> " . $cells . "</row> "
  68.     } 
  69.     /** 
  70.     *添加一個數組到文檔 
  71.      * 
  72.      *這應該是唯一的方法需要生成一個excel 
  73.      *文件。 
  74.      * 
  75.      * @訪問公開 
  76.      * @帕拉姆庫馬拉數組二維數組 
  77.      * @待辦事項可以轉移到__construct()稍后 
  78.      */ 
  79.     function addarray ($array) { 
  80.         // run through the array and add them into rows 
  81.         foreach ($array as $k => $v): 
  82.             $this->addrow ($v); 
  83.         endforeach
  84.     } 
  85.     /** 
  86.     設置工作表名稱 
  87.      * 
  88.      *檢查的字符串不允許字符(: /?*), 
  89.      *削減它的最大31個字符,并設置標題。該死 
  90.      *為何未允許字符無處可尋?視窗 
  91.      *幫助沒有幫助... 
  92.      * 
  93.      * @訪問公開 
  94.      * @帕拉姆庫馬拉字符串$標題設計標題 
  95.      */ 
  96.     function setworksheettitle ($title) { 
  97.         // strip out special chars first 
  98.         $title = preg_replace ("/[/|:|/|?|*|[|]]/"""$title); 
  99.         // now cut it to the allowed length 
  100.         $title = substr ($title, 0, 31); 
  101.         // set title 
  102.         $this->worksheet_title = $title
  103.     } 
  104.    /** 
  105.      *生成excel文件 
  106.      * 
  107.      *最后生成的excel文件,并使用header()函數 
  108.      *提供給瀏覽器。 
  109.      * 
  110.      * @訪問公開 
  111.      * @帕拉姆庫馬拉字符串$文件名名稱的excel文件來生成(... xls)中 
  112.      */ 
  113.     function generatexml ($filename) { 
  114.         // deliver header (as recommended in php manual) 
  115.         header("content-type: application/vnd.ms-excel; charset=utf-8"); 
  116.         header("content-disposition: inline; filename="" . $filename . ".xls""); 
  117.         // print out document to the browser 
  118.         // need to use strips教程lashes for the damn ">" 
  119.         echo stripslashes ($this->header); 
  120.         echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";//開源代碼Vevb.com 
  121.         echo "<column ss:index="1" ss:autofitwidth="0" ss:width="110"/> "
  122.         echo implode (" "$this->lines); 
  123.         echo "</table> </worksheet> "
  124.         echo $this->footer; 
  125.     } 

cakephp中使用方法

注意:cakephp 配置文件 define('debug', 0);

  1. vendor ('excel'); 
  2. $doc = array ( 
  3.      0 => array ('中國''中國人''中國人民''123456'); 
  4. ); 
  5. $xls = new excel; 
  6. $xls->addarray ( $doc ); 
  7. $xls->generatexml ("mytest"); 

非框架使用方法,實例代碼如下:

  1. require_once('excel.php'); 
  2. $doc = array ( 
  3.      0 => array ('中國''中國人''中國人民''123456'); 
  4. ); 
  5. $xls = new excel; 
  6. $xls->addarray ( $doc ); 
  7. $xls->generatexml ("mytest");

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丽水市| 措美县| 阿巴嘎旗| 沙田区| 芦山县| 乌拉特后旗| 康定县| 普定县| 阿鲁科尔沁旗| 泰州市| 吉首市| 光山县| 绍兴市| 文水县| 仲巴县| 德清县| 凤山县| 博客| 道孚县| 镇雄县| 南涧| 尖扎县| 阿城市| 株洲市| 罗甸县| 井陉县| 东方市| 泉州市| 兴文县| 蕉岭县| 武宣县| 东丰县| 娱乐| 乐业县| 淮南市| 新宾| 安乡县| 福海县| 双城市| 齐齐哈尔市| 水富县|