ASP.NET 中的办公自动化问题。如果我知道如何的话,我可以使用开放式办公室等替代方案
我有一个 ASP.NET 2.0 Web 应用程序,它应该上传一个 ppt 文件,然后将其幻灯片提取为图像。为此,我导入了 office.dll 和 Microsoft.Office.Interop.PowerPoint.dll 程序集,并编写了以下代码。
public static int ExtractImages(string ppt, string targetPath, int width, int height)
{
var pptApplication = new ApplicationClass();
var pptPresentation = pptApplication.Presentations.Open(ppt, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
var slides = new List<string>();
for (var i = 1; i <= pptPresentation.Slides.Count; i++)
{
var target = string.Format(targetPath, i);
pptPresentation.Slides[i].Export(target, "jpg", width, height);
slides.Add(new FileInfo(target).Name);
}
pptPresentation.Close();
return slides.Count;
}
如果我在本地计算机、asp.net 或可执行文件中运行此代码,它会完美运行。但是如果我尝试在生产服务器中运行它,则会出现以下错误:
System.Runtime.InteropServices.COMException (0x80004005): PowerPoint 无法打开该文件。在 Microsoft.Office.Interop.PowerPoint.Presentations.Open(字符串 文件名、MsoTriState 只读、MsoTriState 无标题、MsoTriState WithWindow) 在 PPTImageExtractor.PptConversor.ExtractImages(String caminhoPpt,字符串 caminhoDestino,Int32 largura,Int32 altura,字符串 caminhoThumbs、Int32 larguraThumb、Int32 alturaThumb、布尔 geraXml) 在Upload.ProcessRequest(HttpContext上下文)
该进程正在使用用户 NT AUTHORITY\NETWORK SERVICE 运行。 IIS 配置为使用匿名身份验证。匿名用户是管理员,我这样设置是为了让应用程序运行而不必担心权限问题。
在我的开发机器上,我有 Office 2010 beta1。我也在装有 Office 2007 的电脑上测试了可执行文件。如果我在安装了 Office 2003 的服务器上运行可执行文件中的代码,它就会完美运行。
为了确保不会出现任何权限问题,服务器中的每个人都具有对该网站的完全访问权限。该网站正在 IIS7 和经典模式下运行。
我还听说 Open-office 有一个 API 应该可以做到这一点,但我找不到任何相关信息。我不介意使用 DLLImport 来做我必须做的事情,并且我可以在 Web 服务器上安装 open-office。不用担心重写这个方法,只要参数相同,一切都会起作用。
我很感激你的帮助。
I have a ASP.NET 2.0 web application that should upload a ppt file and then extract its slides to images. For that I have imported office.dll and Microsoft.Office.Interop.PowerPoint.dll assemblies and wrote the following code
public static int ExtractImages(string ppt, string targetPath, int width, int height)
{
var pptApplication = new ApplicationClass();
var pptPresentation = pptApplication.Presentations.Open(ppt, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
var slides = new List<string>();
for (var i = 1; i <= pptPresentation.Slides.Count; i++)
{
var target = string.Format(targetPath, i);
pptPresentation.Slides[i].Export(target, "jpg", width, height);
slides.Add(new FileInfo(target).Name);
}
pptPresentation.Close();
return slides.Count;
}
If I run this code in my local machine, in asp.net or a executable, it runs perfectly. But If I try running it in the production server, I get the following error:
System.Runtime.InteropServices.COMException (0x80004005): PowerPoint
could not open the file. at
Microsoft.Office.Interop.PowerPoint.Presentations.Open(String
FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState
WithWindow) at PPTImageExtractor.PptConversor.ExtractImages(String
caminhoPpt, String caminhoDestino, Int32 largura, Int32 altura, String
caminhoThumbs, Int32 larguraThumb, Int32 alturaThumb, Boolean geraXml)
at Upload.ProcessRequest(HttpContext context)
The process is running with the user NT AUTHORITY\NETWORK SERVICE. IIS is configured to use anonymous authentication. The anonymous user is an administrator, I set it like this to allow the application to run without having to worry about permissions.
In my development machine I have office 2010 beta1. I have tested with the executable in a pc with office 2007 as well. And if I run the code from the executable in the server, with office 2003 installed, it runs perfectly.
To ensure that there wouldn't be any problems with permissions, everyone in the server has full access to the web site. The website is running in IIS7 and Classic Mode.
I also heard that Open-office has an API that should be able to do this, but I couldn't find anything about it. I don't mind using DLLImport to do what I have to do and I can install open-office on the web server. Don't worry about rewriting this method, as long as the parameters are the same, everything will work.
I appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
切勿从 ASP.NET 应用程序使用 Office Interop。请参阅Office 服务器端自动化注意事项。
但是,如果您没有其他选择,则在 Windows Server 2008 上,您需要创建以下目录才能使其正常工作:
Windows 2008 Server x64:C:\Windows\SysWOW64\config\systemprofile\Desktop
Windows 2008 Server x86:C:\Windows\System32\config\systemprofile\Desktop
Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of Office.
However, if you have no alternative, on Windows Server 2008 you need to create the following directories to get this to work:
Windows 2008 Server x64: C:\Windows\SysWOW64\config\systemprofile\Desktop
Windows 2008 Server x86: C:\Windows\System32\config\systemprofile\Desktop
当考虑在服务器上使用 Office Automation 时,您可能需要阅读以下知识库文章中的信息。
http://support.microsoft.com/kb/257757
上述链接中的关键短语
Microsoft 当前不建议也不支持通过任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)实现 Microsoft Office 应用程序的自动化,因为 Office 可能会表现出不稳定在此环境中运行 Office 时的行为和/或死锁。
我可以确认上述内容是正确的,而不仅仅是 MS 试图阻止您......
When considering using Office Automation on the server, you might want to take a read through the info in the following KB article.
http://support.microsoft.com/kb/257757
A key phrase from the above link
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
And I can confirm that the above is very true, and not just MS trying to discourage you...
在我的情况下(将演示文稿转换为视频,并且我对打开的内容有非常相似的问题)这会有所帮助:
创建以下目录:
C:\ Windows \ SysWOW64 \ config \ systemprofile \ Desktop
C:\ Windows \ System32 \ config \ systemprofile \桌面(我没有这个)
我的操作系统 Windows Server 2012 R2 Standard
In my situation (converting presentation to video, and I have very similar problem with open stuff) this helps:
Creating the following directories:
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop (I don't have this)
My operating system Windows Server 2012 R2 Standard
Interop 库要求将相关应用程序安装在运行它们的计算机上。即,为了使其在您的生产服务器上运行,您需要在该服务器上安装 Office。这通常是一个糟糕的主意。设置类型存在许多问题,因为每次有人发出请求时,都会启动相关应用程序的新实例(在您的情况下为 PowerPoint)。
如果您想在中央服务器上解析 PowerPoint 文件,那么您应该使用读取 PowerPoint 文件的库。我知道 Aspose制作这样的产品(Aspose.Slides),但还有其他产品。
如果文档位于 PowerPoint 2007 中,您也许能够解析 Open Office XML 以获取所需的数据。请参阅Office (2007) Open XML 文件格式简介和适用于 Microsoft Office 的 Open XML SDK 2.0< /a> 了解更多。
The Interop libraries require that the application in question be installed on the machine from which they are running. I.e., in order for this to work on your production server, you will need to have Office installed on that server. This is generally an awful idea. There are many issues with type of setup as each time someone makes a request, a new instance of the application in question (in your case PowerPoint) is launched.
If you want to parse PowerPoint files on a central server, then you should use a library that reads PowerPoint files. I know that Aspose makes such product (Aspose.Slides) but there are others as well.
If the documents are in PowerPoint 2007, you might be able to parse the Open Office XML for the data you want. See Introducing the Office (2007) Open XML File Formats and Open XML SDK 2.0 for Microsoft Office for more.
我可能是错的,最近开始研究类似的事情。但您不需要在计算机上安装 Office 来创建或编辑 2007+ Office 文件。它们基本上是具有不同扩展名的 zip 文件。如果将其重命名为 .zip,您应该会看到所有文件并能够直接编辑它们。
然后可以将其解压缩,所有文件都在其中。您可以以类似的方式创建新文件
I might be wrong, started looking into a similar thing recently. But you should not need office installed on a machine to create or edit 2007+ office files. They are basically zip files with a different extention. If you rename it to .zip you should see all the files and be able to edit them directly.
It can then be unzipped and all the files are there for you. You can create new files in a similar fashion
切勿从 ASP.NET 应用程序使用 Office Interop。请参阅Office 服务器端自动化注意事项。
不过,我只是想在此指出,我必须在装有 Office 2010 的 Windows 7 Professional 64 位计算机上执行 Murthy Pantham 描述的修复。它没有有 Windows 7 Service Pack 1 或 Office 2010已安装 Service Pack 3。因此,这里的教训显然是修复不仅限于 Server 2008。希望这对某人有帮助。
Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of Office.
However, I just wanted to note here that I had to do the fix described by Murthy Pantham on a Windows 7 Professional 64 bit machine with Office 2010. It did not have Windows 7 Service Pack 1 or Office 2010 Service Pack 3 installed. So the lesson here is apparently that fix is not limited to Server 2008. Hope this helps someone.