【问题标题】:Opening a word doc from sharepoint, replace text and stream to user从共享点打开 word 文档,替换文本并流式传输给用户
【发布时间】:2015-02-26 07:31:39
【问题描述】:

我希望能够将模板 word 文档存储在 Sharepoint 中,并将其用作吐出包含注入模板中的数据的 word doc 的基础。

我可以使用以下代码获取我的 word doc 的文本:

    SPSite sc = SPContext.Current.Site; 
    SPWeb web = sc.AllWebs["MySite"];             

    string contents = web.GetFileAsString("Documents/MyTemplateWord.doc"); 

    web.Dispose(); 

然后我可以对“内容”变量进行字符串替换。这很好用。

我现在想以 word doc 的形式“打开”这个新内容。

我的代码如下:

string attachment = "attachment; filename=MyWord.doc"; 
    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.ClearHeaders(); 
    HttpContext.Current.Response.ClearContent(); 
    HttpContext.Current.Response.AddHeader("content-disposition", attachment); 
    HttpContext.Current.Response.ContentType = "text/ms-word"; 
    HttpContext.Current.Response.Write(outputText); 
    HttpContext.Current.Response.End(); 

我遇到了一个错误,不知道如何解决它。

错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器接收到的消息。此错误的常见原因是通过调用 Response.Write()、响应过滤器、HttpModules 或启用了服务器跟踪来修改响应。详细信息:'ࡱ> 附近的解析错误

现在显然它在解析“字符串”内容时存在问题。

我做错了什么?我应该这样做吗?

【问题讨论】:

    标签: asp.net sharepoint ms-word


    【解决方案1】:

    您不是在读取字符串,而是将二进制数据转换为字符串。 (请记住,docx 是一个包含 xml 数据的 zip 文件)。 在这方面,您替换文本的方法的性质存在缺陷。

    如果不是出于查找/替换文本的愿望,我会推荐

    using(SPWeb web = new SPSite("<Site URL>").OpenWeb())
    {
        SPFile file = web.GetFile("<URL for the file>");
        byte[] content = file.OpenBinary();
        HttpContext.Current.Response.Write(content);
    }
    

    http://support.microsoft.com/kb/929265 使用 BinaryWrite 将数据获取到您的页面。

    但是,由于您使用的是 Word,我建议将文档加载到 Microsoft.Office.Interop.Word objects 的实例中。 但是,使用 Word 互操作可能有点像时间吸血鬼。

    【讨论】:

      【解决方案2】:

      1- 将 docx 程序集添加到您的 shaepoint 包 https://docx.codeplex.com

      2- 使用 Novacode;

      3-注意以下示例下载按钮

        protected void ButtonExportToWord_Click(object sender, EventArgs e)
              { 
                  byte[] bytesInStream;
      
                  using (Stream tplStream =
                      SPContext.Current.Web.GetFile("/sitename/SiteAssets/word_TEMPLATE.docx").OpenBinaryStream())
                  {
                      using (MemoryStream ms = new MemoryStream((int)tplStream.Length))
                      {
                          CopyStream(tplStream, ms);
                          ms.Position = 0L;
      
                          DocX doc = DocX.Load(ms);
                          ReplaceTextProxy(doc,"#INC#", "11111111");
      
                          doc.InsertParagraph("This is my test paragraph");
      
                          doc.Save();
                          bytesInStream = ms.ToArray();
                      }
                  }
      
                  Page.Response.Clear();
                  Page.Response.AddHeader("Content-Disposition", "attachment; filename=" +
                      CurrentFormId + ".docx");
                  Page.Response.AddHeader("Content-Length", bytesInStream.ToString());
                  Page.Response.ContentType = "Application/msword";
                  Page.Response.BinaryWrite(bytesInStream);
      
                  Page.Response.Flush();
                  Page.Response.Close();
                  //Page.Response.End();// it throws an error
              }
      
              private void ReplaceTextProxy(DocX doc, string oldvalue, string newValue)
              {
                  doc.ReplaceText(oldvalue,newValue,false, RegexOptions.None, null, null,
                              MatchFormattingOptions.SubsetMatch);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        相关资源
        最近更新 更多