Php生成Excel文件如此简单

 

前阵子做了一个调查的小小项目。客户的想法是通过后台能将收集到的资料导出到Excel文件中,这样方便阅读,方便保存,及提取信息。

于是我就开始动手了:-),分析了一下这个小小项目,对于我来说最大的难点就是生成Excel文件。本人只是一个小小站长。生成Excel文件之前也没搞过。当时只知道这么一点点:先用php header()出一个Excel头,然后后面跟上Excel的XML代码,这样就能导出Excel功能了。但是Excel头是什么? Excel的XML写法又如何写呢?一直不明!

于是又开始我的g.cn之旅……最终功夫不负有心人,被我找到了一个外国老大写的程序,原名为:class-excel-xml.inc.php。我在未争得此牛人同意的情况下做了如下修改,于是我的那个小小项目不到一天完工,客户也比较满意,毕竟他要求的功能咱实现了!

如您也对生成Excel而烦恼,可以看一下此段代码:

>>>点击下载源文件<<<

PHP代码
  1. <?php   
  2. /**  
  3.  * Excel vendor By dx_andy  
  4.  * 整理自 class-excel-xml.inc.php  
  5.  * 时间 2008-3-27 11:58  
  6.  * 文件编码 utf-8  
  7.  *   
  8.  *  
  9.  * 整理记录:  
  10.  * 原文件中(function addRow) utf8_encode($v) => $v  
  11.  * 并添加判断字符串是 数字型 还是 字符串型的方法  
  12.  */  
  13.   
  14. class Excel{   
  15.   
  16.     /**  
  17.      * Header of excel document (prepended to the rows)  
  18.      *   
  19.      * Copied from the excel xml-specs.  
  20.      *   
  21.      * @access private  
  22.      * @var string  
  23.      */  
  24.     var $header = "<?xml version=/"1.0/" encoding=/"UTF-8/"?/>  
  25. <Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/"  
  26.  xmlns:x=/"urn:schemas-microsoft-com:office:excel/"  
  27.  xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/"  
  28.  xmlns:html=/"http://www.w3.org/TR/REC-html40/">";   
  29.   
  30.     /**  
  31.      * Footer of excel document (appended to the rows)  
  32.      *   
  33.      * Copied from the excel xml-specs.  
  34.      *   
  35.      * @access private  
  36.      * @var string  
  37.      */  
  38.     var $footer = "</Workbook>";   
  39.   
  40.     /**  
  41.      * Document lines (rows in an array)  
  42.      *   
  43.      * @access private  
  44.      * @var array  
  45.      */  
  46.     var $lines = array ();   
  47.   
  48.     /**  
  49.      * Worksheet title  
  50.      *  
  51.      * Contains the title of a single worksheet  
  52.      *  
  53.      * @access private   
  54.      * @var string  
  55.      */  
  56.     var $worksheet_title = "Table1";   
  57.   
  58.     /**  
  59.      * Add a single row to the $document string  
  60.      *   
  61.      * @access private  
  62.      * @param array 1-dimensional array  
  63.      * @todo Row-creation should be done by $this->addArray  
  64.      */  
  65.     function addRow ($array) {   
  66.   
  67.         // initialize all cells for this row   
  68.         $cells = "";   
  69.            
  70.         // foreach key -> write value into cells   
  71.         foreach ($array as $k => $v):   
  72.                
  73.             // 加个字符串与数字的判断 避免生成的 excel 出现数字以字符串存储的警告   
  74.             if(is_numeric($v)) {   
  75.                 // 防止首字母为 0 时生成 excel 后 0 丢失   
  76.                 if(substr($v, 0, 1) == 0) {   
  77.                     $cells .= "<Cell><Data ss:Type=/"String/">" . $v . "</Data></Cell>/n";   
  78.                 } else {   
  79.                     $cells .= "<Cell><Data ss:Type=/"Number/">" . $v . "</Data></Cell>/n";   
  80.                 }   
  81.             } else {   
  82.                 $cells .= "<Cell><Data ss:Type=/"String/">" . $v . "</Data></Cell>/n";   
  83.             }   
  84.   
  85.         endforeach;   
  86.   
  87.         // transform $cells content into one row   
  88.         $this->lines[] = "<Row>/n" . $cells . "</Row>/n";   
  89.   
  90.     }   
  91.   
  92.     /**  
  93.      * Add an array to the document  
  94.      *   
  95.      * This should be the only method needed to generate an excel  
  96.      * document.  
  97.      *   
  98.      * @access public  
  99.      * @param array 2-dimensional array  
  100.      * @todo Can be transfered to __construct() later on  
  101.      */  
  102.     function addArray ($array) {   
  103.   
  104.         // run through the array and add them into rows   
  105.         foreach ($array as $k => $v):   
  106.             $this->addRow ($v);   
  107.         endforeach;   
  108.   
  109.     }   
  110.   
  111.     /**  
  112.      * Set the worksheet title  
  113.      *   
  114.      * Checks the string for not allowed characters (://?*),  
  115.      * cuts it to maximum 31 characters and set the title. Damn  
  116.      * why are not-allowed chars nowhere to be found? Windows  
  117.      * help's no help…  
  118.      *  
  119.      * @access public  
  120.      * @param string $title Designed title  
  121.      */  
  122.     function setWorksheetTitle ($title) {   
  123.   
  124.         // strip out special chars first   
  125.         $title = preg_replace ("/[///|:|//|/?|/*|/[|/]]/"""$title);   
  126.   
  127.         // now cut it to the allowed length   
  128.         $title = substr ($title, 0, 31);   
  129.   
  130.         // set title   
  131.         $this->worksheet_title = $title;   
  132.   
  133.     }   
  134.   
  135.     /**  
  136.      * Generate the excel file  
  137.      *   
  138.      * Finally generates the excel file and uses the header() function  
  139.      * to deliver it to the browser.  
  140.      *   
  141.      * @access public  
  142.      * @param string $filename Name of excel file to generate (…xls)  
  143.      */  
  144.     function generateXML ($filename) {   
  145.   
  146.         // deliver header (as recommended in php manual)   
  147.         header("Content-Type: application/vnd.ms-excel; charset=UTF-8");   
  148.         header("Content-Disposition: inline; filename=/"" . $filename . ".xls/"");   
  149.   
  150.         // print out document to the browser   
  151.         // need to use stripslashes for the damn ">"   
  152.         echo stripslashes ($this->header);   
  153.         echo "/n<Worksheet ss:Name=/"" . $this->worksheet_title . "/">/n<Table>/n";   
  154.         echo "<Column ss:Index=/"1/" ss:AutoFitWidth=/"0/" ss:Width=/"110/"/>/n";   
  155.         echo implode ("/n"$this->lines);   
  156.         echo "</Table>/n</Worksheet>/n";   
  157.         echo $this->footer;   
  158.   
  159.     }   
  160.   
  161. }   
  162. /**  
  163.  *  CakePHP中使用方法  
  164.  *  注意 ** cakePHP 配置文件 define('DEBUG', 0);  
  165.  *  
  166.  *  vendor ('Excel');  
  167.  *  $doc = array (  
  168.  *       0 => array ('中国', '中国人', '中国人民', '123456");  
  169.  *  );  
  170.  *  $xls = new Excel;  
  171.  *  $xls->addArray ( $doc );  
  172.  *  $xls->generateXML ("mytest");  
  173.  */  
  174.   
  175. /**  
  176.  *  非框架使用方法  
  177.  *  
  178.  *  require_once('excel.php');  
  179.  *  $doc = array (  
  180.  *       0 => array ('中国', '中国人', '中国人民', '123456");  
  181.  *  );  
  182.  *  $xls = new Excel;  
  183.  *  $xls->addArray ( $doc );  
  184.  *  $xls->generateXML ("mytest");  
  185.  */  
  186.   
  187. ?>  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值