5

我有一个 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(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)

该进程正在使用用户 NT AUTHORITY\NETWORK SERVICE 运行。IIS 配置为使用匿名身份验证。匿名用户是管理员,我这样设置是为了让应用程序运行而不必担心权限问题。

在我的开发机器中,我有 Office 2010 beta1。我也在带有office 2007的电脑上测试了可执行文件。如果我从服务器中的可执行文件运行代码,并安装了 Office 2003,它运行完美。

为确保权限不会出现任何问题,服务器中的每个人都可以完全访问该网站。该网站在 IIS7 和经典模式下运行。

我还听说 Open-office 有一个 API 应该能够做到这一点,但我找不到任何关于它的信息。我不介意使用 DLLImport 来做我必须做的事情,我可以在 Web 服务器上安装 open-office。不用担心重写这个方法,只要参数相同,一切都会工作。

我感谢您的帮助。

4

6 回答 6

10

永远不要在 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

于 2010-05-19T06:09:32.397 回答
1

在我的情况下(将演示文稿转换为视频,并且我对开放的东西有非常相似的问题)这有助于:

创建以下目录:

C:\Windows\SysWOW64\config\systemprofile\Desktop

C:\Windows\System32\config\systemprofile\Desktop(我没有这个)

我的操作系统 Windows Server 2012 R2 Standard

于 2014-06-04T10:06:59.733 回答
1

考虑在服务器上使用 Office 自动化时,您可能需要阅读以下知识库文章中的信息。

http://support.microsoft.com/kb/257757

来自上述链接的关键短语

Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定的行为和/或在此环境中运行 Office 时出现死锁。

而且我可以确认以上是非常真实的,而不仅仅是MS试图阻止你......

于 2010-04-17T18:01:29.227 回答
0

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.

于 2011-12-02T15:56:14.323 回答
0

互操作库要求将相关应用程序安装在运行它们的机器上。即,为了在您的生产服务器上工作,您需要在该服务器上安装 Office。这通常是一个糟糕的想法。设置类型存在许多问题,因为每次有人提出请求时,都会启动相关应用程序的新实例(在您的情况下为 PowerPoint)。

如果要在中央服务器上解析 PowerPoint 文件,则应使用读取 PowerPoint 文件的库。我知道Aspose生产这样的产品(Aspose.Slides),但也有其他产品。

如果文档位于 PowerPoint 2007 中,您可能能够解析 Open Office XML 以获得所需的数据。有关更多信息,请参阅Office (2007) Open XML File FormatsOpen XML SDK 2.0 for Microsoft Office简介。

于 2010-04-16T15:09:32.347 回答
0

我可能错了,最近开始研究类似的事情。但是您不需要在机器上安装 office 来创建或编辑 2007+ office 文件。它们基本上是具有不同扩展名的 zip 文件。如果将其重命名为 .zip,您应该会看到所有文件并能够直接对其进行编辑。

然后可以解压缩它,所有文件都在那里。您可以以类似的方式创建新文件

于 2010-04-16T16:21:49.630 回答