• PHP Help PHP Coding
  • [RESOLVED] cannot create excel COM object : Fatal error: Uncaught exception 'com_exception'...

Hi guys,

Im trying to access excel file using COM object but still I have been unable to do it. I have tried many ways. I have PHP 5.0.5 which runs in Apache and the operating system is windows XP.

I have simply tried to create the object as below,

<?php
$excelApp = new COM("Excel.Application") or die("Cannot create an Excel object");
?>

but this gave me an error saying,

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `Excel.Application': Server execution failed ' in C:\Program Files\xampp\htdocs\websites\htmlFeed\streaming\com_parser.php:5 Stack trace: #0 C:\Program Files\xampp\htdocs\websites\htmlFeed\streaming\com_parser.php(5): com->com('Excel.Applicati...') #1 {main} thrown in C:\Program Files\xampp\htdocs\websites\htmlFeed\streaming\com_parser.php on line 5

I have tried the same code with another machine and it works fine. the only difference that both machine has is, machine one has installed PHP separatly but my machine I have used xampp (automatic installer that comes with PHP / mysql / Apache ..etc)

I have done some research and it says this can be due to the permission error. so i changed the permission by going,

Control Panel->Administrative Tools->Component Services->Component Services ->Computers->My Computer->DCOM Config->Microsoft excel

But still im getting the same error.

Does anyone knows the reason for this???

Thanks In advance,

Regards,
Niroshan

    Take a look at a Microsoft Article here.

    Please be advised that it does not directly relate to PHP, however there may be varied issues with creating the excel object.

    For these reasons my company chose to purchase a server side excel solution.

    keep the following thigns in mind:

    Newer versions of Office (2000+) have permissions mixed into the barrel, and this MAY be causing your error.

    Another alternative is you try an Excel PHP Class .

    Also, is this on your own Personal Machine? Make sure that you have run excel before on the Terminal (not just over web w/ COM).

    I hope this was of some help...

      Hi big.nerd,

      Thank u very much for the reply. I think there was something wrong with my machine. So i just switched another machine.

      But there was a bug in my system which i was previously done. The bug was even though it created csv file it kept it running on the back ground so even the application terminated i couldn`t able to delete the file. (even manual delete was not possible)

      Anyways after long research finally found the answer.

      here is the answer for all you guys if just in case if you come across this kind of a problem,

      <?php
      $rootPath=$_SERVER['DOCUMENT_ROOT'];
      
      $xlsFile = $rootPath.'test.xls';
      $csvFile = $rootPath.'csv/test.csv';
      
      $xlsObj = new COM("Excel.application") or Die ("Did not connect");
      $xlsObj->DisplayAlerts = false;
      $xlsObj->Workbooks->Open($xlsFile);
      $book = $xlsObj->ActiveWorkbook;
      $sheets = $book->Sheets;
      $sheet = $book->Worksheets(1);
      $sheet->SaveAs($csvFile,6);
      $book->Close(false);
      unset($sheets);
      $xlsObj->Workbooks->Close();
      unset($book);
      $xlsObj->Quit;
      unset($xlsObj);
      ?>
      
      

      Thanks guys,

      Regards,
      Niroshan

        Oh yes, sorry I was unaware that you had it running before.

        You MUST terminate the processes otherwise you will find a rogue excel process (or MANY) running.

        Niroshan, please mark this thread resolved (under Thread Tools).

        This will also help those looking to see how you retreived your answer.

          Write a Reply...