SharePoint 更新word 等文档的内容,包括替换哦。功能强大

   public void UpdateDocument()
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteColl = new SPSite("http://localhost:8080/"))
                {
                    using (SPWeb web = siteColl.OpenWeb())
                    {
                        try
                        {
                            SPFile spfile = web.GetFile("http://localhost:8080/Lists/DemoLib/abc.txt");
                            if (spfile.Exists)
                            {
                                byte[] byteArrayFileContentsBefore = spfile.OpenBinary();
                                if (byteArrayFileContentsBefore.Length > 0)
                                {
                                    string strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore); //convert byte array to string. 
                                    string newStr = strFileContentsBefore.Replace("http://dev:999","http://google.com");
                                    byte[] byteArrayFileContentsAfter = null;
                                    if (!newStr.Equals(""))
                                    {
                                        byteArrayFileContentsAfter = enc.GetBytes(newStr);
                                        spfile.SaveBinary(byteArrayFileContentsAfter); //save to the file.  
                                    }
                                }
                            }
                        }
                        catch (Exception e) { }
                    }
                }
            });
        }

 

 

SPFile has a CopyFile method which can copy the file to a new location. But if there was an existing file on the new location, you can set the overwrite parameter to true to overwrite it. Here is a problem, supposingly there was a workflow already on the file in the new location... when you use CopyFile.. the workflow is lost.. basically it is not an update of the file.. it is infact a delete and re-adding of the file. The following code will overcome this problem

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void UpdateDocumentForERB_ExecuteCode(SPWeb web, string originalFileUrl, string targetFileUrl)
{
  SPSecurity.RunWithElevatedPrivileges( delegate ()
  {
   SPFile OriFile = web.GetFile(originalFileUrl);
   SPFile TarFile = web.GetFile(targetFileUrl);
  
   byte [] byteArrayOriFile = OriFile.OpenBinary();
  
   TarFile.SaveBinary(byteArrayOriFile);
  
  });
  
}
 
public void UpdateDocument()
{
  System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  
  SPSecurity.RunWithElevatedPrivileges( delegate ()
  {
   using (SPSite siteColl = new SPSite( "http://localhost:8080/" ))
   {
    using (SPWeb web = siteColl.OpenWeb())
    {
     try
     {   
      SPFile spfile = web.GetFile( "http://localhost:8080/Lists/DemoLib/abc.txt" );
      if (spfile.Exists)
      {
       byte [] byteArrayFileContentsBefore = spfile.OpenBinary();
  
       if (byteArrayFileContentsBefore.Length > 0)
       {
        string strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore); //convert byte array to string.
        string newStr = strFileContentsBefore + "This is the new text added" ;
        byte [] byteArrayFileContentsAfter = null ;
        if (!newStr.Equals( "" ))
        {
         byteArrayFileContentsAfter = enc.GetBytes(newStr);
         spfile.SaveBinary(byteArrayFileContentsAfter); //save to the file.
        }
       }
      }
     }
     catch (Exception e){}
   
   }
  });
}
 

转载于:https://www.cnblogs.com/ahghy/archive/2012/08/13/2635825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值