php 生成excel的类,保留待用

本文介绍了一种使用PHP脚本创建Excel文件的方法。通过简单的代码示例,展示了如何定义标题、数据内容及设置文件名等关键步骤。此外,还涉及了如何通过HTTP头部信息来控制文件的下载方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
<?php 
class excel
{
/*
    require_once("excel.php");
$title=array("标题一","标题二","标题三");
$data=array(array(1,2,3),array(4,5,6),array(7,8,9));
$myexcel= new excel($title,$data,"myfile.xsl");
$myexcel->createexcel();

*/
    
    
var $titles;//标题
    var $datas;//数据数组
    var $filename;//文件名
    var $splits//分隔符
    function excel ( $titles=array(), $datas=array(), $filename="my.xsl",$split=" ")
    {
        
$this->titles=$titles;
        
        
$this->datas=$datas;
        
$this->filename=$filename;

        
$this->splits=$split;
    }
    
function excelheader()
    {
        
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
        
header ("Last-Modified: " . gmdate("D,d M YH:i:s". " GMT"); 
        
header ("Cache-Control: no-cache, must-revalidate");     
        
header ("Pragma: no-cache");
        
header ("Content-type: application/x-msexcel"); 
        
header ("Content-Disposition: attachment; filename=".$this->filename );  
        
header ("Content-Description: PHP/INTERBASE Generated Data" );
    }
    
function createexcel()
    {
        
$this->excelheader();
        
$title=implode($this->splits,$this->titles)." ";
        
echo $title;
        
for ($i=0;$i<count($this->datas);$i++)
        {
            
if (! is_array($this->datas[$i]))
            {
                
echo $this->datas[$i]." ";
            }
            
else
            {
                
echo implode($this->splits,$this->datas[$i])." ";
            }
        }

    }
    
function xlsBOF() { 
        
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
        
return
    } 
    
// Excel end of file footer 
    function xlsEOF() { 
        
echo pack("ss", 0x0A, 0x00); 
        
return
    } 
    
// Function to write a Number (double) into Row, Col 
    function xlsWriteNumber($Row, $Col, $Value) { 
        
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
        
echo pack("d", $Value); 
        
return
    } 
    
// Function to write a label (text) into Row, Col 
    function xlsWriteLabel($Row, $Col, $Value ) { 
        
$L = strlen($Value); 
        
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
        
echo $Value
    
return
    }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值