vba 未安装 excel 时运行 Excel 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20139456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Excel macro when excel is not installed
提问by amaturcoder
I have a excel workbook. I need to show it as a HTML page that will be refreshed automatically(say every 2 minutes). For that i need to run the calculations on every sheet and then save as HTML. I will figure out later how to swap the new HTML file for the old one later. I already have a macro in excel that does all the calculations and then saves as HTML every 2 min. All i need to do is call that macro
我有一个excel工作簿。我需要将其显示为将自动刷新的 HTML 页面(例如每 2 分钟)。为此,我需要在每张纸上运行计算,然后另存为 HTML。稍后我将弄清楚如何将新的 HTML 文件替换为旧的。我已经在 excel 中有一个宏可以完成所有计算,然后每 2 分钟保存为 HTML。我需要做的就是调用那个宏
The problem is that the machine on which all this is to take place is a server machine and thus MS office cannot be installed on it. Thus i cannot use Excel COM Inter-op.
问题是,所有这一切发生的机器是服务器机器,因此不能在其上安装 MS Office。因此我不能使用 Excel COM Inter-op。
One was is if i could write a .net program or a Vb Script that will run the macro. But a .net program always uses inter-op.
一个是我是否可以编写一个 .net 程序或一个运行宏的 Vb 脚本。但是 .net 程序总是使用互操作。
Another way is if i could save the book as a HTML and run the macro in the HTML itself(Dont know if the HTML has the VB A code). Here also i found that i require Visual Basic Editor which i cant install( as it comes with excel only).
另一种方法是,如果我可以将这本书另存为 HTML 并在 HTML 本身中运行宏(不知道 HTML 是否具有 VB A 代码)。在这里,我还发现我需要无法安装的 Visual Basic 编辑器(因为它仅随 excel 一起提供)。
I would prefer not to use third party soft wares.
我宁愿不使用第三方软件。
Can any of the above two procedures be implemented in the given conditions? if not is there any other way?
在给定的条件下,是否可以执行上述两个程序中的任何一个?如果没有,还有其他方法吗?
UPDATE:
更新:
Have been looking into open XML for a solution and have finally found that open XML 2.0 cannot run excel calculations as the SDK does not have the calculation engine. Does open XML SDK 2.5 have the calculation engine??
一直在寻找解决方案的 open XML,最终发现 open XML 2.0 无法运行 excel 计算,因为 SDK 没有计算引擎。open XML SDK 2.5 有计算引擎吗??
回答by Christian Sauer
In short: You cannot run a macro without Excel installed. Worse: You should not intsall Excel on a Server. It is not recommended and impossible to do this in a stable way. My best bet would be something like this: Use a :net library which can read xlsx files, like EPPLUS and "translate" this macro into net, which the EPPLUS library as its foundation. Also, this is a lot fastrer than Interop (factor 10 - 100) and fits better into a Server infrastructure with ASP.NET (for example).
简而言之:如果没有安装 Excel,您将无法运行宏。更糟糕的是:您不应该在服务器上安装 Excel。不建议且不可能以稳定的方式执行此操作。我最好的选择是这样的:使用 :net 库,它可以读取 xlsx 文件,如 EPPLUS 并将这个宏“翻译”成网络,EPPLUS 库作为其基础。此外,这比 Interop(因子 10 - 100)快得多,并且更适合使用 ASP.NET 的服务器基础结构(例如)。
回答by Our Man in Bananas
Have you considered putting the macro as a .NET Managed AddIn on a shared machine, executing the macro from the server using a shell command to open Excel on the shared machine and the macro runs on Open.
您是否考虑过将宏作为 .NET Managed AddIn 放在共享机器上,使用 shell 命令从服务器执行宏以在共享机器上打开 Excel,并且宏在 Open 上运行。
Then you can get the macro itself to export the data to HTML and copy the pages to your server.
然后,您可以使用宏本身将数据导出为 HTML 并将页面复制到您的服务器。
As has been said, avoid installing Office on a server (though it has been done in the past, it's never advisable)
如前所述,避免在服务器上安装 Office(尽管过去曾这样做过,但绝不可取)
Most robust option would be to re-write your vba in .NET as a .NET Managed AddIn - then you have more control over processes, but as has been pointed out, NOT on a Server.
最可靠的选择是在 .NET 中将您的 vba 重写为 .NET Managed AddIn - 然后您可以更好地控制进程,但正如已经指出的那样,而不是在服务器上。
回答by amaturcoder
Thanks for the resonses. Found a way to show the excel as HTML in the given conditions.
感谢您的回复。找到了一种在给定条件下将 excel 显示为 HTML 的方法。
Just FYI, I used a project known as NPOI. It is the .net version of POI used in java. It allows me to open edit and save excel workbooks without installing excel. plus it is opensource so i customized it according to my requirements.
仅供参考,我使用了一个名为 NPOI 的项目。它是 Java 中使用的 POI 的 .net 版本。它允许我在不安装 excel 的情况下打开编辑和保存 excel 工作簿。加上它是开源的,所以我根据我的要求定制了它。