一个很好用excel导出类

<?php

class PHPExcel
{

    /**
     * Header of excel document (prepended to the rows)
     */
    public $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\">";

    /**
     * Footer of excel document (appended to the rows)
     */
    public $footer = "</Workbook>";

    public $lines = array();

    public $worksheet_title = "Table1";

    /**
     * Add a single row to the $document string
     */
    public function addRow($array)
    {
        $cells = "";

        foreach ($array as $k => $v)
        {
            $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
            /**continue;
            if (is_numeric($v)) {
                // 防止首字母为 0 时生成 excel 后 0 丢失
                if (substr($v, 0, 1) == 0) {
                    $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
                } else {
                    $cells .= "<Cell><Data ss:Type=\"Number\">" . $v . "</Data></Cell>\n";
                }
            } else {
                $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
            }*/
        }

        $this->lines[] = "<Row>\n" . $cells . "</Row>\n";

    }

    /**
     * Add an array to the document
     */
    public function addArray($array)
    {
        foreach ($array as $k => $v):
            $this->addRow($v);
        endforeach;

    }

    /**
     * Set the worksheet title
     * @access public
     * @param string $title Designed title
     */
    public function setWorksheetTitle($title)
    {
        // strip out special chars first
        $title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);

        $title = substr($title, 0, 31);

        $this->worksheet_title = $title;

    }

    /**
     * Generate the excel file
     */
    public function generateXML($filename)
    {
        header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
        header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");

        echo stripslashes($this->header);
        echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
        echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n";
        echo implode("\n", $this->lines);
        echo "</Table>\n</Worksheet>\n";
        echo $this->footer;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值