简体   繁体   English

服务器上MS Office的远程自动化

[英]Remoting Automation of MS Office on the server

I'm warning you, that yhis question will seems to be very strange for a lot of people:) But I have to post it because my project manager is teeling me that a technical solution exist, even if for me it doesn't. 我警告您,对于许多人来说,他的问题似乎很奇怪:)但是我必须发布该问题,因为我的项目经理认为我存在技术解决方案,即使对我而言也不存在。

What we have: 我们有什么:

  • A Windows 7 Console Application with no UI, with our C# application running and no Office and Interop on it Windows 7控制台应用程序,没有UI,运行我们的C#应用​​程序,并且没有Office和Interop
  • a Windows 2012 server, with Ms Office 2010 + Interop installed on it (also with IIS and .NET of course) Windows 2012服务器,上面装有Ms Office 2010 + Interop(当然还有IIS和.NET)

What my PM want (and I told him it is not possible) : 我的总理想要什么(我告诉他这是不可能的):

  • From my C# client application 从我的C#客户端应用程序
  • Automate "Ms office" installed on the server 自动化服务器上​​安装的“ Ms office”
  • Automate means "Save" or "print" a doc file to a network printer. 自动化意味着将文档文件“保存”或“打印”到网络打印机。
  • Of course the Ms office process had to run on the server 当然,女士办公室流程必须在服务器上运行

This kind of solution of "remote Ms Office automation is possible" seems to be impossible for me. 对我来说,这种“可以实现Office远程女士自动化”的解决方案似乎是不可能的。 But maybe I am wrong, it can be possiblbe using DCOM, WCF, or something else? 但是也许我错了,可以使用DCOM,WCF或其他东西吗?

Anyone can confirm I am right please ;) 任何人都可以确认我是对的;)

As you already learned from the comments, automating the desktop version of any of the Office applications is bad for several reasons. 正如您已经从注释中了解到的那样,由于某些原因,自动化任何Office应用程序的桌面版本都是很糟糕的。 The details can be found in the Knowledge base article KB257757 Considerations for server-side Automation of Office . 有关详细信息,请参见知识库文章KB257757 Office的服务器端自动化注意事项 The main take away from that article is: 该文章的主要内容是:

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. Microsoft当前不建议也不支持任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。

But as you are still insisting, consider the following example as a very simple, naive, not to be used near production proof-of-concept that enables you to quickly run into all problems mentioned in the KB article. 但是,正如您仍然坚持认为的那样,请考虑以下示例,这是一个非常简单,幼稚的示例,不能在生产概念验证时使用,它使您可以快速解决知识库文章中提到的所有问题。

In a fresh solution create a WCF Service application and a Console Application. 在全新的解决方案中,创建WCF服务应用程序和控制台应用程序。 In the WCF application add the following interface: 在WCF应用程序中,添加以下接口:

[ServiceContract]
public interface IPrintService
{
    [OperationContract]
    string Print(Stream wordDoc);
}

and have a service implement that. 并有一个服务来实现。 Make sure to add a reference to Microsoft.Office.Interop.Word that you can find in the COM tab of the Add Reference dialog. 确保添加对Microsoft.Office.Interop.Word的引用,您可以在“添加引用”对话框的“ COM”选项卡中找到该引用。

public class PrintService : IPrintService
{
    public string Print(Stream wordDocStream)
    {
        // copy our stream to a local file
        var tempFile = Path.GetTempFileName();
        using(var file = File.Create(tempFile))
        {
            wordDocStream.CopyTo(file);
        }

        // start word
        var wordApp = new Microsoft.Office.Interop.Word.Application();
        // setup printer
        wordApp.ActivePrinter = "Canon LBP3010/LBP3018/LBP3050";
        // open, collect data, print and close
        var doc = wordApp.Documents.Open(tempFile);
        doc.PrintOut();
        var res = doc.Words.Count;
        doc.Close(false);

        // quit word
        wordApp.Quit(false);
        // delete temp file
        File.Delete(tempFile);
        return String.Format("{0} words", res);
    }
}

You can see here a barebone solution to print a document that is sent as a stream to the Service. 您可以在此处看到一个准系统解决方案,用于打印作为流发送到服务的文档。 The service copies the stream to a file, starts Word, Opens the file, prints the document, get some data from the document and tears down and cleans up hen finished. 该服务将流复制到文件,启动Word,打开文件,打印文档,从文档中获取一些数据,然后拆解并清理干净。

The client is straight forward: 客户很直接:

using(var client = new PrintService.PrintServiceClient())
{
    using(var file = File.Open(@"small.docx", FileMode.Open))
    {
        var response = client.Print(file);
        Console.WriteLine(response);
    }
}

This is technically all there is that is needed to print a Word document from a service. 从技术上讲,这就是从服务中打印Word文档所需要的全部。 This runs without much problems on the dev server. 这在开发服务器上运行没有很多问题。 If you run this on IIS you'll probably have to make sure that the account used as an identity in the AppPool is a "user" that can start Word, is allowed to access the printers etc. I already ran into one known issue: I used the XPS Print driver which caused a dialog to popup. 如果您在IIS上运行此程序,则可能必须确保在AppPool中用作标识的帐户是可以启动Word的“用户”,允许其访问打印机等。我已经遇到一个已知问题:我使用XPS打印驱动程序,导致弹出对话框。 That is something you can't have on a server and there is no real way to prevent or detect that. 这是您在服务器上无法做到的,并且没有阻止或检测到这种情况的真正方法。

Remember that this service interface only allows for a stream to be sent. 请记住,此服务接口仅允许发送流。 If you want to add extra data you'll have to use a message contract as is explained on msdn in Large Data and Streaming . 如果要添加额外的数据,则必须使用消息合同,如大数据和流媒体中的 msdn所述。 Your contract would have to look like this in that case: 在这种情况下,您的合同必须如下所示:

[MessageContract]
public class UploadStreamMessage
{
   [MessageHeader]
   public string appRef;
   [MessageBodyMember]
   public Stream data;
} 

If you run all this, (stress)test, consider deployment and install I'm sure you'll convince anyone that this isn't a good idea. 如果运行所有这些(压力)测试,请考虑部署并安装。我敢肯定,您会说服任何人这不是一个好主意。

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

相关问题 通过Office Automation或其他方式检查MS Office许可证状态 - Check MS Office license status via Office Automation or with other way MS Office 自动化:在宏运行时收到通知(事件) - MS Office Automation: Get notified (Event) when a macro runs MS Word Office Automation - 填写文本表单字段和复选框表单字段和邮件合并 - MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge 使用PublishObjects.Add从Excel到Word的MS Office Automation传输表 - MS Office Automation transfer table from excel to word using PublishObjects.Add 如何像使用MS-Office应用程序一样使用自动化来启动我的应用程序 - howto launch my application like MS-Office applications do using automation 如何使用矢量 + Ms Graph 和 C# office interop 词库在词自动化中绘制图表(图表) - How to Draw diagrams (Charts) in word automation using vectors + Ms Graph with C# office interop word library Office自动化的DCOM故障 - DCOM Failure of Office Automation IT-Hit WebDAV服务器检出模式以及与MS Office的交互 - IT-Hit WebDAV server checkout mode and interaction with MS Office .net远程并发服务器 - .net Remoting Concurrent server 服务器上需要MS Office才能使用Microsoft.Excel.interop - Is MS office required on server to use Microsoft.Excel.interop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM