- 論壇徽章:
- 0
|
查看了PHP手冊中關于 COM 的用法, 發(fā)現(xiàn)調(diào)用MS的 word / excel 很容易, 但其中有一些問題講的不細, 但網(wǎng)上沒有更多的資料, 寫一些來共享, 拋磚引玉.
?php
// starting word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}
";
//bring it to front
$word->Visible = 1;
//open an empty document
$word->Documents->Add();
//如果是 UTF-8 編碼要轉(zhuǎn)碼
$str = iconv("utf-8","gbk","邀月同宿青山深處");
//do some weird stuff
$word->Selection->TypeText($str);
$word->ActiveDocument->SaveAs("d:/demo/test.doc");
//closing word
$word->Quit();
//free the object
$word = null;
?>
以上代碼中有兩點要注意:
1. 如果寫入word文件中的字符有中文時要注意轉(zhuǎn)碼.如不轉(zhuǎn)碼則報錯:
Warning: Unknown: Could not convert string to unicode: `在多字節(jié)的目標代碼頁中,沒有此 Unicode 字符可以映射到的字符。
2. 保存文件時注意路徑, 如不寫完整則存入系統(tǒng)默認路徑下:
C:\WINDOWS\system32\config\systemprofile\My documents\
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/104997/showart_2088547.html |
|