PHP Write Excel Style & Format (Excel.Application) |
PHP Write Excel Style & Format (Excel.Application) ตัวอย่างนี้จะเป็นการเขียน PHP กับ Excel เพื่อเขียนข้อมูลลงใน Excel โดยกำหนดรูปแบบข้อความและรูปแบบเซลล์ (Excel Format & Style)
Config Excel.Application
phpWriteExcelFormatAndStyle.php
<html>
<head>
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
</head>
<body>
<?php
//*** 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 = "ThaiCreate.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 ThaiCreate.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 Live";
$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 ***//
@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="<?php echo $strFileName?>">Click here</a> to Download.
</body>
</html>
เพิ่มเติม
$xlApp->ActiveSheet->Cells(9,1)->Characters(1, 13)->Font->Bold = True;
// ใช้กำหนดความหนาที่ระบุตำแหน่งภายใน Cell
Screenshot
|