php 產生excel檔案

來源:互聯網
上載者:User
 代碼如下 複製代碼

class excel{   
  
 
    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">";   

    var $footer = "</workbook>";
    var $lines = array ();   
    var $worksheet_title = "table1";   
   
    function addrow ($array) {   
  
 
        $cells = "";   
           
 
        foreach ($array as $k => $v):   
               
            // 加個字串與數位判斷 避免產生的 excel 出現數字以字串儲存的警告   
            if(is_numeric($v)) {   
                // 防止首字母為 0 時產生 excel 後 0 丟失   
                if(substr($v, 0, 1) == 0) {   
                    $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";   
                 } else {   
                    $cells .= "<cell><data ss:type="number">" . $v . "</data></cell> ";   
                 }   
             } else {   
                $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";   
             }   
  
        endforeach;   
  
        // transform $cells content into one row   
        $this->lines[] = "<row> " . $cells . "</row> ";   
  
     }   
     
    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;   
  
     }   
 
    function generatexml ($filename) {   
  
 
         header("content-type: application/vnd.ms-excel; charset=utf-8");   
         header("content-disposition: inline; filename="" . $filename . ".xls"");   
 
        echo strips教程lashes ($this->header);   
        echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";   
        echo "<column ss:index="1" ss:autofitwidth="0" ss:width="110"/> ";   
        echo implode (" ", $this->lines);   
        echo "</table> </worksheet> ";   
        echo $this->footer;   
  
     }   
  
}   
 
  
/**
*   非架構使用方法
*
*   require_once('excel.php');
*   $doc = array (
*        0 => array ('中國', '中國人', '中國人民', '123456");
*   );
*   $xls = new excel;
*   $xls->addarray ( $doc );
*   $xls->generatexml ("mytest");
*/  

?>

方法二
其實在做真正的應用的時候,大家可以將資料從資料庫教程中取出,然後按照每一列資料結束後加t,每一行資料結束後加n的方法echo出來,在php的開頭用header("content-type:application/vnd.ms-excel");表示輸出的是excel檔案,用header("content-disposition:filename=test.xls");表示輸出的檔案名稱為text.xls。這樣就ok了。

 代碼如下 複製代碼

<?

       header("content-type:application/vnd.ms-excel");

       header("content-disposition:filename=test.xls");

       echo "test1";

       echo "test2";

       echo "test1";

       echo "test2";

       echo "test1";

       echo "test2";

       echo "test1";

       echo "test2";

       echo "test1";

       echo "test2";

       echo "test1";

       echo "test2";

?>

方法三

<?
  header("content-type:   application/octet-stream"); 
  header("accept-ranges:   bytes"); 
  header("content-type:application/vnd.ms-excel");   
  header("content-disposition:attachment;filename=export_excel_gshjsl.xls");   
  
  $tx='表頭'; 
  echo   $tx." ";
  echo   "編號"." "; 
  echo   "姓名"." "; 
  echo   " ";

 echo "="411481198507150666""." ";
 echo "="0123456""." ";
 echo " ";
 ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.