Strict Standards: mktime(): You should be using the time() function instead in /home/content/05/7150505/html/subdomain/blog/rss.php on line 43
記憶日記 http://blog.jfoto.org/index.php 我的過去、現在和未來 zh-tw Jr Fri, 18 Oct 2019 11:19:23 -0700 [PHP] 如何利用 PHP 將資料寫入至 Excel http://blog.jfoto.org/index.php?pl=77 因為工作上的關係,需要把一些資訊以 Excel 的方式輸出,在拜訪過 <a href="http://www.google.com" target="_blank">Google</a> 大神後,找到了 PHP 利用 <a href="http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=101" target="_blank">COM</a> 函式將資料寫入至 Excel 的方式,在此之前,先提一個更簡單的方式,就是直接利用 fopen 寫至 Excel,方式如下 ...<br /><br /><div style="line-height:120%"><div style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; BACKGROUND-COLOR: #F6F6F6; BORDER-RIGHT: #cccccc 1px dashed; BORDER-TOP: #cccccc 1px dashed; BORDER-LEFT: #cccccc 1px dashed; BORDER-BOTTOM: #cccccc 1px dashed;"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"excel_filename.xls"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fputs</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"data1&nbsp;&nbsp;&nbsp;&nbsp;data2&nbsp;&nbsp;&nbsp;&nbsp;data3"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div><br />也就是說,餵進去的資料只要以 Tab 隔開,寫入至 Excel 中就會被分配至各欄位,這方法雖然簡便,但卻缺少了對 Excel 一些額外的控制,如選擇字體顏色、大小、背景等 ...<br /><br />利用 COM 函式的呼叫,就可以有更多的選擇來控制 Excel 的輸出格式 ... <div id="div771a" style="display:block"><a href="#" onclick="layer_toggle(document.getElementById(\'div771a\')); layer_toggle(document.getElementById(\'div771b\')); return false; "> 閱讀全文.. </a></div><div id="div771b" style="display:none"><a href="#" onclick="layer_toggle(document.getElementById(\'div771a\')); layer_toggle(document.getElementById(\'div771b\'));return false;"> 部分摘要.. </a> <br /><br /><div style="line-height:120%"><div style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; BACKGROUND-COLOR: #F6F6F6; BORDER-RIGHT: #cccccc 1px dashed; BORDER-TOP: #cccccc 1px dashed; BORDER-LEFT: #cccccc 1px dashed; BORDER-BOTTOM: #cccccc 1px dashed;"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?<br />$excel&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"Excel.Application"</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;instanciate&nbsp;Excel"</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Visible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;設定執行時&nbsp;Excel&nbsp;是否可見<br /></span><span style="color: #0000BB">$book</span><span style="color: #007700">=</span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Workbooks</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Add</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB">$sheet&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Worksheets</span><span style="color: #007700">(</span><span style="color: #DD0000">"Sheet1"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;選擇資料表<br /></span><span style="color: #0000BB">$Range&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sheet</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Range</span><span style="color: #007700">(</span><span style="color: #DD0000">"A1:J3"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;設定資料的範圍<br /><br /></span><span style="color: #007700">for(</span><span style="color: #0000BB">$x</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">&lt;=</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">++){<br />&nbsp;&nbsp;for(</span><span style="color: #0000BB">$y</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">&lt;=</span><span style="color: #0000BB">3</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">++){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cell&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$Range</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Cells</span><span style="color: #007700">(</span><span style="color: #0000BB">$y</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;資料寫入的欄位<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cell</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"x=</span><span style="color: #0000BB">$x</span><span style="color: #DD0000">,&nbsp;y=</span><span style="color: #0000BB">$y</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;要寫入的值<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">SaveAs</span><span style="color: #007700">(</span><span style="color: #0000BB">$excel_path</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;存檔<br /></span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Close</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Quit</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Release</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div><br /><br />另一種利用 template 的方式寫入資料,也就是說,你可以事先安排好 Excel 的排版,然後再填入資料 ...<br /><div style="line-height:120%"><div style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; BACKGROUND-COLOR: #F6F6F6; BORDER-RIGHT: #cccccc 1px dashed; BORDER-TOP: #cccccc 1px dashed; BORDER-LEFT: #cccccc 1px dashed; BORDER-BOTTOM: #cccccc 1px dashed;"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?<br />$excel&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"Excel.Application"</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;instanciate&nbsp;Excel"</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Visible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$book&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Workbooks</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Open</span><span style="color: #007700">(</span><span style="color: #0000BB">$excel_path</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Excel&nbsp;Template&nbsp;File<br /></span><span style="color: #0000BB">$sheet&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Worksheets</span><span style="color: #007700">(</span><span style="color: #DD0000">"Sheet1"</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$Range&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sheet</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Range</span><span style="color: #007700">(</span><span style="color: #DD0000">"A1:J3"</span><span style="color: #007700">);<br /><br />for(</span><span style="color: #0000BB">$x</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">&lt;=</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">++){<br />&nbsp;&nbsp;for(</span><span style="color: #0000BB">$y</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">&lt;=</span><span style="color: #0000BB">3</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">++){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cell&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$Range</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Cells</span><span style="color: #007700">(</span><span style="color: #0000BB">$y</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cell</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"x=</span><span style="color: #0000BB">$x</span><span style="color: #DD0000">,&nbsp;y=</span><span style="color: #0000BB">$y</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;另存新檔,不直接寫入&nbsp;Template&nbsp;File&nbsp;中<br /></span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">SaveAs</span><span style="color: #007700">(</span><span style="color: #0000BB">$excel_output_path</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Close</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Quit</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Release</span><span style="color: #007700">();&nbsp;<br /></span><span style="color: #0000BB">$excel&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div><br /><br />其它更多有關 Excel 的控制可以參考<a href="http://vfp.sunyear.com.tw/viewtopic.php?t=32&start=0&postdays=0&postorder=asc&highlight=" target="_blank">這裡</a></div> 資訊 - PHP & DB Jr Sun, 09 Apr 2006 01:45:32 -0700 [PHP] 刪除指定目錄 http://blog.jfoto.org/index.php?pl=76 只是一個很簡單的小小 Function,用遞迴的方式來掃出目錄下的所有檔案及資料夾,進而刪除。<br /><br />雖說這種小 Function 很簡單,可是有時突然要用到卻又沒現成 Code 可取時,還要再花個五至十分鐘寫都覺得煩,既然已經浪費了一次十分鐘,好吧 ... 就記下來,下次就別再浪費這十分鐘啦 !!<br /><br />對了,這個 Function 不做任何的資料判斷,也就是說,如果有任何的唯讀檔或鎖死檔案在目錄中,一樣執行動作,PHP 會有自己的錯誤訊息,但 Function 本身不管這塊,有需要的人可以自己再加上其它的錯誤判斷。<br /><br /><div style="line-height:120%"><div style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; BACKGROUND-COLOR: #F6F6F6; BORDER-RIGHT: #cccccc 1px dashed; BORDER-TOP: #cccccc 1px dashed; BORDER-LEFT: #cccccc 1px dashed; BORDER-BOTTOM: #cccccc 1px dashed;"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">rm_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">){<br />&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #DD0000">\'.\'&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #DD0000">\'..\'&nbsp;</span><span style="color: #007700">){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$path</span><span style="color: #DD0000">/</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">rm_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">colsedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">rmdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);&nbsp;<br />&nbsp;&nbsp;}&nbsp;<br />}&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div> 資訊 - PHP & DB Jr Sun, 09 Apr 2006 00:20:30 -0700 [PHP] PHP 如何透過 ODBC 來存取資料庫 http://blog.jfoto.org/index.php?pl=51 最近因為使用上的需要,想利用 PHP 來連結 Access 資料庫,對於只有 PHP vs MySQL 經驗的我當然是不得其門而入,一度想透過轉檔的方式將資料庫文件轉成純文字檔,再透過特定的標籤去截取資料,就這樣,差點把原本簡單快速又方便的工作搞得複雜又費時。<br /><br />PHP 的 ODBC 函式和 MySQL 函式一樣,本身已經包含在 PHP 預設的函式中,是不需另外透過模組掛載才能使用的。在拜訪過 Google 大神後,相關的資源並不多,顯然 PHP 大多還是被拿來和 MySQL 搭配應用,在此提供一份由 ernest (Chiang Deng-Wei) 所發表的教學範例以供參考 ...<br /><br /><a href="http://kmserv.com/windows/odbc/howto_odbc.htm" target="_blank">PHP 如何透過 ODBC 來存取資料庫</a><br /><br />特別需注意的是在選擇使用的 ODBC 驅動程式時,是在 "<b>系統資料來源名稱</b>" 中選擇 "<b>Microsoft Access Driver</b>",特別特別注意 !! 因為我的眼睛特別大,沒仔細看清楚就亂選一通,結果就裁在這二個小地方,搞了老半天都在做白工,特別在此提出以免再次犯相同的錯誤 !! 資訊 - PHP & DB Jr Tue, 06 Dec 2005 23:39:42 -0700