资讯专栏INFORMATION COLUMN

PHP-5.3向更高版本迁移系列博客使用的php-excel文件类

Harriet666 / 1422人阅读

摘要:


 */
class Excel_XML
{

        /**
         * MicrosoftXML Header for Excel
         * @var string
         */
        const sHeader = "
";

        /**
         * MicrosoftXML Footer for Excel
         * @var string
         */
        const sFooter = "";

        /**
         * Worksheet & Data
         * @var array
         */
        private $aWorksheetData;
        /**
         * Encoding to be used
         * @var string
         */
        private $sEncoding;

        /**
         * write xls file handle resouce
         *
         */
        private $rHandle = null;
        /**
         * Constructor
         *
         * Instanciates the class allowing a user-defined encoding.
         *
         * @param string $sEncoding Charset encoding to be used
         */
        public function __construct($sEncoding = "UTF-8")
        {
                $this->sEncoding = $sEncoding;
                $this->sOutput = "";
        }

        /**
         * Add a worksheet
         *
         * Creates a new worksheet and adds the given data to it.
         * @param string $title Title of worksheet
         * @param array $data 2-dimensional array of data
         */
        public function addWorksheet($title, $data)
        {
                $this->aWorksheetData[] = array(
                        "title" => $this->getWorksheetTitle($title),
                        "data"  => $data
                );
        }
        /**
         * Write workbook to file
         *
         * Writes the workbook into the file/path given as a parameters.
         * The method checks whether the directory is writable and the
         * file is not existing and writes the file.
         *
         * @param string $filename Filename to use for writing (must contain mimetype)
         * @param string $path Path to use for writing [optional]
         */
        public function writeWorkbook($filename, $path = "")
        {
                $filename = $this->getWorkbookTitle($filename);
                //open file check
                if (!$this->rHandle = fopen($path . $filename, "w+"))
                {
                        throw new Exception(sprintf("Not allowed to write to file %s", $path . $filename));
                }
                //write header
                $sheader = stripslashes(sprintf(self::sHeader, $this->sEncoding)) . "
";
                if (fwrite($this->rHandle, $sheader) === false)
                {
                        throw new Exception(sprintf("Error writing to file %s", $path . $filename));
                }
                //write coentent
                $this->generateWorkbook();
                //wirte footer
                if (fwrite($this->rHandle, self::sFooter) === false)
                {
                        throw new Exception(sprintf("Error writing to file %s", $path . $filename));
                }
                fclose($this->rHandle);
                return sprintf("File %s written", $path . $filename);
        }
        /**
         * Workbook title correction
         *
         * Corrects filename (if necessary) stripping out non-allowed
         * characters.
         *
         * @param string $filename Desired filename
         * @return string Corrected filename
         */
        private function getWorkbookTitle($filename)
        {
                return preg_replace("/[^aA-zZ0-9\_-.]/", "", $filename);
        }

        /**
         * Worksheet title correction
         *
         * Corrects the worksheet title (given by the user) by the allowed
         * characters by Excel.
         *
         * @param string $title Desired worksheet title
         * @return string Corrected worksheet title
         */
        private function getWorksheetTitle($title)
        {
                $title = preg_replace ("/[|:|/|?|*|[|]]/", "", $title);
                return substr ($title, 0, 31);
        }

        /**
         * Generate the workbook
         *
         * This is the main wrapper to generate the workbook.
         * It will invoke the creation of worksheets, rows and
         * columns.
         */
        private function generateWorkbook()
        {
                foreach ($this->aWorksheetData as $item):
                        $this->generateWorksheet($item);
                endforeach;
        }

        /**
         * Generate the Worksheet
         *
         * The second wrapper generates the worksheet. When the worksheet
         * data seems to be more than the excel allowed maximum lines, the
         * array is sliced.
         *
         * @param array $item Worksheet data
         * @todo Add a security check to testify whether this is an array
         */
        private function generateWorksheet($item)
        {
                $ssheet= sprintf("
    
", $item["title"]);
                if (fwrite($this->rHandle, $ssheet) === false)
                {
                        throw new Exception(sprintf("Error writing to file"));
                }
                $i = 0;
                foreach ($item["data"] as $k => $v)
                {
                    if($i > 65536)
                    {
                        break;
                    }
                    $this->generateRow($v);
                    $i++;
                }
                if (fwrite($this->rHandle, "    
") === false) { throw new Exception(sprintf("Error writing to file")); } } /** * Generate the single row * @param array Item with row data */ private function generateRow($item) { $row = " "; foreach ($item as $k => $v) { $row .= $this->generateCell($v); } $row .= " "; if (fwrite($this->rHandle, $row) === false) { throw new Exception(sprintf("Error writing to file")); } } /** * Generate the single cell * @param string $item Cell data */ private function generateCell($item) { $type = "String"; if (is_numeric($item)) { $type = "Number"; if ($item{0} == "0" && strlen($item) > 1 && $item{1} != ".") { $type = "String"; } } $item = str_replace("'", "'", htmlspecialchars($item, ENT_QUOTES)); return sprintf(" %s ", $type, $item); } /** * Deconstructor * Resets the main variables/objects */ public function __destruct() { unset($this->aWorksheetData); unset($this->sOutput); } }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/20612.html

相关文章

  • PHP-5.3更高版本迁移之变更

    摘要:变更本部分内容不再具体区分版本号及现在使用作为默认库强烈建议使用库和在编译安装的时候,加上如下参数扩展现在需要或更高版本不再支持使用低于版本的客户端库连接更多变更请访问下面的资源上面的变更主要是函数参数和配置指令在中 PHP5.4-5.5变更 ps:本部分内容不再具体区分版本号 mysqlnd mysql mysqli及PDO_mysql现在使用mysqlnd作为默认库 ...

    Simon 评论0 收藏0
  • PHP-5.3更高版本迁移之不兼容

    PHP 5.4不兼容内容 熟悉 安全模式的移除(safe_mode),涉及到php.ini配置指令 安全模式开启,限制PHP中的一些内置函数的使用 代码中如果有依赖于安全模式保障安全的内容,需要调整 移除魔术引号(magic_quote),涉及到php.ini配置指令 魔术引号自动对用户提交数据转义(包括不必要转义的数据),性能低下 魔术引号的效果和使用 addslashes() ...

    bitkylin 评论0 收藏0
  • PHP 5.3更高版本迁移之新特性

    摘要:新特性掌握的引入,可以扩展的内容,使在某种形式上实现了多重继承,更加灵活不能被实例化示例代码需要注意的是,的继承顺序来自当前类的成员覆盖了的方法,而则覆盖了被继承的方法当多个被同一个类使用的时候,会出现方法冲突的情况,使用关键词解决示 PHP 5.4新特性 掌握 traits trait的引入,可以扩展class的内容,使class在某种形式上实现了多重继承,更加灵活 t...

    macg0406 评论0 收藏0
  • PHPer面试指南-PHP

    摘要:本书的地址篇收集了一些常见的基础进阶面试题,基础的面试题不再作答。如何实现持久化持久化,将在内存中的的状态保存到硬盘中,相当于备份数据库状态。相当于备份数据库接收到的命令,所有被写入的命令都是以的协议格式来保存的。 本书的 GitHub 地址:https://github.com/todayqq/PH... PHP 篇收集了一些常见的基础、进阶面试题,基础的面试题不再作答。 基础篇 ...

    stackvoid 评论0 收藏0
  • php 学习指南及技术干货

    摘要:安全生成安全的随机数,加密数据,扫描漏洞的库一个兼容标准的过滤器一个生成随机数和字符串的库使用生成随机数的库一个安全库一个纯安全通信库一个简单的键值加密存储库一个结构化的安全层一个试验的面向对象的包装库一个扫描文件安全的库 Security 安全 生成安全的随机数,加密数据,扫描漏洞的库 HTML Purifier-一个兼容标准的HTML过滤器 RandomLib-一个生成随机数和字...

    lifefriend_007 评论0 收藏0

发表评论

0条评论

Harriet666

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<