亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) 轉(zhuǎn)帖:PHP4(windows版本)中的COM函數(shù)

轉(zhuǎn)帖:PHP4(windows版本)中的COM函數(shù)

Jun 21, 2016 am 09:14 AM
application com gt quot word

window|函數(shù)

這幾天一直在寫excel轉(zhuǎn)化成mysql,發(fā)現(xiàn)一篇文章,搜一下phpx論壇,沒有這個(gè)帖子,把它轉(zhuǎn)帖如下:


PHP4(windows版本)中的COM函數(shù)


  介紹

  內(nèi)置于PHP4里的COM函數(shù)對(duì)于我們?cè)趙in32環(huán)境下開發(fā)程序是相當(dāng)有吸引力的,但是至今仍沒有多少相關(guān)的技術(shù)文檔。本文將以三個(gè)例子分

別處理 MS office 2000 Word 、 Excel 、 Adobe Distiller 來(lái)說(shuō)明如何在PHP中使用COM函數(shù)。

  COM技術(shù)是由Microsoft在幾年前提出并開發(fā)的,本文中提到的相關(guān)名詞有OLE, OLE Automation, ActiveX, COM ,這些詞的意思都基本一

樣,都表示用一段封裝的代碼(對(duì)象)來(lái)完成一個(gè)windows 應(yīng)用程序的一些功能。 PHP4 COM 函數(shù)可以連接一個(gè)對(duì)象實(shí)例,并使用它的方法與

屬性。

如果你想使用下面的例子源碼,請(qǐng)參考一下我的配置。

Windows 98 - MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI


PHP4中的COM標(biāo)記

現(xiàn)在讓我們開始吧,用PHP4的COM來(lái)實(shí)例化一個(gè)組件,需要 new 操作符和對(duì)象的 "OLE 程序標(biāo)識(shí)":


$instance = new COM("$identifier");

?>

因?yàn)镃OM是一個(gè)PHP4的保留字,它傳送這個(gè)對(duì)象的標(biāo)識(shí)給一個(gè)構(gòu)造函數(shù),現(xiàn)在得到了這個(gè)組件的一個(gè)實(shí)例,根據(jù)OOP類的性質(zhì),我們可以很容易

地訪問它的方法與屬性。

例如:


$instance->[Object]->[method1]->[method2]->..->[property];

?>

就是這么簡(jiǎn)單!

OOP的結(jié)構(gòu)在PHP下不能工作,(由于PHP語(yǔ)法的問題,屬性的名字.值是非法字符,如點(diǎn)和圓括號(hào)等),所以PHP4提供了兩個(gè)相應(yīng)的函數(shù):


bool com_set(class com_object, string property name, string property_value);

mixed com_get(class com_object, string property_name);

?>

最后,PHP4也支持DCOM技術(shù),可以在遠(yuǎn)程計(jì)算機(jī)創(chuàng)建一個(gè)對(duì)象實(shí)例。


$Instance = new COM(string "Component name", string "remote_server_address");

?>

注意:這是用DCOM指令來(lái)設(shè)置PHP。在將來(lái),PHP開發(fā)者提供Unix下對(duì)DCOM的支持。

標(biāo)識(shí)、方法和屬性

標(biāo)識(shí)是一個(gè)如下的字串:

MS Word: "Word.Application" or "Word.Application.9"
MS Excel: "Excel.Application" or "Excel.Sheet"
ADOBE Acrobat: "Exch.application" or "PdfDistiller.PdfDistiller"

  對(duì)于最后一個(gè)標(biāo)識(shí),我要指明的是,獲得正確的對(duì)象標(biāo)識(shí)名不是一件容易的事。如果你不能訪問VBA文檔,你可以查找一下windows的注冊(cè)

表,在 HKEY_CLASSES_ROOT 中尋找一下,你就可以得到一些應(yīng)用程序的名字。在你的機(jī)器上有效的對(duì)象標(biāo)識(shí)放在 CLSID 子文件夾下。

  應(yīng)用程序一般會(huì)提供文檔說(shuō)明它的COM方法和屬性。在office2000中,你可以運(yùn)行程序,打開VBA編輯器 ,選擇對(duì)象編輯器。輸入應(yīng)用程序

庫(kù)中的一個(gè)方法名或?qū)傩悦缓?,在下面的窗口中用鼠?biāo)右鍵選擇一個(gè)類或成員名稱,點(diǎn)幫助,你就會(huì)得到關(guān)于這個(gè)類或成員的描述。你也

可以參考 MSDN。一個(gè) Excel 的例子如下: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm


用COM函數(shù)操作 MS Word

現(xiàn)在,我們開始第一個(gè)例子吧:


#*********************************************************
# 本例來(lái)自Zend站點(diǎn),略有改動(dòng)
# 打開一個(gè)word實(shí)例,并新建一個(gè)文檔Useless test.doc
# 輸入一行文字 "This is a test2..."
#*********************************************************

#實(shí)例化一個(gè)對(duì)象

$word = new COM("word.application") or die("Unable to instantiate Word");

#取得并顯示版本

print "Loaded Word, version {$word->Version}
";

#另一種方法去取得版本

$testversion = com_get($word->application,version);

print "Version using Com_get(): $testversion
";

#使其可見

$word->Visible = 1;

#創(chuàng)建新文件

$word->Documents->Add();

#寫字符

$word->Selection->TypeText("This is a test...");

#保存

$word->Documents[1]->SaveAs("Useless test.doc");

#關(guān)閉

$word->Quit();

?>

你只要花幾分鐘來(lái)讀這個(gè)程序,并參考Word的OLE 技術(shù)文檔,你將學(xué)到幾乎是你在自己程序中所需的全部的操作。

MS Excel在使用PHP的COM函數(shù)

  如同上面的Word的例子一樣,你應(yīng)學(xué)習(xí)這個(gè)例子的同時(shí)參考Excel的Visual Basic 編輯器中的對(duì)象瀏覽器的幫助文檔。


#打開workbook和它的sheet,
#本例使用一個(gè)電子表格是Excel安裝時(shí)自帶的SOLVSAMP.XLS

$workbook = "C:Program FilesMicrosoft officeOfficeSamplesSOLVSAMP.XLS";
$sheet = "Quick Tour";

#實(shí)例化一個(gè)組件的對(duì)象
$ex = new COM("Excel.sheet") or Die ("Did not connect");

#取程序名稱和版本
print "Application name:{$ex->Application->value}
" ;
print "Loaded version: {$ex->Application->version}
";

#打開工作本使我們可使用它
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");

#預(yù)保存原來(lái)的工作本,創(chuàng)建一個(gè)工作本的復(fù)本
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#$ex->Application->Visible = 1; #本句去注釋讓Excel可見

# 讀寫一個(gè)單元格在一個(gè)新的工作表中
# 我們可以讀到這個(gè)單元格 E11 (Advertising in the 4th. Quarter)
$sheets = $wkb->Worksheets($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->Cells(11,5) ; #Select the cell (Row Column number)
$cell->activate; #Activate the cell
print "Old Value = {$cell->value}
"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
print "New value = {$cell->value}
";#Print the new value=15000

#最后,用新值重新計(jì)算這個(gè)單元格
$sheets->Calculate;
#必須的如果要計(jì)算,手動(dòng)則是可選的
#可看到效果總價(jià)值(E13單元格)
$cell = $sheets->Cells(13,5) ; #Select the cell (Row Column number)
$number = Number_format($cell->value);
print "New Total cost =$$number - was $47,732 before.
";
#根據(jù)計(jì)算公式,廣告影響了公司的開銷,這里將顯示 $57,809

#使用Excel內(nèi)建的函數(shù)
# PMT(percent/12 months,Number of payments,Loan amount)
$pay = $ex->application->pmt(0.08/12,10,10000);
$pay = sprintf("%.2f",$pay);
print "Monthly payment for $10,000 loan @8% interest /10 months: $ $pay
";

#Should print monthly payment = $ -1,037.03

#可選,保存
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#關(guān)閉,不提問
$ex->application->ActiveWorkbook->Close("False");
unset ($ex);

?>

  這個(gè)例子讓你的PHP與Excel一同工作了,當(dāng)然,也有更多的對(duì)象可以使用,訪問一個(gè)自已寫的OOP封裝類也與訪問excel一樣容易。

用PHP的COM訪問 Adobe Distiller

  這最后一個(gè)例子不是MS程序了,如果你的程序有一個(gè)PostScript文件,你會(huì)對(duì)這個(gè)有興趣的,改寫(蒸餾)它成為一個(gè)PDF文檔. Adobe 有一

個(gè)程序叫 Distiller ,它可以生成一個(gè)實(shí)例。代碼如下:


$pdf = new COM("pdfdistiller.pdfdistiller.1");

?>

  有一點(diǎn)要注意的,是在Distiller 的文檔中給出的這個(gè)OLE標(biāo)識(shí)名 "pdfdistiller" 是無(wú)效的。

蒸餾一個(gè)文件的最基本的方法是:


$pdf->FileToPdf ($psfile, strOutputPDF '', strJobOptions "");

?>

這 $psfile 是這個(gè)PostScript的文件名, strOutputPDF 是輸出文件PDF的文件名。 StrJobOptions 是Distiller的參數(shù)文件名,最后兩個(gè)參數(shù)

是可選的,默認(rèn)是同一名字。 這PS文件名與PDF文件名,使用這個(gè)默認(rèn)的Job options 文件。例如:


$pdf->FileToPdf ($psfile, "", "");
#這兒$psfile 可以是 Myfile.ps 將返回 Myfile.pdf 文件。

?>

在Distiller中有更多的方法和屬性能被用。如果你感興趣,請(qǐng)參考一下Adobe的技術(shù)文檔。


中止/可能的問題

  如果你的代碼中發(fā)生了什么錯(cuò)誤,你可能會(huì)創(chuàng)建了一個(gè)實(shí)例,但沒有正常地關(guān)閉它。最糟的是,這個(gè)應(yīng)用程序可能被這個(gè)實(shí)例所保持,結(jié)

果,在你的程序列表中就存在多份這個(gè)程序的副本,即使你更正了這個(gè)錯(cuò)誤也會(huì)干擾你的結(jié)果。解決方法是:修正一個(gè)bug以來(lái)要及時(shí)清除它們

在你重新開始運(yùn)行之前,用 并結(jié)束任務(wù)。同樣的原因,在你的代碼最后,也要及時(shí)關(guān)閉這個(gè)程序并刪除這個(gè)實(shí)例。

你有一些技巧在處理 com_get 和 com_set的異常時(shí)。例如:
$Version = Com_get($instance->Application,"Version");

將會(huì)在Word中工作但在Excel中會(huì)產(chǎn)生一個(gè)錯(cuò)誤。

有一些對(duì)象在PHP4中是不能實(shí)例化的,這是因?yàn)檫@個(gè)程序要一個(gè)自定義的接口,但PHP4不支持。


為什么我們要用它?

  我希望這三個(gè)例子可以給你一些思考的線索,PHP的COM允許你在PHP的腳本中訪問windows4的程序。這個(gè)代碼比ASP簡(jiǎn)單并且能集成其它的

PHP對(duì)數(shù)據(jù)庫(kù)強(qiáng)大的支持功能。Microsoft 在各個(gè)方面都大力銷售這個(gè)COM 技術(shù),在不同的名稱和結(jié)構(gòu)下,如 COM+(Combine COM with

Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, 等等。 PHP 和 Apache的結(jié)合,提供了一個(gè)開放源碼的解決方案。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to cancel automatic word wrapping in word How to cancel automatic word wrapping in word Mar 19, 2024 pm 10:16 PM

When editing content in a word document, lines may automatically wrap. If no adjustment is made at this time, it will have a great impact on our editing and make people very headache. What is going on? In fact, it is a problem with the ruler. Below, I will introduce the solution to how to cancel automatic word wrapping in word. I hope it can help everyone! After opening a Word document and entering text, when you try to copy and paste, the text may jump to a new line. In this case, you need to adjust the settings to solve this problem. 2. To solve this problem, we must first know the cause of this problem. At this time we click View under the toolbar. 3. Then click the "Ruler" option below. 4. At this time we will find that a ruler appears above the document with several conical markers on it.

Detailed explanation of how to display the ruler in Word and how to operate the ruler! Detailed explanation of how to display the ruler in Word and how to operate the ruler! Mar 20, 2024 am 10:46 AM

When we use Word, in order to edit the content more beautifully, we often use rulers. You should know that the rulers in Word include horizontal rulers and vertical rulers, which are used to display and adjust the document's page margins, paragraph indents, tabs, etc. So, how do you display the ruler in Word? Next, I will teach you how to set the ruler display. Students in need should quickly collect it! The steps are as follows: 1. First, we need to bring up the word ruler. The default word document does not display the word ruler. We only need to click the [View] button in word. 2. Then, we find the option of [Ruler] and check it. In this way, we can adjust the word ruler! Yes or no

How to add handwritten signature to word document How to add handwritten signature to word document Mar 20, 2024 pm 08:56 PM

Word documents are widely used due to their powerful functions. Not only can various formats be inserted into Word, such as pictures and tables, etc., but now for the integrity and authenticity of the files, many files require a manual signature at the end of the document. It sounds like this How to solve complex problems? Today I will teach you how to add a handwritten signature to a word document. Use a scanner, camera or mobile phone to scan or photograph the handwritten signature, and then use PS or other image editing software to perform necessary cropping on the image. 2. Select "Insert - Picture - From File" in the Word document where you want to insert the handwritten signature, and select the cropped handwritten signature. 3. Double-click the handwritten signature picture (or right-click the picture and select "Set Picture Format"), and the "Set Picture Format" pops up.

How to set page margins for Word How to set page margins for Word Mar 19, 2024 pm 10:00 PM

Among office software, Word is one of our most commonly used software. The text documents we produce are generally operated with Word. Some documents need to be submitted in paper version as required. Before printing, the layout must be set before it can be presented. produce better results. So the question is, how do you set page margins in Word? We have specific course explanations to help you solve your doubts. 1. Open or create a new word document and click the "Page Layout" menu on the menu bar. 2. Click the "Margins" button of the "Page Setup" option. 3. Select a commonly used page margin in the list. 4. If there are no suitable margins in the list, click "Custom Margins". 5. The "Page Setup" dialog box pops up, enter the "Margins" option respectively.

How to draw a table in Word How to draw a table in Word Mar 19, 2024 pm 11:50 PM

Word is a very powerful office software. Compared with WPS, Word has more advantages in detail processing. Especially when the document description is too complex, it is generally more worry-free to use Word. Therefore, when you enter the society, you must learn some tips on using word. Some time ago, my cousin asked me a question like this. I often see other people drawing tables when using Word, and I feel very high-level. I laughed at that time. It seemed like high-level content, but actually it only took 3 steps to operate. Do you know how to draw a table in Word? 1. Open word, select the place where you want to insert the table, and find the "Insert" option in the upper menu bar. 2. Click the "Table" option, and densely packed small cubes will appear.

Where is the shading setting in word? Where is the shading setting in word? Mar 20, 2024 am 08:16 AM

We often use word for office work, but do you know where the shading settings are in word? Today I will share with you the specific operation steps. Come and take a look, friends! 1. First, open the word document, select a paragraph of text paragraph information that needs to be added with shading, then click the [Start] button on the toolbar, find the paragraph area, and click the drop-down button on the right (as shown in the red circle in the figure below) ). 2. After clicking the drop-down box button, in the pop-up menu options, click the [Border and Shading] option (as shown in the red circle in the figure below). 3. In the pop-up [Border and Shading] dialog box, click the [Shading] option (as shown in the red circle in the figure below). 4. In the filled column, select a color

How to draw a dotted line in word How to draw a dotted line in word Mar 19, 2024 pm 10:25 PM

Word is a software that we often use in our office. It has many functions that can facilitate our operations. For example, for a large article, we can use the search function inside to find out that a word in the full text is wrong, so we can directly replace it. Make changes one by one; when submitting the document to your superiors, you can beautify the document to make it look better, etc. Below, the editor will share with you the steps on how to draw a dotted line in Word. Let's learn together! 1. First, we open the word document on the computer, as shown in the figure below: 2. Then, enter a string of text in the document, as shown in the red circle in the figure below: 3. Next, press and hold [ctrl+A] Select all the text, as shown in the red circle in the figure below: 4. Click [Start] on the top of the menu bar

How to underline in Word How to underline in Word Mar 20, 2024 pm 03:16 PM

As a very commonly used word processing software, Word is used in our life, study and work all the time. Of course, if you want to make good use of Word to edit text, you must lay a good foundation. So today I will take you to learn how to underline in Word. You can do it together with the editor. It is very simple. 1. First, we open the file we need to edit. Here we take the following figure as an example. 2. Use the mouse to select the text we need to edit. In the pop-up tab, we select the [U] icon. The operation is as shown in the figure: 3. Let’s take a look at the effect: 4. In fact, we can use a more convenient and faster The method is to use the key combination [ctrl] + [U] on the keyboard to add and follow your text.

See all articles