一聚教程网:一个值得你收藏的教程网站

热门教程

php 生成excel文件

时间:2022-06-24 19:07:17 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

class excel{   
  
 
    var $header = "
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 = "";
    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 .= "" . $v . " ";   
                 } else {   
                    $cells .= "" . $v . " ";   
                 }   
             } else {   
                $cells .= "" . $v . " ";   
             }   
  
        endforeach;   
  
        // transform $cells content into one row   
        $this->lines[] = " " . $cells . " ";   
  
     }   
     
    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_title . "">

";   
        echo " ";   
        echo implode (" ", $this->lines);   
        echo "
";   
        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 " ";
 ?>

热门栏目