摘要:
*/ 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("") === false) { throw new Exception(sprintf("Error writing to file")); } } /** * Generate the single row * @param array Item with row data */ private function generateRow($item) { $row = " ", $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, "
"; 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
摘要:變更本部分內(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)庫 ...
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() ...
摘要:新特性掌握的引入,可以擴(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...
摘要:本書的地址篇收集了一些常見的基礎(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ǔ)篇 ...
摘要:安全生成安全的隨機(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ù)和字...
閱讀 1773·2023-04-26 00:20
閱讀 1823·2021-11-08 13:21
閱讀 2017·2021-09-10 10:51
閱讀 1581·2021-09-10 10:50
閱讀 3312·2019-08-30 15:54
閱讀 2144·2019-08-30 14:22
閱讀 1439·2019-08-29 16:10
閱讀 3102·2019-08-26 11:50