小弟想请教一个问题,我用Microsoft.Office.Interop.Word将服务器上一个doc文件的书签的值改了,然后我想将这个doc当模板用,在打开的地方直接把文件不保存传给客户端代码如下:Word.Application wordApp = new Word.Application();
            object fileobj = docFileName;
            object nullobj = System.Reflection.Missing.Value;
            //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
            Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                );
            string strCase = string.Empty;
            foreach (Word.Book BM in doc.Books)  //这是最关键的地方:对文档的任何书签进行便利匹配
            {
                strCase = BM.Name.ToString();
                switch (strCase)
                {
                    case "E_ID": //替换Advice书签的内容,其他相同
                        BM.Select();
                        BM.Range.Text = "asd";
                        break;
                }
但是怎么才能给用户提供下载呢?请指教

解决方案 »

  1.   

    Word.Document 应该不提供 在内存中管理文档,因此只能存成临时文件,再读取。
      

  2.   

    有其它方法,但是不是太容易:
    下面文章讨论用 DocX
    save the document created by docX into response and send it to user for downloading
    http://stackoverflow.com/questions/8988959/save-the-document-created-by-docx-into-response-and-send-it-to-user-for-download
      

  3.   

    这篇文章讨论用OpenXML SDKStreaming In Memory Word Document using OpenXML SDK w/ASP.NET results in “corrupt” document
    http://stackoverflow.com/questions/10667513/streaming-in-memory-word-document-using-openxml-sdk-w-asp-net-results-in-corrup
      

  4.   

    I am unable to stream a word document that I create on the fly down to the browser. I am constantly getting a message from Microsoft Word that the document is corrupt.When I run the code via a Console Application and take ASP.NET out of the picture, the document is generated correctly with no problems. I believe everything centers around writing the file down.Here is my code:
    using (MemoryStream mem = new MemoryStream())
                {
                    // Create Document
                    using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, 
              WordprocessingDocumentType.Document, true))
                    {
                        // Add a main document part. 
                        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();                    new Document(new Body()).Save(mainPart);                    Body body = mainPart.Document.Body;
                        body.Append(new Paragraph(
                                    new Run(
                                        new Text("Hello World!"))));                    mainPart.Document.Save();
                        // Stream it down to the browser                    // THIS IS PROBABLY THE CRUX OF THE MATTER <---
                        Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
                        Response.ContentType = "application/vnd.ms-word.document";
                        mem.WriteTo(Response.OutputStream);
                        Response.End();
                    }            }Solution for those who stumble on this question:Within the using().. directive of the WordProcessingDocument, you must call:wordDocument.Save();Also to correctly stream the MemoryStream, use this in the outer using block:
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
                    mem.Position = 0;
                    mem.CopyTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();