基于Spreadsheet的php生成excel

<?
class Xls{

    /**
     * The clasee of Spreadsheet_Excel_Writer's object
     * @var Spreadsheet_Excel_Writer
     */
    protected $xls;

    /**
     * The clasee of Spreadsheet_Excel_Writer_Worksheet's object
     * @var Spreadsheet_Excel_Writer_Worksheet
     */
    protected $sheet;

    /**
     * The clasee of Spreadsheet_Excel_Writer_Format's object
     * @var Spreadsheet_Excel_Writer_Format
     */
    protected $format;

    /**
     * The data will be used to create file
     * the data must be an tow levels  array,
     * for example:
     *   array(array('title_a'=>'a','title_b'=>'b'),array('title_a'=>'a','title_b'=>'b') )
     * @var array
     */
    protected $data;

    /**
     * The file's rows
     * @var int
     */
    protected $rowCount;

    /**
     * The titles of the sheet
     * @var array
     */
    protected $title;

    /**
     * The construct method
     * initialize the xls,sheet,
     * format and rowCount.
     */
    public function __construct()
    {
        $this->xls = &new Spreadsheet_Excel_Writer();
        $this->xls->setVersion(8);
        $this->sheet = & $this->xls->addWorksheet('sheet1');
        $this->sheet->setInputEncoding('utf-8');
        $formats = $this->getFormat();
        $this->sheet->format = &$this->xls->addFormat($formats);
        $this->rowCount = 0;
    }

    /**
     * Initialize data.
     * @param array $data
     */
    public function setData(array $data)
    {
        $this->data=$data;
    }

    /**
     * Send file to the browser.
     *
     * @param string $filename
     * @param array $title
     *
     */
    public function sendFile($filename,$title = array())
    {
        if(empty ($filename))
        {
            $filename = data('Y-m-d');
        }
       
        $this->xls->send($filename);
       
        if(!empty ($title))
        {
            $this->title = $title;
            $this->writeTitle();
        }

        $this->writeRows();

        $this->setColumns();
        $this->setRows();
        $this->xls->close();
    }

    /**
     * Write rows to the file.
     */
    public function writeRows()
    {
        $COUNTROW = count($this->data);
        for($i =0 ;$i < $COUNTROW;$i++)
        {
            $this->writeRow($this->rowCount++,0,$this->data[$i],$this->format);
        }
    }

    /**
     *  Wirte row to the file.
     *
     * @param int $row
     * @param int $col
     * @param array $data
     *
     */
    protected function writeRow($row,$col,$data)
    {
        if(empty ($this->title))
        {
             $this->sheet->writeRow($row,$col,$data,$this->format);
             return;
        }
       
        foreach ($this->title as $key => $val)
        {
            $this->sheet->write($row, $col,$data[$key] ,$this->format);
            $col++;
        }
    }

    /**
     * Return the array to create Spreadsheet_Excel_Writer_Format object.
     *
     * @param string $style
     * @return array
     */
    protected function getFormat($style = 'body')
    {
        $style = strtolower($style);
        switch ($style) {
            case 'body':
                $formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center');
                break;
            case 'title':
                $formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center','Bold'=>true);
                break;
            default:
                $formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center');
                break;
        }
        return $formats;
    }

    /**
     * Set all columns width with data's string lenth.
     */
    protected function setColumns()
    {
        $width  = 10;
        $col = 0;
        foreach ($this->title as $key =>$val)
        {
            $this->sheet->setColumn($col, $col, strlen($this->data[0][$key])>$width?strlen($this->data[0][$key]):$width);
            $col++;
        }
    }

    /**
     * Set all rows hight.
     */
    protected function setRows()
    {
        $hight  = 20;
        for($i=0;$i<$this->rowCount;$i++){
            $this->sheet->setRow($i, $hight);
        }
    }

    /**
     * Write titles to file.
     * @param array $title
     */
    public function writeTitle(array $title=array())
    {
        if(empty ($title))
        {
            $title = $this->title;
        }
        $formats = $this->getFormat('title');
        $this->sheet->writeRow($this->rowCount++,0,$title,$this->xls->addFormat($formats));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值