简体   繁体   English

立即下载新创建的Word文件,而不是保存到磁盘

[英]Download newly created word file straight away instead of saving to disk

I'm creating a word file using interop and it works fine. 我正在使用互操作创建一个Word文件,它工作正常。 At the moment I'm passing in a filepath to create the new file from after find and replacing any vars inside. 目前,我正在传递文件路径以从查找并替换其中的所有var之后创建新文件。 Ideally i'd like it to once its created the file automatically serve/download the file through the browser and not leave anything on the server hdd. 理想情况下,我希望它一旦创建文件,便会通过浏览器自动提供/下载文件,而不在服务器硬盘上保留任何内容。 Is this possible? 这可能吗? Here's my code i'm using to create the file; 这是我用来创建文件的代码;

  public static void CreateWordDoc(List<objReplace> lstReplace, String TemplateFile, String OutputPath)
    {
        var app = new Microsoft.Office.Interop.Word.Application();
        var doc = app.Documents.Open(TemplateFile);

        var range = doc.Range();
        foreach (var ro in lstReplace)
        {
            range.Find.Execute(FindText: ro.Find, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, ReplaceWith: ro.Replace);

            var shapes = doc.Shapes;

            foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
            {
                var initialText = shape.TextFrame.TextRange.Text;
                var resultingText = initialText.Replace(ro.Find, ro.Replace);
                shape.TextFrame.TextRange.Text = resultingText;
            }
        }
        doc.SaveAs2(OutputPath);

        doc.Close();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    }

Looks like you are automating Word on the server (ASP.NET?). 看起来您正在服务器(ASP.NET?)上自动化Word。

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时出现死锁或死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. 如果要构建在服务器端上下文中运行的解决方案,则应尝试使用对无人值守执行安全的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side. 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. 如果您从服务器端解决方案中使用Office应用程序,则该应用程序将缺少许多成功运行所需的功能。 Additionally, you will be taking risks with the stability of your overall solution. 此外,您将承担整体解决方案稳定性的风险。 Read more about that in the Considerations for server-side Automation of Office article. 在《 服务器端Office自动化注意事项》一文中了解有关此内容的更多信息。

You may consider using the Open XML SDK for dealing with Open XML documents, see Welcome to the Open XML SDK 2.5 for Office for more information. 您可以考虑使用Open XML SDK处理Open XML文档,有关更多信息,请参见欢迎使用Office Open XML SDK 2.5 Or just use any third-party components designed for the server side execution. 或仅使用为服务器端执行而设计的任何第三方组件。

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

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