2007年8月24日 星期五

php 動態建立 excel(.xls)檔案

PHP 建立 Excel 檔案

有時需要將資料庫內的資料或 PHP 的動態內容儲存成 excel 檔,本文會介紹一個概簡單又實用的方法。


建立 Excel 檔案
如果需要用 PHP 建立 Excel 檔案,只需用一般的檔案處理函式,而檔案名稱儲存成 .xls 副檔名即可,例如:

01 02 $fp = fopen("excel/filename.xls", "w");
03 fputs($fp, "Mon\tTue\tWed\tThu\tFri\tSat\tSun");
04 fclose($fp);
05 ?>


以上程式碼會在 excel 目錄建立一個名為 filename.xls 的檔案,內容是在第一行寫入一星期七天的英文簡寫,每一欄一天。

實際上與建立一般文字檔的方法一樣,只是如果要跳到下一個欄位,就用 \t 字元,而要跳到下一行,則用 \n 字元。


顯示下載窗口
如果電腦內有安裝 Excel 的話,在預設情況下當在 IE 按了 .xls 副檔名的文件時,IE 不會彈出下載窗口,而是直接顯示內容,以下兩個例子可避免這情況:

直接將內容輸出給瀏覽器下載

01 02 header("Content-type:application/vnd.ms-excel");
03 header("Content-Disposition:filename=php_excel.xls");
04
05 echo "Mon\tTue\tWed\tThu\tFri\tSat\tSun"
06 ?>


讀取已存在的檔案給瀏覽器下載


01 02 header("Content-type:application/vnd.ms-excel");
03 header("Content-Disposition:filename=php_excel.xls");
04
05 readfile("excel/filename.xls");
06 ?>


上面使用了 header() 傳送檔頭資訊給瀏覽器,可使瀏覽器出現下載窗口,而預設儲存的檔案名稱是 php_excel.xls。


====================================================
歡迎轉載,但轉載時請保留此宣告,不得作為商業用途
作者: Sam Tang
來源網站: http://www.phpdc.com/

2007年8月22日 星期三

[php] Fatal error: Allowed memory size of 8388608 bytes exhausted

最近開始試著玩PHP,原以為入門簡單的PHP語言卻發現還是有許多的小技巧與細節需要注意的地方!
因此特定將我練習與學習過程中遇到的一些問題與解決方式po出來。
目前我練習的系統是windows 2003 與 appserv 2.5.9
安裝PEAR套件:
採用appserv安裝完成後內附的go-pear.bat該批次檔進行安裝。
安裝PEAR:Auth套件

pear install Auth
出現該錯誤訊息:
Fatal error: Allowed memory size of 8388608 bytes exhausted ...

修正Windows下php.ini這行敘述

memory_limit = 16M ; Maximum amount of memory a script may consume (8MB)

因為這個錯誤訊息的意思表示,安裝該套件須大於8M的記憶體空間(8M預設值)

2007年8月21日 星期二

Do You Know ?

安立奎的新歌,雖然是200706發行的,不過應該還算新吧!
MV也不錯看,推薦一下囉^^


歌詞:

Do you know?
Do you know?

Do you know what it feels like loving someone that's in a rush to throw you away?
Do you know, do you know, do you know, do ya?
Do you know what it feels like to be the last one to know the lock on the door has changed?

If birds flying south is a sign of changes
At least you can predict this every year
Love, you never know the minute it ends suddenly
I can't get it to speak
Maybe if I knew all the things it took to save us
I could fix the pain that bleeds inside of me
Look in your eyes to see something about me
I'm standing on the edge and I don't know what else to give.

Do you know what it feels like loving someone that's in a rush to throw you away?
Do you know, do you know, do you know, do ya?
Do you know what it feels like to be the last one to know the lock on the door has changed?

How can I love you?
How can I love you?
How can I love you?
How can I love you?
If you just don't talk to me, babe.

I flow through my act
There's a question: Is she needed?
And decide all the man I can ever be.
Looking at the last 3 years like I did
I could never see us ending like this.

(Do you know?)

Seeing your face no more on my pillow
Is a scene that's never ever happened to me.

(Do you know?)

But after this episode I don't see
You could never tell the next thing life could be

Do you know what it feels like loving someone that's in a rush to throw you away?
Do you know, do you know, do you know, do ya?
Do you know what it feels like to be the last one to know the lock on the door has changed?

Do you know what it feels like loving someone that's in a rush to throw you away?
Do you know, do you know, do you know, do ya?
Do you know what it feels like to be the last one to know the lock on the door has changed?

(Do you know?)
(Do you know?)
(Do you know?)
(Do you know?)

Do you know what it feels like loving someone that's in a rush to throw you away?
Do you know, do you know, do you know, do ya?
Do you know what it feels like to be the last one to know the lock on the door has changed?

Do you know what it feels like loving someone that's in a rush to throw you away?
(Do you know how it feels?)
Do you know what it feels like to be the last one to know the lock on the door has changed?

(Do you know how it feels?)

(Do you know? Do you know? Do you know? Do you?)
(Do you know? Do you know? Do you know? Do you?)