成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

Harriet666 / 1571人閱讀

摘要:


 */
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); } }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/20612.html

相關(guān)文章

  • PHP-5.3更高版本遷移之變更

    摘要:變更本部分內(nèi)容不再具體區(qū)分版本號(hào)及現(xiàn)在使用作為默認(rèn)庫強(qiáng)烈建議使用庫和在編譯安裝的時(shí)候,加上如下參數(shù)擴(kuò)展現(xiàn)在需要或更高版本不再支持使用低于版本的客戶端庫連接更多變更請(qǐng)?jiān)L問下面的資源上面的變更主要是函數(shù)參數(shù)和配置指令在中 PHP5.4-5.5變更 ps:本部分內(nèi)容不再具體區(qū)分版本號(hào) mysqlnd mysql mysqli及PDO_mysql現(xiàn)在使用mysqlnd作為默認(rèn)庫 ...

    Simon 評(píng)論0 收藏0
  • PHP-5.3更高版本遷移之不兼容

    PHP 5.4不兼容內(nèi)容 熟悉 安全模式的移除(safe_mode),涉及到php.ini配置指令 安全模式開啟,限制PHP中的一些內(nèi)置函數(shù)的使用 代碼中如果有依賴于安全模式保障安全的內(nèi)容,需要調(diào)整 移除魔術(shù)引號(hào)(magic_quote),涉及到php.ini配置指令 魔術(shù)引號(hào)自動(dòng)對(duì)用戶提交數(shù)據(jù)轉(zhuǎn)義(包括不必要轉(zhuǎn)義的數(shù)據(jù)),性能低下 魔術(shù)引號(hào)的效果和使用 addslashes() ...

    bitkylin 評(píng)論0 收藏0
  • PHP 5.3更高版本遷移之新特性

    摘要:新特性掌握的引入,可以擴(kuò)展的內(nèi)容,使在某種形式上實(shí)現(xiàn)了多重繼承,更加靈活不能被實(shí)例化示例代碼需要注意的是,的繼承順序來自當(dāng)前類的成員覆蓋了的方法,而則覆蓋了被繼承的方法當(dāng)多個(gè)被同一個(gè)類使用的時(shí)候,會(huì)出現(xiàn)方法沖突的情況,使用關(guān)鍵詞解決示 PHP 5.4新特性 掌握 traits trait的引入,可以擴(kuò)展class的內(nèi)容,使class在某種形式上實(shí)現(xiàn)了多重繼承,更加靈活 t...

    macg0406 評(píng)論0 收藏0
  • PHPer面試指南-PHP

    摘要:本書的地址篇收集了一些常見的基礎(chǔ)進(jìn)階面試題,基礎(chǔ)的面試題不再作答。如何實(shí)現(xiàn)持久化持久化,將在內(nèi)存中的的狀態(tài)保存到硬盤中,相當(dāng)于備份數(shù)據(jù)庫狀態(tài)。相當(dāng)于備份數(shù)據(jù)庫接收到的命令,所有被寫入的命令都是以的協(xié)議格式來保存的。 本書的 GitHub 地址:https://github.com/todayqq/PH... PHP 篇收集了一些常見的基礎(chǔ)、進(jìn)階面試題,基礎(chǔ)的面試題不再作答。 基礎(chǔ)篇 ...

    stackvoid 評(píng)論0 收藏0
  • php 學(xué)習(xí)指南及技術(shù)干貨

    摘要:安全生成安全的隨機(jī)數(shù),加密數(shù)據(jù),掃描漏洞的庫一個(gè)兼容標(biāo)準(zhǔn)的過濾器一個(gè)生成隨機(jī)數(shù)和字符串的庫使用生成隨機(jī)數(shù)的庫一個(gè)安全庫一個(gè)純安全通信庫一個(gè)簡單的鍵值加密存儲(chǔ)庫一個(gè)結(jié)構(gòu)化的安全層一個(gè)試驗(yàn)的面向?qū)ο蟮陌b庫一個(gè)掃描文件安全的庫 Security 安全 生成安全的隨機(jī)數(shù),加密數(shù)據(jù),掃描漏洞的庫 HTML Purifier-一個(gè)兼容標(biāo)準(zhǔn)的HTML過濾器 RandomLib-一個(gè)生成隨機(jī)數(shù)和字...

    lifefriend_007 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<