`

PHP生成Excel

    博客分类:
  • PHP
阅读更多
<?php    
   
class 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\">";    
   
    /**   
     
       以Workbook开头,根据xml规则,就要以Workbook结尾
     */   
    var $footer = "</Workbook>";    
   
    /**
     * 
     * 新建一个数组
     */  
    var $lines = array ();    
   
   /**
    * 
    * 给当前工作表一个默认的名称
    */ 
    var $worksheet_title = "Table1";    
   
    /**
     * 
     * 处理一维数组,每一行
     * 
     */
    function addRow ($array) {    
   
        // 每一行都由很多单元格组成,初始化这个单元格集合,采用字符串拼接的方式
        $cells = "";    
            
        // foreach key -> write value into cells    
        foreach ($array as $k => $v):    
                
         
                $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";    
              
   
        endforeach;    
   
        // 把这些单元格合成一行   
        $this->lines[] = "<Row>\n" . $cells . "</Row>\n";    
   
    }    
   
 /**
  * 
  * 
  * 
  * 处理二维数组
  */    
    function addArray ($array) {    
   
        // run through the array and add them into rows    
        foreach ($array as $k => $v):    
            $this->addRow ($v);    
        endforeach;    
   
    }    
   
  /**
   * 
   * 
   * 
   * 设置工作薄的名称
   */
    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;    
   
    }    
   
    /**
     * 
     * 设置php下载名字,以及进行中文的乱码处理
     */
    function generateXML ($filename,$widthArr) {    
		$ua = $_SERVER["HTTP_USER_AGENT"];
		 $encoded_filename = urlencode($filename);
		 $encoded_filename = str_replace("+", "%20", $encoded_filename);
	     header("Content-Type: application/vnd.ms-excel; charset=UTF-8");  
		 if (preg_match("/MSIE/", $ua)) {	
		 	header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
		 } else if (preg_match("/Firefox/", $ua)) {	
		 	header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '.xls"');
		 } else {
		 	header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
		 }
         
        echo stripslashes ($this->header); //去掉反斜线字符,因为在 header的定义中的反斜线用来转义 
        echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n"; 
    	print_r($widthArr);
        $length=sizeof($widthArr);
        for ($i = 0; $i <$length; $i++) {
        	$j=$i+1;
        	 echo "<Column ss:Index=\"$j\" ss:AutoFitWidth=\"1\" ss:Width=\"$widthArr[$i]\"/>\n";    
        }  
        echo implode ("\n", $this->lines);    
        echo "</Table>\n</Worksheet>\n";    
        echo $this->footer;   
   
    }    
   
}    
 ?>
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics