PHP无法通过COM打开Excel工作簿 [英] PHP fails to open an Excel workbook through COM

查看:205
本文介绍了PHP无法通过COM打开Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码正常工作,直到到达打开的工作簿行,然后失败。

 <?PHP 

$ file =C:\\xampp\\\ \\htdocs\\php\\test.xls;
openWorkbook($ file);

函数openWorkbook($文件)
{
$ excel =新的COM(Excel.Application)或死(错误:无法即刻COM!\ r \\ \
);
echoApplication name:{$ excel-> Application-> value} \r\\\
;
echoLoaded version:{$ excel-> Application-> version} \r\\\
;

//在这行失败!
$ Workbook = $ excel-> Workbooks-> Open($ file)or die(ERROR:Unable to open。$ file。!\r\\\
);





$ b

有没有人有任何建议,为什么这可能会发生?我已经看了很多其他的帖子,但没有人似乎有这个具体问题。

解决方案

我会假设为什么需要使用COM而不是像 PHPExcel 这样的第三方库有一些原因。如果没有具体的原因,考虑切换到一个这些尝试,测试和相当好的支持替代品。






你的代码似乎是正确的,我只能想象你有某种权限问题。

从PHP中的MSO应用程序中提取合理的错误信息是相当困难的通过COM,所以这里有一些你可以尝试的东西:
$ b $ ul
验证文件扩展名( .xls 在你的情况下)匹配文件内的数据 - 确保你没有错误的文件扩展名的CSV文件,例如。 Excel(甚至当通过COM访问时)应该能够处理这个问题,但是如果你在Excel应用程序中打开这个文件,它会引发一个警告。

  • 在Excel中打开文件,键入在一个空单元格中,删除你刚输入的内容并保存。这将导致Excel重新编码并将整个文件写回到磁盘,这也可能有助于解决任何损坏的文件问题。

  • 确保文件具有适当的权限。最好的办法是将文件设置为Everyone组的完整权限,如果这样可以解决问题,则可以将权限调整为所需的限制。






  • 这个问题不太可能解决这个问题(文件打开的代码看起来是相同的),但它可能会帮助你一些:前段时间,我写了一个非常小的Excel包装库,通过COM访问,可以在这里找到这里 。它的功能是非常有限的,但更多的方法可以很容易地添加;它主要给聚会带来的是一个稍微干净的API,稍微好一点的错误处理。

    This code works fine until it gets to the open workbook line and then it fails.

     <?PHP
    
    $file = "C:\\xampp\\htdocs\\php\\test.xls";
    openWorkbook($file);
    
    function openWorkbook($file)
    {
    $excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!\r\n");
    echo "Application name: {$excel->Application->value}\r\n";
    echo "Loaded version: {$excel->Application->version}\r\n";
    
    // FAILS ON THIS LINE!
    $Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
    }
    

    Does anyone have any suggestions to why this may be happening? I've looked though a lot of other posts, but no one seems to have this specific issue.

    解决方案

    I shall assume that there is some reason why you need to use COM and not a 3rd-party library like PHPExcel. If there isn't a specific reason for this, consider switching to one these tried, tested and considerably better supported alternatives.


    Your code seems to be correct, I can only imagine you have some kind of permissions problem.

    It can be quite difficult to extract sensible error messages from MSO applications in PHP via COM, so here's a couple of things you can try:

    • Verify that the file extension (.xls in you case) matches the data inside the file - make sure you don't have a CSV file with the wrong file extension, for example. Excel (even when accessed via COM) should be able to handle this, but it will throw up a warning if you open the file in the Excel application.
    • Open the file in Excel, type in an empty cell, delete what you just typed, and save it. This will cause Excel to re-encode and write the entire file back to disk, which may also help resolve any corrupt file issues.
    • Make sure the file has the appropriate permissions on it. The best way to do this would be to set the file as full permissions for the "Everyone" group, and if this fixes the problem you can then adjust the permissions to the required restrictions.

    This is highly unlikely to fix this specific problem (the file open code appears to be identical) but it may assist you a little: some time ago I wrote a very small wrapper library for Excel accessed via COM, which can be found here. Its functionality is very limited, but more methods could be added quite easily; what it mostly brings to the party is a slightly cleaner API and slightly better error handling.

    这篇关于PHP无法通过COM打开Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆