to all:
昨天试验的结果是:
如果用这一句:
$exapp = new COM("Excel.application") or Die ("Did not connect");
则不能打开文件。
但是如果把这句话改成:
$exapp = new COM("Excel.sheet") or Die ("Did not connect");
全部代码如下:
<pre>
<?php
$workbook = "C:\\web\\test.xls";
$sheet = "test";
$ex = new COM("Excel.sheet") or Die ("Did not connect");
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");
$ex->Application->Visible = 1;
$sheets = $wkb->Worksheets($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->Cells(3,1) ; #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
$sheets->Calculate;
$cell = $sheets->Cells(13,5) ; #Select the cell (Row Column number)
$number = Number_format($cell->value);
print "New Total cost =$$number - was ,732 before.";
$ex->application->ActiveWorkbook->Close("False");
$ex->Quit();
unset($ex);
?>
</pre>
这段代码可以打开已经存在的excel文件,并且可以进行读写操作。
<?
$exapp = new COM("Excel.application") or Die ("Did not connect");
$wk = "C:/web/test.xls";
$ws = "test";
$cs = "Daily SMS Usage";
$wkb = $exapp->application->Workbooks->Open($wk) or Die ("Did not open workbook");
$sheets = $wkb->Worksheets($ws) or Die("Did not open the sheet");
$exapp->Visible = 1;
$sheets->activate;
?>