PHP操作excel的一个例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php
2
3 //定义一个excel文件
4 $workbook = "C:/My Documents/test.xls";
5 $sheet = "Sheet1";
6
7 //生成一个com对象 $ex
8 $ex = new COM("Excel.sheet") or Die ("连不上!!!");
9
10 //打开一个excel文件
11 $book = $ex->application->Workbooks->Open( $workbook) or Die ("打不开!!!");
12
13 $sheets = $book->Worksheets( $sheet);
14 $sheets->activate;
15
16 //获取一个单元格
17 $cell = $sheets->Cells(5,5);
18 $cell->activate;
19 //给该单元格赋值
20 $cell->value = 999;
21
22 //保存为另一文件newtest.xls
23 $ex->Application->ActiveWorkbook->SaveAs("newtest.xls");
24
25 //关掉excel,如果想看效果,则注释掉下面两行,由用户手动关掉excel
26 $ex->Application->ActiveWorkbook->Close("False");
27 unset ($ex);
28
29 ?>