Skip to content

Instantly share code, notes, and snippets.

@BrunoAssis
Created May 11, 2013 01:28
Show Gist options
  • Save BrunoAssis/5558544 to your computer and use it in GitHub Desktop.
Save BrunoAssis/5558544 to your computer and use it in GitHub Desktop.
Excel macro example in PHP.
<?php
// set up excel
$excel = new COM("excel.application") or die("Unable to instantiate excel");
// run Excel silently, since we don’t want dialog boxes popping up in the background
$excel->DisplayAlerts = false;
// open up Excel file and select the first sheet, which contains the inputs
$excel->Workbooks->Add();
$book = $excel->ActiveWorkbook;
$sheets = $book->Sheets;
$sheet = $book->Worksheets(1);
$cell = $sheet->Cells(1, 1);
$cell->Activate;
$cell->value = "myValue";
$input = array("kalle", "Anka", "And", "Duck");
// input stuff into excel spreadsheet
for ($i = 0; $i < count($input); $i++) {
$cell = $sheet->Cells(1, $i + 1);
$cell->Activate;
$cell->value = $input[$i];
}
// run macro
$excel->Run("runModel");
// save spreadsheet
$book->SaveAs("test1.xls");
// quit Excel and clean up
$book->Close(false);
unset($sheets);
$excel->Workbooks->Close();
unset($book);
$excel->Quit;
unset($excel);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment