doushi1847 2010-11-09 18:44
浏览 141

在Office 2010中使用PHP的COM对象(Excel.Application)

I recently coded a script in PHP using the COM object to strip data out of cells in an Excel 2007 spreadsheet into an array. This was all done locally on an XP system with Office 2007. Once I'd enabled the Apache server to interact with the desktop the script worked like an absolute dream, allowing me to populate my HTML with the array values via PHP.

Now we've just upgraded to Win 7 x64 and Office 2010 32bit - The same script now throws me a COM_exception:

'Microsoft Excel cannot access' the xml/xmlx file (it worked for both previously). "There are several possible reasons:

  • The file name or path does not exist.
  • The file is being used by another program.
  • The workbook you are trying to save has the same name as a currently open workbook"... apparently.

I've disabled UAC thinking that was the culprit and of course allowed Apache to interact with the desktop, but an Excel process doesn't even attempt to start. I'm guessing Windows 7 isn't allowing the script to interact with Excel at all. There are other scripts available using classes (eg PHPExcel) however I would rather avoid writing the recipient code, plus I don't even know if the classes would work for Excel 2010.

How can I overcome this com_exception?

Code:

<?php
error_reporting(E_ALL);

function getDataFromExcel($file, $sheet, $rows, $cols)
{
    // COM CREATE
    fwrite(STDOUT, "----------------------------------------
");
    $excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!
");
    $excel->Visible = true; // so that we see the window on screen
    fwrite(STDOUT, "Application name: {$excel->Application->value}
") ;
    fwrite(STDOUT, "Loaded version: {$excel->Application->version}
");
    fwrite(STDOUT, "----------------------------------------

");

    // DATA RETRIEVAL
    $Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!
");
    $Worksheet = $Workbook->Worksheets($sheet);
    $Worksheet->Activate;
    $i = 0;
    foreach ($rows as $row)
    {
        $i++; $j = 0;
        foreach ($cols as $col)
        {
            $j++;
            $cell = $Worksheet->Range($col . $row);
            $cell->activate();
            $matrix[$i][$j] = $cell->value;
        }
    }

    // COM DESTROY
    $Workbook->Close();
    unset($Worksheet);
    unset($Workbook);
    $excel->Workbooks->Close();
    $excel->Quit();
    unset($excel);

    return $matrix;
}

// define inputs
$xls_path = "D:\\xampp\\htdocs\\path_to_document\\test.xls"; // input file
$xls_sheet = 1; // sheet #1 from file
$xls_rows = range(3, 20, 1); // I want extract rows 3 - 20 from test.xls with 1 row stepping
$xls_cols = array("B", "D", "E", "G"); // I want to extract columns B, D, E and G from the file

// retrieve data from excel
$data = getDataFromExcel($xls_path, $xls_sheet, $xls_rows, $xls_cols);

?>
<html>
<pre>
<?php print_r ($data);?>
</pre> 
</html>
  • 写回答

1条回答 默认 最新

  • dov11020 2010-11-10 11:42
    关注

    I think your problem is most likely that Apache doesn't have rights to the DCOM component. As admin, open DCOMCNFG.EXE. Browser to Computers -> My Computer -> DCOM Config and locate the "Microsoft Excel Application" component. Right click this and select "Properties", then go to the Security tab.

    I'm a bit lazier than I probably should be, so I tend to grant the httpd permissions to all three of the options provided, rather than bother to understand when each one applies. Put the radio button to "Customize", and hit the "Edit" button, the follow the standard Windows dialogs for adding permissions to the user/role your httpd runs as.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图