How to use PHP & Open Excel on Specified Worksheet (.xls) (Excel.Application) The Learn / tutorial php programming how to using PHP Open Excel on Specified Worksheet.
ShotDev Focus:
- PHP & Open Excel on Specified Worksheet.
Example
php_open_write.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? //*** Get Document Path ***// $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp $xlEdgeLeft = 7; $xlEdgeTop = 8; $xlEdgeBottom = 9; $xlEdgeRight = 10; //*** Excel Open Document ***// $strOpenDoc = "Excel.xls"; //*** Excel Document Root ***// $strFileName = "MyXls/MyExcel.xls"; //*** Connect to Excel.Application ***// $xlApp = new COM("Excel.Application"); $xlBook = $xlApp->Workbooks->Open($strPath."/".$strOpenDoc); $xlSheet1 = $xlBook->Worksheets(1); $xlApp->Application->Visible = False; //*** Sheet 1 ***// $xlSheet1->Name = "My Sheet1"; //*** Write text to Row 3 Column 1 ***// for($i = 3;$i <= 10;$i++) { $xlApp->ActiveSheet->Cells($i,1)->Value = "Data 1"; $xlApp->ActiveSheet->Cells($i,2)->Value = "Data 2"; $xlApp->ActiveSheet->Cells($i,3)->Value = "Data 3"; $xlApp->ActiveSheet->Cells($i,4)->Value = "Data 4"; } @unlink($strFileName); //*** Delete old files ***// $xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***// //*** Close & Quit ***// $xlApp->Application->Quit(); $xlApp = null; $xlBook = null; $xlSheet1 = null; ?> Excel Created <a href="<?=$strFileName?>">Click here</a> to Download. </body> </html>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_open_write.php
Screenshot