Working with Excel instance code in PHP
- 2020-03-31 20:42:35
- OfStack
<?php
//Specify the page code to prevent the occurrence of Chinese garbled code
header('Content-type: text/html; charset=gb2312');
//Start the Excel
$ms_excel=new COM("excel.application") or die(" Can't open Excel The application ");
//Displays the current version of Excel in the web page
echo "Excel version :{$ms_excel->Version}n";
//Create a new workbook
$ms_excel->Application->Workbooks->Add() or die(" Cannot add a new workbook ");
//In Sheet1 of the workbook, enter the text in cell A1
$ms_excel->Worksheets("Sheet1")->Range("A1")->Value=" test ";
//Save the workbook, if no path is specified, in my document by default
$ms_excel->Workbooks(1)->SaveAs("php_excel_test.xls");
//Close workbook
$ms_excel->Quit();
//Empty the object
$ms_excel=null;
?>
Save the file as "excelsample.php" in the htdocs folder. Open the browser and type in the address bar:
http://localhost/excelsample.php
At this point, the current version of Excel is displayed on the page, and a workbook called php_excel_test.xls is created and the word "test" is entered in Sheet1, cell A1.
Open the workbook php_excel_test.xls, and in cell A1 you will see that "test" has been entered.
Note: in order to prevent the occurrence of garbled code, the coding format is set at the beginning of the code, and the file is saved as gb2312 format.