You can use COM to operate Excel in PHP and control Excel in PHP. The following is a simple example to demonstrate how PHP interacts with Excel (the example contains detailed notes ):
1 2 // specify the page encoding to prevent Chinese garbled characters
3 header ('content-type: text/html; charset = gb2312 ');
4
5 // start Excel
6 $ ms_excel = new COM ("excel. application") or die ("Excel application cannot be opened ");
7
8 // display the current Excel version on the webpage
9 echo "Excel Version: {$ ms_excel-> Version} \ n ";
10
11 // Create a workbook
12 $ ms_excel-> Application-> Workbooks-> Add () or die ("New Workbooks cannot be added ");
13
14 // enter text in cell A1 in worksheet Sheet1 of The Workbook
15 $ ms_excel-> Worksheets ("Sheet1")-> Range ("A1")-> Value = "test ";
16
17 // Save the workbook. If no path is specified, it is saved in my document by default.
18 $ ms_excel-> Workbooks (1)-> SaveAs ("php_excel_test.xls ");
19
20 // Close The Workbook
21 $ ms_excel-> Quit ();
22
23 // clear the object
24 $ ms_excel = null;
25?>
In the htdocs folder, save the file as "excelsample. php ". Open your browser and enter:
Http: // localhost/excelsample. php
The current Excel worksheet is displayed on the page. a workbook named php_excel_test.xls is created at the same time and "test" is entered in cell A1 of sheet 1 ".
Open the work bookphp_excel_test.xls. in cell A1, you will see that "test" has been entered ".
Note: To prevent garbled characters, the encoding format is set at the beginning of the code, and the file is saved in gb2312 format when saving the file.