Home  >  Article  >  Backend Development  >  Operating Excel example code in PHP_PHP tutorial

Operating Excel example code in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:58790browse

Copy the code The code is as follows:

//Specify the page encoding to prevent Chinese garbled characters
header('Content-type: text/html; charset=gb2312');

//Start Excel
$ms_excel=new COM("excel.application") or die("Cannot open Excel Application");

//Display the current Excel version on the web page
echo "Excel version: {$ms_excel->Version}n";

//Create a new one Workbook
$ms_excel->Application->Workbooks->Add() or die("Cannot add a new workbook");

//Cell in Sheet1 of the workbook Enter text in cell A1
$ms_excel->Worksheets("Sheet1")->Range("A1")->Value="Test";

//Save the workbook, if If no path is specified, it will be saved in My Documents by default
$ms_excel->Workbooks(1)->SaveAs("php_excel_test.xls");

//Close the workbook
$ms_excel->Quit();

//Clear the object
$ms_excel=null;
?>

Place the file in the htdocs folder Save as "excelsample.php". Open the browser and enter in the address bar:

http://localhost/excelsample.php

At this time, the current Excel version will be displayed on the page, and a file named php_excel_test will be created. .xls workbook and enter "Test" in cell A1 of Sheet1.
Open the workbook php_excel_test.xls, and you will see that "Test" has been entered in cell A1.
Note: In order to prevent garbled characters, the encoding format is set at the beginning of the code, and when saving the file, choose to save it in gb2312 format.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321657.htmlTechArticleCopy the code as follows: ?php //Specify the page encoding to prevent Chinese garbled characters header('Content- type: text/html; charset=gb2312'); //Start Excel $ms_excel=new COM("excel.app...
Statement:
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