Asp.net 从sharepoint打开word文档,将文本和流替换为用户

Asp.net 从sharepoint打开word文档,将文本和流替换为用户,asp.net,sharepoint,ms-word,Asp.net,Sharepoint,Ms Word,我希望能够在Sharepoint中存储一个模板word文档,并将其用作输出包含注入模板的数据的word文档的基础 我可以使用以下代码获取word文档的文本: SPSite sc = SPContext.Current.Site; SPWeb web = sc.AllWebs["MySite"]; string contents = web.GetFileAsString("Documents/MyTemplateWord.doc");

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

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

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

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

    web.Dispose(); 
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(); 
然后我可以替换“contents”变量上的字符串。这个很好用

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

我的代码如下:

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

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

    web.Dispose(); 
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或服务器跟踪时。详细信息:分析“附近”时出错ࡱ>

现在很明显,它在解析“字符串”内容时遇到了问题


我做错了什么?是否有其他方法可以执行此操作?

您不是在读取字符串,而是在将二进制数据转换为字符串。 (请记住,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);
}
使用(spwebweb=newspsite(“”.OpenWeb())
{
SPFile file=web.GetFile(“”);
byte[]content=file.OpenBinary();
HttpContext.Current.Response.Write(内容);
}
使用二进制写入将数据获取到页面

但是,由于您使用的是Word,我建议将文档加载到的实例中。
但是,使用单词interop可能有点费时费力。

1-将docx程序集添加到shaepoint包中

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);
        }