VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php生成excel文件源代码

  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> ";//开源代码phpfensi.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");
 
出处:http://www.phpfensi.com/php/20140911/5419.html

相关教程