简体   繁体   English

如何从 Excel 调用 Perl 脚本?

[英]How do I call a Perl script from Excel?

I want to call a Perl script from Excel using VBA.我想使用 VBA 从 Excel 调用 Perl 脚本。 I have used a shell command.我使用了 shell 命令。 I have also used the code below:我还使用了以下代码:

Function Chk()
Dim RetVal
RetVal = Shell("C:\Perl\bin\Hello.pl", 1)
End Function

But the above function doesnt run the file.但是上面的 function 并没有运行该文件。 Is there any error?有什么错误吗?

Is there any other way to call a Perl script or external program using Excel?有没有其他方法可以使用 Excel 调用 Perl 脚本或外部程序?

Have a look at ActiveState's Perl extensions.看看 ActiveState 的 Perl 扩展。 They have an "ActivePerl" DLL which runs the Perl interpreter as an ActiveX component which should allow seamless integration with Excel.他们有一个“ActivePerl”DLL,它将 Perl 解释器作为 ActiveX 组件运行,它应该允许与 Excel 无缝集成。

In addition, their OLE extensions allow Perl to access the full Excel OLE Object model so you can manipulate the spreadsheet very precisely. In addition, their OLE extensions allow Perl to access the full Excel OLE Object model so you can manipulate the spreadsheet very precisely.

You'll need to shell out to:您需要将 shell 输出到:

 c:\perl\bin\perl.exe Hello.pl 

to make it run.让它运行。

You could also add this line as the first in your script:您还可以将此行添加为脚本中的第一行:

#!c:\perl\bin\perl.exe

Excel's Shell() function is probably ignoring file associations. Excel 的 Shell() function 可能会忽略文件关联。 You almost certainly need to say something more like:你几乎肯定需要说更多类似的话:

Function Chk()
  Dim RetVal
  Chk = Shell("C:\Perl\bin\perl.exe C:\Perl\bin\Hello.pl", 1)
End Function

to get the effect you wanted.得到你想要的效果。 The two changes to note are first, the explicit invocation of perl.exe, and second, the minor syntax error in your function as written.需要注意的两个更改首先是显式调用 perl.exe,其次是 function 中的小语法错误。

Note that if either the path to perl.exe or the path to your script (or any additional arguments to your script too) has spaces in it, then you will need to form a string that contains double-quote characters quoting them, something like:请注意,如果 perl.exe 的路径或脚本的路径(或脚本的任何其他 arguments 的路径)中有空格,那么您将需要形成一个包含双引号字符的字符串来引用它们,例如:

Function Chk()
  Dim RetVal
  Chk = Shell("C:\Perl\bin\perl.exe ""C:\Some path\with spaces\Hello.pl""", 1)
End Function

would do the trick.会成功的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM