摘要:
*/ 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); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/20612.html
摘要:變更本部分內容不再具體區(qū)分版本號及現(xiàn)在使用作為默認庫強烈建議使用庫和在編譯安裝的時候,加上如下參數(shù)擴展現(xiàn)在需要或更高版本不再支持使用低于版本的客戶端庫連接更多變更請訪問下面的資源上面的變更主要是函數(shù)參數(shù)和配置指令在中 PHP5.4-5.5變更 ps:本部分內容不再具體區(qū)分版本號 mysqlnd mysql mysqli及PDO_mysql現(xiàn)在使用mysqlnd作為默認庫 ...
PHP 5.4不兼容內容 熟悉 安全模式的移除(safe_mode),涉及到php.ini配置指令 安全模式開啟,限制PHP中的一些內置函數(shù)的使用 代碼中如果有依賴于安全模式保障安全的內容,需要調整 移除魔術引號(magic_quote),涉及到php.ini配置指令 魔術引號自動對用戶提交數(shù)據(jù)轉義(包括不必要轉義的數(shù)據(jù)),性能低下 魔術引號的效果和使用 addslashes() ...
摘要:新特性掌握的引入,可以擴展的內容,使在某種形式上實現(xiàn)了多重繼承,更加靈活不能被實例化示例代碼需要注意的是,的繼承順序來自當前類的成員覆蓋了的方法,而則覆蓋了被繼承的方法當多個被同一個類使用的時候,會出現(xiàn)方法沖突的情況,使用關鍵詞解決示 PHP 5.4新特性 掌握 traits trait的引入,可以擴展class的內容,使class在某種形式上實現(xiàn)了多重繼承,更加靈活 t...
摘要:本書的地址篇收集了一些常見的基礎進階面試題,基礎的面試題不再作答。如何實現(xiàn)持久化持久化,將在內存中的的狀態(tài)保存到硬盤中,相當于備份數(shù)據(jù)庫狀態(tài)。相當于備份數(shù)據(jù)庫接收到的命令,所有被寫入的命令都是以的協(xié)議格式來保存的。 本書的 GitHub 地址:https://github.com/todayqq/PH... PHP 篇收集了一些常見的基礎、進階面試題,基礎的面試題不再作答。 基礎篇 ...
摘要:安全生成安全的隨機數(shù),加密數(shù)據(jù),掃描漏洞的庫一個兼容標準的過濾器一個生成隨機數(shù)和字符串的庫使用生成隨機數(shù)的庫一個安全庫一個純安全通信庫一個簡單的鍵值加密存儲庫一個結構化的安全層一個試驗的面向對象的包裝庫一個掃描文件安全的庫 Security 安全 生成安全的隨機數(shù),加密數(shù)據(jù),掃描漏洞的庫 HTML Purifier-一個兼容標準的HTML過濾器 RandomLib-一個生成隨機數(shù)和字...
閱讀 1771·2023-04-26 00:20
閱讀 1819·2021-11-08 13:21
閱讀 2010·2021-09-10 10:51
閱讀 1577·2021-09-10 10:50
閱讀 3310·2019-08-30 15:54
閱讀 2142·2019-08-30 14:22
閱讀 1438·2019-08-29 16:10
閱讀 3098·2019-08-26 11:50