PHPを使用してMicrosoft Officeドキュメントを作成する方法

PHPを使用してExcel、Word、およびPowerPointを構築するには、主に2つの方法があります。 最初-COMライブラリ(Windowsサーバーのみ)を使用し、もう1つ-CSVやHTMLなどのより標準化された形式を使用します。



Word文書の動的作成:



<?php

$word = new COM("word.application");



$word->Visible = 0;

$word->Documents->Add();

$word->Selection->PageSetup->LeftMargin = '2"';

$word->Selection->PageSetup->RightMargin = '2"';



//Setup the font

$word->Selection->Font->Name = 'Verdana';

$word->Selection->Font->Size = 8;



//Write some text

$word->Selection->TypeText("This is a test document");

//Save the document as DOC file

$word->Documents[1]->SaveAs("c:\\docs\\test1.doc");



//quit and release COM resources

$word->quit();

$word->Release();

$word = null;



?>








Excelドキュメントを動的に作成します。



<?php

$excel = new COM("excel.application");

$excel->Visible = 0;



//Create a new workbook

$wkb = $excel->Workbooks->Add();

$sheet = $wkb->Worksheets(1);



//This code adds the text 'myvalue' on row 2, column 4

$sheet->activate;

$cell = $sheet->Cells(2,4);

$cell->Activate;

$cell->value = 'myvalue';



$wkb->SaveAs("C:\docs\test.xls");



//close and free resources

$wkb->Close(false);

$excel->Workbooks->Close();

$excel->Quit();

?>






PowerPointプレゼンテーションの動的作成



<?php

$powerpnt = new COM("powerpoint.application");

//Creating a new presentation

$pres=$powerpnt->Presentations->Add();

//Adds the first slide. "12" means blank slide

$pres->Slides->Add(1,12);

//Adds another slide. "10" means a slide with a clipart and text

$pres->Slides->Add(2,10);

//Adds a textbox

$pres->Slides[1]->Shapes->AddTextbox(1,20,50,300,40);

//Save the document as PPT file

$powerpnt->Presentations[1]->SaveAs("C:\Docs\test1.ppt");



//free resources and quit powerpoint

$powerpnt->quit();

?>









Word、Excel、Powerpointの機能を見つける方法



テキストは、PHPからMicrosoft Officeコンポーネントを操作するための関数を見つける方法をさらに説明しています。



  1. Microsoft Word、Excel、またはPowerPointを開きます
  2. Alt + F11を押してVisual Basic Editorを起動します
  3. F2を押す
  4. 左側の「ThisDocument」を見つけます。 右側のフレームに、COMオブジェクトに使用できる変数と関数が表示されます





All Articles