How to use PHP & Add Style & Format into excel (Excel.Application) The Learn / tutorial php programming how to using PHP Add Style & Format into excel.
ShotDev Focus:
- PHP & Add Style & Format into excel.
Example
php_format_style.php
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "MyXls/MyExcel.xls";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
$xlSheet1 = $xlBook->Worksheets(1);
$xlApp->Application->Visible = False;
//*** Create Sheet 1 ***//
$xlBook->Worksheets(1)->Name = "My Sheet1";
$xlBook->Worksheets(1)->Select;
//*** Width & Height (A1:A1) ***//
$xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 40.0;
$xlApp->ActiveSheet->Range("A1:A1")->RowHeight = 25.0;
//*** Write text to Row 1 Column 1 ***//
$xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com ";
$xlApp->ActiveSheet->Cells(1,1)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(1,1)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(1,1)->VerticalAlignment = -4108; //*** Center Rows ***//
$xlApp->ActiveSheet->Cells(1,1)->HorizontalAlignment = -4108; //*** Center Column ***//
$xlApp->ActiveSheet->Cells(1,1)->Font->Size = 12;
//*** Write text to Row 1 Column 2 ***//
$xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram ";
$xlApp->ActiveSheet->Cells(1,2)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(1,2)->Font->Size = 20;
//*** Width & Height (A2:A2) ***//
$xlApp->ActiveSheet->Range("A2:A2")->BORDERS->Weight = 1; //*** Border ***//
//*** Write text to Row 1 Column 2 ***//
$xlApp->ActiveSheet->Cells(2,1)->Value = "I Love ShotDev.Com ";
$xlApp->ActiveSheet->Cells(2,1)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(2,1)->Font->Size = 10;
$xlApp->ActiveSheet->Cells(2,1)->HorizontalAlignment = 4;
//*** Width & Height (A3:D3) ***//
$xlApp->ActiveSheet->Range("A3:D3")->BORDERS->Color = 44; //*** Border Color ***//
$xlApp->ActiveSheet->Range("A3:D3")->BORDERS->Weight = 1; //*** Border ***//
$xlApp->ActiveSheet->Range("A3:D3")->MergeCells = True; //*** Merge Cells ***//
//*** Write text to Row 1 Column 2 ***//
$xlApp->ActiveSheet->Cells(3,1)->Value = "I Love My Live";
$xlApp->ActiveSheet->Cells(3,1)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(3,1)->Font->Size = 10;
$xlApp->ActiveSheet->Cells(3,1)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,1)->Interior->ColorIndex = 44; //*** Background Color ***//
//*** Write text to Row 4 Column 5 ***//
$xlApp->ActiveSheet->Cells(4,5)->Value = "My Life";
$xlApp->ActiveSheet->Cells(4,5)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(4,5)->Font->Size = 10;
$xlApp->ActiveSheet->Cells(4,5)->Font->Italic = True;
$xlApp->ActiveSheet->Cells(4,5)->Font->ColorIndex = 4;
$xlApp->ActiveSheet->Cells(4,5)->EntireColumn->AutoFit; //*** AutoFit Column ***//
//*** Write text to Row 5 Column 5 ***//
$xlApp->ActiveSheet->Cells(5,5)->Value = "My Life";
$xlApp->ActiveSheet->Cells(5,5)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(5,5)->Font->Size = 10;
$xlApp->ActiveSheet->Cells(5,5)->Font->Italic = True;
$xlApp->ActiveSheet->Cells(5,5)->Font->ColorIndex = 4;
$xlApp->ActiveSheet->Cells(5,5)->HorizontalAlignment = -4152; // Text align Right
$xlApp->ActiveSheet->Cells(5,5)->EntireColumn->AutoFit; //*** AutoFit Column ***//
//*** Write text to Row 6 Column 1 ***//
$xlApp->ActiveSheet->Cells(6,1)->Value = "Version 2010";
$xlApp->ActiveSheet->Cells(6,1)->Font->Name = "Tahoma";
$xlApp->ActiveSheet->Cells(6,1)->Font->Size = 10;
$xlApp->ActiveSheet->Cells(6,1)->Characters(1, 7)->Font->ColorIndex = 4;
$xlApp->ActiveSheet->Cells(6,1)->Characters(8, 12)->Font->Bold = True;
/*
// SheetType
xlChart = -4109;
xlWorksheet = -4167;
// WBATemplate
xlWBATWorksheet = -4167;
xlWBATChart = -4109;
// Page Setup
xlPortrait = 1;
xlLandscape = 2;
xlPaperA4 = 9;
// Format Cells
xlBottom = -4107;
xlLeft = -4131;
xlRight = -4152;
xlTop = -4160;
// Text Alignment
xlHAlignCenter = -4108;
xlVAlignCenter = -4108;
// Cell Borders
xlThick = 4;
xlThin = 2;
*/
@unlink($strFileName); //*** Delete old files ***//
$xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//
//$xlBook->SaveAs(realpath($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_format_style.php
Screenshot




1southwest…
…