当前位置:首页 > PHP教程 > php应用 > 列表

php 生成excel文件

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-11 20:35:29 浏览: 评论:0 

本文章收藏了三款php生成excel文件代码程序,第一款为比较全面的生成函数,后面二款很简单,但是不如第一款好,好了现在来看看生成excel的代码如下:

  1. class excel{ 
  2.     var $header = "<?xml version="1.0" encoding="utf-8"?>  
  3. <workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"  
  4. xmlns:x="urn:schemas-microsoft-com:office:excel"  
  5. xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"  
  6. xmlns:html="http://www.w3.org/tr/rec-html40">";    
  7.     var $footer = "</workbook>";  
  8.     var $lines = array ();     
  9.     var $worksheet_title = "table1";     
  10.      
  11.     function addrow ($array) {     
  12.     
  13.    
  14.         $cells = "";     
  15.              
  16.    
  17.         foreach ($array as $k => $v):     
  18.                  
  19.             // 加个字符串与数字的判断 避免生成的 excel 出现数字以字符串存储的警告     
  20.             if(is_numeric($v)) {     
  21.                 // 防止首字母为 0 时生成 excel 后 0 丢失     
  22.                 if(substr($v, 0, 1) == 0) {     
  23.                     $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";     
  24.                  } else {     
  25.                     $cells .= "<cell><data ss:type="number">" . $v . "</data></cell> ";     
  26.                  }     
  27.              } else {     
  28.                 $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";     
  29.              }     
  30.     
  31.         endforeach;     
  32.     
  33.         // transform $cells content into one row     
  34.         $this->lines[] = "<row> " . $cells . "</row> ";     
  35.     
  36.      }     
  37.        
  38.     function addarray ($array) {     
  39.     
  40.         // run through the array and add them into rows     
  41.         foreach ($array as $k => $v):     
  42.             $this->addrow ($v);     
  43.         endforeach;     
  44.     
  45.      }     
  46.      
  47.     function setworksheettitle ($title) {     
  48.     
  49.         // strip out special chars first     
  50.         $title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);     
  51.     
  52.         // now cut it to the allowed length     
  53.         $title = substr ($title, 0, 31);     
  54.     
  55.         // set title     
  56.         $this->worksheet_title = $title;     
  57.     
  58.      }     
  59.    
  60.     function generatexml ($filename) {     
  61.     
  62.    
  63.          header("content-type: application/vnd.ms-excel; charset=utf-8");     
  64.          header("content-disposition: inline; filename="" . $filename . ".xls"");     
  65.    
  66.         echo stripslashes ($this->header);     
  67.         echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";     
  68.         echo "<column ss:index="1" ss:autofitwidth="0" ss:width="110"/> ";     
  69.         echo implode (" ", $this->lines);     
  70.         echo "</table> </worksheet> ";     
  71.         echo $this->footer;     
  72.     
  73.      }     
  74.     
  75. } 
  76. /**  
  77. *   非框架使用方法  
  78. *  
  79. *   require_once('excel.php');  
  80. *   $doc = array (  
  81. *        0 => array ('中国', '中国人', '中国人民', '123456");  
  82. *   ); //开源代码phpfensi.com 
  83. *   $xls = new excel;  
  84. *   $xls->addarray ( $doc );  
  85. *   $xls->generatexml ("mytest");  
  86. */   

方法二:

其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每一列数据结束后加t,每一行数据结束后加n的方法echo出来,在php的开头用header("content-type:application/vnd.ms-excel");表示输出的是excel文件,用header("content-disposition:filename=test.xls");表示输出的文件名为text.xls,这样就ok了.代码如下:

  1. <?php 
  2.        header("content-type:application/vnd.ms-excel"); 
  3.        header("content-disposition:filename=test.xls"); 
  4.        echo "test1"; 
  5.        echo "test2"; 
  6.        echo "test1"; 
  7.        echo "test2"; 
  8.        echo "test1"; 
  9.        echo "test2"; 
  10.        echo "test1"; 
  11.        echo "test2"; 
  12.        echo "test1"; 
  13.        echo "test2"; 
  14.        echo "test1"; 
  15.        echo "test2"; 
  16. ?> 

方法三,代码如下:

  1. <?php 
  2.   header("content-type:   application/octet-stream");   
  3.   header("accept-ranges:   bytes");   
  4.   header("content-type:application/vnd.ms-excel");     
  5.   header("content-disposition:attachment;filename=export_excel_gshjsl.xls");     
  6.     
  7.   $tx='表头';   
  8.   echo   $tx." "; 
  9.   echo   "编号"." ";   
  10.   echo   "姓名"." ";   
  11.   echo   " "; 
  12.  echo "="411481198507150666""." ";  
  13.  echo "="0123456""." "; 
  14. //开源代码phpfensi.com 
  15.  echo " "; 
  16.  ?>

Tags: php 生成excel文件

分享到: